universal_pathlib icon indicating copy to clipboard operation
universal_pathlib copied to clipboard

Inherit from `PathBase` instead of `Path`

Open ap-- opened this issue 2 years ago • 3 comments

This will be the next big change in UPath.

In the near future we will derive from pathlib_abc.PathBase:

from pathlib_abc import PathBase

class UPath(PathBase):
    ...

We will have to consider a few things before this is possible:

  • [ ] a clean deprecation mechanism for code that checks isinstance(p, Path)
  • [ ] how do WindowsUPath and PosixUPath change?

ap-- avatar Feb 18 '24 18:02 ap--

Here's a potential class hierarchy:

image

You're right that users would need to switch to checking isinstance(p, PathBase) to check for a rich path object.

Personally I see no reason for there to be PosixUPath and WindowsUPath classes. You could provide a LocalUPath class and leave it there, maybe?

barneygale avatar Apr 24 '24 19:04 barneygale

Hi @barneygale

Thank you for your message! I hope that I can get to this soon, so that it's ready for the Python3.13 release. I just saw that the 3.13 alpha is scheduled to release by the end of the month already. Are there any subtle changes that I should be aware of from versions 3.12 to 3.13?

I currently assume that at some point in time I will be able to do:

if sys.version_info >= (3, XX):
    from pathlib import PathBase  # or maybe pathlib.abc or somewhere else in the stdlib
else:
    from pathlib_abc import PathBase

You're right that users would need to switch to checking isinstance(p, PathBase) to check for a rich path object.

Do you have an idea how I could raise a deprecation warning when user code calls:

p = UPath(...)

isinstance(p, pathlib.Path)  # this should throw a warning
isinstance(p, str)  # (or anything other than pathlib.Path) should not raise a warning

Metaclasses with custom __instancecheck__ don't help because the logic is inverted. And overriding UPath.__class__ to return some object that then does magic to figure out the call context (I have not checked if that would work) seems like a horrible hack...

Personally I see no reason for there to be PosixUPath and WindowsUPath classes. You could provide a LocalUPath class and leave it there, maybe?

Yeah, ideally I would like to avoid having PosixUPath and WindowsUPath. But a while ago a few custom attributes and methods were added to UPath and so I needed to subclass. For relative paths, these are the only option as of now, because in the filesystem_spec world all paths are absolute. At some point in time, I think it would be good to have a LocalUPath class as you suggest, as well as a PureUPath that supports the different parsers (flavours) as well as relative paths to cover those use cases.

Cheers, Andreas

ap-- avatar May 04 '24 10:05 ap--

Do you have an idea how I could raise a deprecation warning [...]

I don't think so :(. I think the best you could do is to raise a warning from __fspath__(), which is arguably the most important interface difference between Path and PathBase.

barneygale avatar Jul 09 '24 22:07 barneygale