Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.1k
bpo-24132: Direct sub-classing of pathlib.Path#26438
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Introduce stub classes with initial tests on what will becomesubclassable alternatives to PurePath/Path that do not haveinverted dependencies.
Replace references in PurePath and changed function in_BasePurePathTest.test_ordering_common to test missing execution paths.This is preparation for seperating the base class and factoryfunctionality in PurePath in order to fully implement SimplePath.
Introduce new base class _PurePathBase and move all of the methodsfrom PurePath into it (except for the factory __new__) for use inbuilding SimplePath / FilePath.
SimplePath provides a subclassable alternative to PurePath which isotherwise functionally equivalent -- passing all of the existingPurePath tests.
Partial implementation of FilePath which offers all of the PurePathand SimplePath functionality and passes the equivalent existing tests.
Existing is_mount method in Path was Posix only and not crossplatform even though it was in a cross-platform base class. The existingdesign relied upon being overriden later in WindowsPath. Consolidatedcode from both into new Path.is_mount() which is now cross plaformsimiliar to all of the other Path methods.
Replace hardcorded references to pathlib.Path so that the baseclass and factory functionality of Path can be seperated.
FilePath provides a subclassable alternative to Path which isotherwise functionally equivalent -- passing all of the existingPath tests.
SimplePosixPath and SimpleWindowsPath provide a subclassablealternative to PurePosixPath and PureWindowsPath which are otherwisefunctionally equivalent -- passing all of the existing PurePosixPathand PureWindowsPath tests.
Fix _BasePurePath comparison operators so that all of the newpath classes in pathlib are comparable with one another and returnresults that are consistent with the existing behavoir, i.e.Path('/') == PurePath('/').
Document and make public FilePath, SimplePath, SimplePosixPath,SimpleWindowsPath, and PathIOMixin. Also add section to pathlibdocumentation regarding the limitations of PurePath and Path withregards to subclassing and how to use these new classes as alternatives.
This is a key design decision - should we depart from pathlib's existing My personal view is that this is just too large a departure from how I don't think this needs 5 new classes to implement. We have options for how to make creating a trio of I also have an incurable phobia of mixin classes :( |
kfollstad commentedMay 31, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@barneygale, thank you very much for taking the time to review my PR / ideas thread. I know you have spent a lot of time wrestling with |
kfollstad commentedJun 25, 2021 • edited by bedevere-bot
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by bedevere-bot
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Updated based on feedback - currently marked as draft
I submit for your consideration what I believe is the first working version (with documentation) of an extensible, subclassable PurePath/Path to closebpo-24132, a 6-year-old bug. I have alsoposted a more detailed version of this explanation on discuss.python.org because this fix involves adding classes to pathlib and understanding why that is required takes a significant explanation.
These classes are a complete set of alternatives to PurePath and Path which omit the factory design but instead attach the flavour to the class at the time of instantiation. These newly designed classes still pass all of the test cases that are run against PurePath and Path on both Windows and Linux. Because they omit the factory design, they don't have inverted dependencies and therefore are naively subclassable and extensible in any way you see fit.
To this end I give you:
SimplePath (subclassable alternative to PurePath)
SimplePosixPath (subclassable alternative to PurePosixPath)
SimpleWindowsPath (subclassable alternative to PureWindowsPath)
FilePath (subclassable alternative to Path/PosixPath/WindowsPath)
On Windows, SimplePath behaves as if it were PureWindowsPath. On Posix, it behaves as PurePosixPath. Similarly, FilePath behaves like WindowsPath on Windows and PosixPath on Posix. These four classes combined could, if one were so inclined, act as a complete replacement for the existing six Path/PurePath classes.
Despite being 11 commits, it's essentially just two refactors. The first splits the two responsibilities PurePath has, separating the base class methods into _PurePathBase. The second does the same with Path, moving the base class methods into PathIOMixin. The other commits are just minor tweaks and documentation to account for all of that.
https://bugs.python.org/issue24132