Jeffrey Kintscher <websur...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue41109>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to