[issue41109] subclasses of pathlib.PurePosixPath never call __init__ or __new__

2020-06-24 Thread Étienne Pot

New submission from Étienne Pot :

I have a subclass GithubPath of PurePosixPath.

```
class GithubPath(pathlib.PurePosixPath):

  def __new__(cls, *args, **kwargs):
print('New')
return super().__new__(cls, *args, **kwargs)

  def __init__(self, *args, **kwargs):
print('Init')
super().__init__()
```

Calling `child.parent` create a new GithubPath but without ever calling __new__ 
nor __init__. So my subclass is never notified it is created.

```
p = GithubPath()  # Print "New", "Init"

p.parent  # Create a new GithubPath but bypass the constructors
```

The reason seems to be that parent calls _from_parts which create a new object 
through `object.__new__(cls)`: 
https://github.com/python/cpython/blob/cf18c9e9d4d44f6671a3fe6011bb53d8ee9bd92b/Lib/pathlib.py#L689


A hack is to subclass `_init` but it seems hacky as it relies on internal 
implementation detail.

------
messages: 372297
nosy: Étienne Pot
priority: normal
severity: normal
status: open
title: subclasses of pathlib.PurePosixPath never call __init__ or __new__
type: behavior
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

___
Python tracker 
<https://bugs.python.org/issue41109>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41109] subclasses of pathlib.PurePosixPath never call __init__ or __new__

2020-06-24 Thread Étienne Pot

Étienne Pot  added the comment:

Note that this likely affect all methods which returns new Path by calling 
`_from_parts` or `_from_parsed_parts`, like `.absolute`, `.resolve`,...

--

___
Python tracker 
<https://bugs.python.org/issue41109>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41109] subclasses of pathlib.PurePosixPath never call __init__ or __new__

2020-08-08 Thread Étienne Pot

Étienne Pot  added the comment:

Before solving this issue, I think it would be best to think on a more generic 
solution on how to make Pathlib more extensible. Related to: 
https://discuss.python.org/t/make-pathlib-extensible/3428

For instance, if childs created with `p.parent()`, `p / 'subdir'` need to 
forward some state (e.g. `RemotePath(path, password=, user=)`).

Rather than __init__, maybe there should be some __post_init__ like dataclasses.

--

___
Python tracker 
<https://bugs.python.org/issue41109>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42043] zipfile.Path should support inheritance

2020-10-15 Thread Étienne Pot

New submission from Étienne Pot :

Currently, zipfile.Path inheritance behavior is inconsistent with pathlib.Path:

```
class MyPath(zipfile.Path):
  pass


path = MyPath(zf)
path = path.joinpath('other')
assert isinstance(path, MyPath)  # Oups, type(path) is zipfile.Path
```

Calling parent, /,... should keep the class, as for pathlib.Path

Use case: I'm writing a wrapper around `zipfile.Path` which adds `os.fspath` 
compatibility (the file is extracted in a tmp dir when os.path.join, open,...).

--
components: Library (Lib)
messages: 378678
nosy: Étienne Pot
priority: normal
severity: normal
status: open
title: zipfile.Path should support inheritance
type: enhancement
versions: Python 3.10

___
Python tracker 
<https://bugs.python.org/issue42043>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com