Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bpo-24132: Addpathlib._AbstractPath#31085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

barneygale
Copy link
Contributor

@barneygalebarneygale commentedFeb 2, 2022
edited
Loading

After a couple of years of mostly deleting things in pathlib, I'm glad to finally offer an addition!

This PR adds a new_AbstractPath class that sits betweenPurePath andPath in the class hierarchy._AbstractPath objects have all the same methods asPath objects, but their filesystem-accessing methods consistently raiseNotImplementedError.

(updated Feb 15th: now, onlystat(),open() anditerdir() are abstract; other methods that directly access the filesystem are not part of the_AbstractPath interface).

This class will form the basis of a future public-facing abstract class that can be used by the likes ofs3path andcloudpathlib. It could also be used to makezipfile.Path objects fully pathlib-compatible (no missing methods!)

Why is this underscore-prefixed?I think this needs some time to gestate in CPython before we write full docs/tests and remove the prefix. I'd make an appeal to the authors of pathlib-y packages on PyPI to try it in an experimental branch and let us know where the pain points are. Three or so roadblocks remain before we can document and recommend it. Seethis comment

https://bugs.python.org/issue24132

yan12125 reacted with eyes emoji
The `Path` class is now an *implementation* of `_AbstractPath`; its methodscall functions in `os.path`, `io`, `pwd`, etc, whereas the correspondingmethods in `_AbstractPath` instead raise `NotImplementedError`.
Check the presence of the necessary `os`, `pwd` and `grp` functions atimport time.
Copy link
Member

@AlexWaygoodAlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I love the overall idea of this PR, but I'm afraid I think trying to cram everything into one ABC is the wrong approach -- the interface still feels a little confused to me.

For example -- I'm a static-typing guy, and I have no idea how we'd typeAbstractPath.absolute() in typeshed. For a class that overrideAbstractPath.cwd(),AbstractPath.absolute() returns an instance of that class. But subclasses ofAbstractPath can't be guaranteed to overrideAbstractPath.cwd(), because it's not anabstractmethod, because it's not part of the core interface. Which means that we couldn't include this method in the typeshed stub forAbstractPath at all, because it would be unsafe for an instance of anAbstractPath subclass to ever callabsolute(). So, why isabsolute() inAbstractPath at all?

I'm usingcwd() andabsolute() as an example, but I think this is a broader problem for all of the methods inAbstractPath that raiseNotImplementedError but are notabstractmethods (and, by extension, all of the mixin methods that call methods that might-or-might-not be implemented).

I would counsel splittingAbstractPath up into several smaller ABCs. In typeshed we could pretend that these are PEP 544 protocols, in much the same way we do withos.PathLike, whichis an ABC at runtime but isconsidered aProtocol by static type checkers.

Instead of having a structure like this:

classAbstractPath(PurePath,ABC):# core interface@abstractmethoddefiterdir(self): ...@abstractmethoddefstat(self,*,follow_symlinks=True): ...@abstractmethoddefopen(self,mode='r',buffering=-1,encoding=None,errors=None,newline=None): ...@classmethoddefcwd(cls): ...# Not abstract, but raises NotImplementedErrordefabsolute(self): ...# Depends on cwd() being implementeddefresolve(self): ...# Not abstract, but raises NotImplementedErrorclassPath(AbstractPath): ...

You could instead have something like this:

classAbstractBasePath(PurePath,metaclass=ABCMeta):"""I represent the minimum requirements for a class to implement a pathlib-esque interface"""@abstractmethoddefiterdir(self): ...@abstractmethoddefstat(self,*,follow_symlinks=True): ...@abstractmethoddefopen(self,mode='r',buffering=-1,encoding=None,errors=None,newline=None): ...classResolveablePathMixin(metaclass=ABCMeta):"""Mixin ABC for paths that can be resolved in some sense"""@classmethod@abstractmethoddefcwd(cls): ...defabsolute(self): ...# Concrete method that calls cwd()@abstractmethoddefresolve(self): ...classAbstractPath(AbstractBasePath,ResolveablePathMixin):"""I represent the full interface for a pathlib Path"""# This class body would be completely empty# The only purpose of this class would be to accumulate# all of the abstractmethods from AbstractBasePath# and all of the mixin classesclassPath(AbstractPath):# This class body would be filled with concrete implementations# of all the abstract methods accumulated in `AbstractPath`

In this way, users could decide whether they want to implement just the core interface, by subclassingAbstractBaseClass; the fullpathlib interface, by subclassAbstractPath; or something custom, by using multiple inheritance to subclassAbstractBaseClass and one of the smaller mixin classes at the same time.

(You're far more knowledgeable aboutpathlib internals than I am, so: apologies if I've got any of thepathlib-specific details wrong here!)

barneygale reacted with heart emoji
@AlexWaygood
Copy link
Member

I would look to thecollections.abc module for inspiration -- observe how theMutableSequence interface is built out of an accumulation of smaller interfaces:

@barneygale
Copy link
ContributorAuthor

Thank you so much Alex, that's great advice.

I need to give this idea some serious consideration and work out exactly which ABCs would be involved.

My kneejerk concerns about this are as follows:

  1. That there may betoo many ABCs and combinations thereof. For example, tar archives support symlinks (read+write) but not hardlinks; other APIs may support reading symlink targets but not creating symlinks. I worry there's a difficult multiple taxonomies problem here, but I'm speculating. I need to actually try it.
  2. That pathlib canalready raiseNotImplementedError forreadlink(),symlink_to(),hardlink_to(),owner() andgroup(), and perhaps we need to do something about that first, e.g. make them raiseOSError withEPERM orENOSYS?

@barneygale
Copy link
ContributorAuthor

I've loggedbpo-46733 to address the existing misuse ofNotImplementedError. I suspect the outcome of that ticket will narrow our options here :-)

AlexWaygood reacted with thumbs up emoji

@barneygale
Copy link
ContributorAuthor

Update on this PR: I've reduced the number of abstract methods substantially! Now onlystat(),open() anditerdir() are abstract. I hope that addresses some concerns. Any more feedback? :)

On the python-dev mailing list, Brett Cannon suggested this could be a protocol rather than an ABC. My only concern there is that the protocol would be pretty extensive, because the PurePath + _AbstractPath APIs are pretty extensive, even with methods likecwd() andhome() culled. I'd appreciate any guidance anyone might have!

AlexWaygood reacted with thumbs up emoji

@AlexWaygood
Copy link
Member

AlexWaygood commentedFeb 21, 2022
edited
Loading

Update on this PR: I've reduced the number of abstract methods substantially! Now onlystat(),open() anditerdir() are abstract. I hope that addresses some concerns. Any more feedback? :)

I think the interface ismuch clearer now — thank you! I won't formally "approve" the PR, because I just don't know enough about pathlib, but the overall design of the class looks really strong to me now.

W.r.t. ABCs versus protocols — I agree that it seems too big to be a protocol. Additionally, a protocol cannot inherit from a concrete class, so you'd have to change the inheritance structure if you wanted to make it a protocol (PurePath is concrete).

The only other piece of feedback I have is that I agree with Ethan Furman's comments on python-dev — I'd make the class public (name itpathlib.AbstractPath rather thanpathlib._AbstractPath). I think either we "go for it", or it's not really worth doing — it doesn't make sense to simultaneously say "We'd like users to experiment with this, please send us your feedback", and "Watch out, this is a private-to-this-module implementation detail, users beware" 🙂

barneygale reacted with thumbs up emoji

@barneygale
Copy link
ContributorAuthor

Thanks very much Alex.

On making thisAbstractPath rather than_AbstractPath: I think there are still too many rough edges for me to write docs for this without wincing throughout. Particularly:

  1. Subclassingonly fromAbstractPath doesn't work, because_flavour is unset. You currently need to additionally subclassPurePosixPath orPureWindowsPath, or set_flavour yourself. I'd like to rectify this by simultaneously removing flavours and givingPurePath posix path behaviour by default. (If I can be cheeky and appeal for reviewers, I have two simple PRs available before the main event:bpo-44136: pathlib: merge_Flavour.make_uri() intoPurePath.as_uri() #30320 andbpo-44136: pathlib: merge_Flavour.is_reserved() intoPurePath.is_reserved() #30321)
  2. How do we persist state (sockets, file objects, etc) to child objects, e.g. those generated bypath / 'foo'? It can be done already with a further subclass, but it feels odd.
  3. Should we recommend that users instantiateos.stat_result objects themselves when implementingstat()? If so think we need to add a nicer constructor. Or perhaps we add a newstat.Status class (protocol?) with a little more object-orientation?

What I'll do instead is withdraw my recommendation for limited experimentation by third parties - I'll adjust the PR description and the_AbstractPath docstring. Hopefully that clears things up!

AlexWaygood reacted with thumbs up emoji

@brettcannon
Copy link
Member

I think either we "go for it", or it's not really worth doing

The real question is whether the API will be changingat all, or does@barneygale have some other changes planned which would influence this? Once this is made public there's no going back without a lot of pain, so we should be thoughtful about how the API will look and what is going to be asked of users.

AlexWaygood, barneygale, and zmievsa reacted with thumbs up emoji

@brettcannonbrettcannon self-requested a reviewFebruary 22, 2022 23:35
@barneygale
Copy link
ContributorAuthor

I don't want to commit to freezing the interface just yet. But it's not too far away - I think we'll be able to do it in 3.12 or 3.13.

Even without makingAbstractPath public (yet!), this PR is still worthwhile IMHO. The implementation of pathlib is simpler to understand and work. It allows us to incrementally add tests as we freeze parts of the interface. The outstanding issues I mentioned in my previous comment are unlikely to affect the interface much, if at all.

@AlexWaygood
Copy link
Member

Even without makingAbstractPath public (yet!), this PR is still worthwhile IMHO. The implementation of pathlib is simpler to understand and work. It allows us to incrementally add tests as we freeze parts of the interface. The outstanding issues I mentioned in my previous comment are unlikely to affect the interface much, if at all.

That makes sense to me -- and it's good to have that clarified! :)

@brettcannon
Copy link
Member

My current thinking (while I work through my PR review backlog to reach this PR), is this should be used forzipfile.Path. I also wouldn't be apposed to that happening this PR if@barneygale wanted to give that a shot while he waits for a review from me.

@barneygale
Copy link
ContributorAuthor

Yep, will do! That helps clarify the order of things a lot actually. I think we need to do these things first (in this order):

  1. MakePurePath (and hencePath) directly subclassable, without needing to worry about_flavour. PR here:gh-68320, gh-88302 - Allow forpathlib.Path subclassing #31691
  2. Unify the creation of child paths in a single method, likezipfile.Path._next() does.

I'm going to mark this PR as a draft for now, will circle back when I've solved those. We'll be able to drop the underscore prefix then, too!

brettcannon reacted with thumbs up emojiAlexWaygood reacted with heart emoji

@barneygalebarneygale marked this pull request as draftMarch 5, 2022 02:45
@brettcannon
Copy link
Member

@barneygale do you still want to wait to have this PR reviewed until the API can go public? Or can it be reviewed as-is and the API kept private for now and consider this PR as cleaning up code? I'm trying to clean up my PR review queue and so if this is going to stay in draft because you only want this merged if the API can go public I will unassign myself until it's ready.

@barneygale
Copy link
ContributorAuthor

@barneygale do you still want to wait to have this PR reviewed until the API can go public? Or can it be reviewed as-is and the API kept private for now and consider this PR as cleaning up code? I'm trying to clean up my PR review queue and so if this is going to stay in draft because you only want this merged if the API can go public I will unassign myself until it's ready.

Please go ahead and unassign yourself! I'd like to makezipfile.Path use this new API, but there's some work to do elsewhere first.

brettcannon reacted with thumbs up emoji

@brettcannonbrettcannon removed their request for reviewApril 14, 2022 18:45
@zmievsa
Copy link
Contributor

@barneygale Do I understand correctly that the aim of this change is to make something similar to pathlab somewhat easier to do? I.e. Allowing pathlib capabilities to be used outside of regular system paths and extend it to any kinds of paths that share similar semantics. If so, I'd love to help with it, if you need any help.

@barneygale
Copy link
ContributorAuthor

barneygale commentedMay 11, 2022
edited
Loading

@barneygale Do I understand correctly that the aim of this change is to make something similar to pathlab somewhat easier to do? I.e. Allowing pathlib capabilities to be used outside of regular system paths and extend it to any kinds of paths that share similar semantics. If so, I'd love to help with it, if you need any help.

Yep that's exactly right! Could be archived files (e.g. in.zip and.tar files) or remote files (e.g. on S3, FTP) - if we give them a universal interface, users can learn one API and apply it everywhere. I'll have a think and let you know where I could do with a hand :D thanks for the offer.

zmievsa reacted with thumbs up emoji

@barneygale
Copy link
ContributorAuthor

Once#100479 is resolved, I'll resume work on this PR.

@barneygale
Copy link
ContributorAuthor

I'll resume work on this once#100481 lands.

@barneygale
Copy link
ContributorAuthor

Contrary to my earlier comments, I'm going to abandon this PR and continue the work elsewhere. Specifically, I'm planning to add apathlib._AbstractPath class in order toimplementtarfile.TarPath.

I'm going to leavezipfile.Path alone for now. It's somewhat incompatible with pathlib at the moment, and all the solutions I've considered involve a deprecation cycle. That will be better motivated once we haveTarPath in place.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@brettcannonbrettcannonbrettcannon left review comments

@merwokmerwokmerwok left review comments

@AlexWaygoodAlexWaygoodAlexWaygood left review comments

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

8 participants
@barneygale@AlexWaygood@brettcannon@zmievsa@merwok@the-knights-who-say-ni@ezio-melotti@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp