[issue41109] subclasses of pathlib.PurePosixPath never call __init__ or __new__
Jeffrey Kintscher
report at bugs.python.org
Thu Aug 13 22:12:51 EDT 2020
Jeffrey Kintscher <websurfer at surf2c.net> added the comment:
The current implementation calls object.__new__(cls), where cls is the child class type, from within a class method (@classmethod). This is fine for Path.__new__() and PurePath.__new__(), which are called by the child class's __new__(), because we don't want them to recursively call the child class's __new__() when the child class is created. This all works as expected when the child class is instantiated outside of Path and PurePath, and the child's __init__() gets called as expected. I don't see any point in making changes to this behavior.
When one of approximately 20 PurePath and Path functions and properties instantiate a new child class object the same way PurePath.__new__() and Path.__new__() do, the child class's __new__() and __init__() functions are not called. This is the problem we are trying to solve.
My fix is to add normal functions (i.e. not decorated with @classmethod) to instantiate child class objects using
obj = type(self)()
This creates a child class instance, and the child's __new__() and __init__() functions get called.
Once I have finished re-plumbing Path and PurePath to use the new functions and created the necessary unit tests (to make sure I didn't break anything), I will also look at adding
a proper __init__() function to the two classes instead of having __new__() initialize the member variables. I didn't mean to imply that __init__() isn't useful. It is required to allow the child class to initialize its own variable. I just meant it isn't required to force calling __init__() and __new__() in the child class.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41109>
_______________________________________
More information about the Python-bugs-list
mailing list