Message375047
Path.__new__ should not call _from_parts because it breaks the specialization by directly using object.__new__.
This is why `_from_parts` has to be duplicated in each subclass's `__new__`.
My suggestion (first draft) is to do the parsing of arguments inside an `__init__` in the Path class hierarchy
and deprecate `_from_parts`.
```
class PurePath:
def __new__(cls, *args):
if cls is PurePath:
cls = PureWindowsPath if os.name == 'nt' else PurePosixPath
super().__new__(cls, *args) # Here we remove call to from_parts
def __init__(self, *args):
# We should have an __init__ in the hierarchy.
drv, root, parts = self._parse_args(args) # this would get the proper _flavour.
self._drv = drv
self._root = root
self._parts = parts
...
class Path(PurePath):
def __new__(cls, *args, **kwargs):
if cls is Path:
cls = WindowsPath if os.name == 'nt' else PosixPath
# REMOVE THIS LINE: self = cls._from_parts(args, init=False) #
if not self._flavour.is_supported:
raise NotImplementedError("cannot instantiate %r on your system"
% (cls.__name__,))
return super().__new__(cls, *args, **kwargs) # Use super
```
I don't know what is the purpose of `_init` and if it could be replaced by an extra keyword argument in __init__.
The class method `_from_parts` would become deprecated since the argument parsing would be now handled by `__init__`.
By using __init__ and avoid the direct call to `object.__new__` we would respect class specialization and custom class could be implemented intuitively. |
|
Date |
User |
Action |
Args |
2020-08-08 13:47:37 | louis-vincent.boudre | set | recipients:
+ louis-vincent.boudre, pitrou, Jeffrey.Kintscher, conchylicultor |
2020-08-08 13:47:37 | louis-vincent.boudre | set | messageid: <1596894457.45.0.691783286863.issue41109@roundup.psfhosted.org> |
2020-08-08 13:47:37 | louis-vincent.boudre | link | issue41109 messages |
2020-08-08 13:47:36 | louis-vincent.boudre | create | |
|