| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
`posixpath`. As a result, user subclasses of `PurePathBase` and `PathBase` use POSIX path syntax by default, which is very often desirable.
…t it to a runtime-checkable protocol.
…ies. Objects of this type provide a subset of the `os.DirEntry` API, specifically those methods and attributes needed to implement `glob()` and `walk()`.
Objects of this type provide a small subset of the `os.stat_result` API, specifically attributes for the file type, permissions and location/offset.
This looks reasonable, but StatResult looks rather useless with its three mandatory but unreliable members:
Also, I guess typing has no way to say that e.g. if st_birthtime is present, it needs to be a time in seconds. It seems that for your goal of supporting virtual filesystems, it might be better to rely on DirEntry, with stat remaining as a backend for filesystems where it's useful. def key(self) -> Hashable:
"""Return a comparison key. Entries with the same key refer to the same file."""
# For a Unix-like FS:
...
return (stat_result.st_dev, stat_result.st_ino)
|
Sorry, something went wrong.
|
Thank you for the feedback and reviews.
I've occasionally thought about adding a PathBase.status() abstract method, which could return a DirEntry-like object with caching methods. Methods like PathBase.is_dir() could then be implemented via self.status().is_dir(). If we did that, we could either delete PathBase.stat(), or implement it in terms of self.status(). Would that be better? I don't think DirEntry is an appropriate return type for status(), because we're not scanning the parent directory, rather we're querying the path directly. Perhaps it's just a naming problem. |
Sorry, something went wrong.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Status, then? Like stat, but not a C-style abbr. |
Sorry, something went wrong.
|
I'm not a fan of the stat() model at all. It's the best of a bad lot, I know, but I'd rather have some kind of API that lets us use cached information for is_file()/is_dir()/etc. rather than having to guarantee that it's fresh for each call. I'm not sure what such an API should look like, apart from I want it to have the Path object interface, and be using cached responses about the state of the path (or fetching them and caching on demand). There are more than enough properties that might as well be cached that it's worth it, and the TOCTOU issues basically all exist even with our current model. |
Sorry, something went wrong.
|
Brainstorming: A downside is that this breaks the rule that touching the filesystem is done by methods, not attributes. (Can we say rule was for getting “fresh” information, and we can use something else here?) |
Sorry, something went wrong.
|
This all sounds great to me.
Why would accessing Path.info touch the filesystem? Couldn't make it so that only FileInfo.is_*() methods can perform FS access? |
Sorry, something went wrong.
|
Oh, I suppose you might want to lstat() or stat() so that FileInfo always represents something that exists/existed. Otherwise we'd need a FileInfo.exists() method, and if we're intending to store os.DirEntry objects as Path.info when paths are generated from iterdir(), then we'd need to add an os.DirEntry.exists() method too. |
Sorry, something went wrong.
|
Courtesy CCing @ncoghlan, who proposed something similar here. I shot it down then because it wasn't clear to me when to call [l]stat(), how to clear caches, and how it interacts with Path.is_*(). I'm also a tiny bit uneasy making Path stateful. |
Sorry, something went wrong.
|
(Process note - we should probably move the API design discussion off this PR) exists() shouldn't be an issue, because we can cache that as True or False at creation time (e.g. returned from glob() implies that exists() == True at some point in the recent past; created by a user implies that exists() is unknown and needs to make a filesystem call). The same could potentially be true for any other state, most obviously is_dir() and is_file(), but also any of the regular stat attributes (where a subclass could well return their own type from .stat()). What we need to design is the way for a user to discover that they've got cached values, and reset it. A CachedInfo mixin (just for type check purposes) and a dedicated subclass (so that e.g. Path(p) would recreate and uncache all members) would seem to suffice, though I can also see the reasoning for adding a new member to manage it. |
Sorry, something went wrong.
|
Following up on the forum: https://discuss.python.org/t/ergonomics-of-new-pathlib-path-scandir/71721/30 |
Sorry, something went wrong.
|
I've removed DirEntry and StatResult from this PR, so it now covers only only Parser. |
Sorry, something went wrong.
There was a problem hiding this comment.
The reduced PR to just the parser changes LGTM
Sorry, something went wrong.
|
Thanks both, very much appreciate the feedback :-) |
Sorry, something went wrong.
…27494) Change the default value of `PurePathBase.parser` from `ParserBase()` to `posixpath`. As a result, user subclasses of `PurePathBase` and `PathBase` use POSIX path syntax by default, which is very often desirable. Move `pathlib._abc.ParserBase` to `pathlib._types.Parser`, and convert it to a runtime-checkable protocol. Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
| Back | FazBrowse Home | New Git URL |
Change the default value of PurePathBase.parser from ParserBase() to posixpath. As a result, user subclasses of PurePathBase and PathBase use POSIX path syntax by default, which is very often desirable.
Move pathlib._abc.ParserBase to pathlib._types.Parser, and convert it to a runtime-checkable protocol.
No user-facing changes as the pathlib ABCs are still private.