[New-bugs-announce] [issue41109] subclasses of pathlib.PurePosixPath never call __init__ or __new__
Étienne Pot
report at bugs.python.org
Wed Jun 24 22:51:23 EDT 2020
New submission from Étienne Pot <etiennefg.pot at gmail.com>:
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 <report at bugs.python.org>
<https://bugs.python.org/issue41109>
_______________________________________
More information about the New-bugs-announce
mailing list