Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
Description
Feature or enhancement
Provide a way of checking whether a user subclass ofpathlib.PurePath andPath uses POSIX or Windows semantics
The Problem
Now that#68320 is resolved, users can subclasspathlib.PurePath andPath directly:
importpathlibclassMyPath(pathlib.Path):passpath=MyPath('foo')
However, there's no (public) procedure to detect whether an instance ofMyPath uses POSIX or Windows semantics.
A non-public way of doing it:
path._flavourisposixpath# Check whether the path object uses POSIX semanticspath._flavourisntpath# Check whether the path uses Windows semanticspath._flavourisos.path# Check whether the path uses the current OS's semantics
Note that checkingisinstance(path, PurePosixPath) (etc) won't work, as user subclasses ofPurePath andPath do not have the POSIX- and Windows-specific subclasses in their class hierarchy.
The Proposal
Make the_flavour attribute public; document it. Possible names:
flavour(simply remove the underscore)pathmod(used internally in older pathlib versions)module(used in the longstanding thirdpartypathpackage)
Alternatives
We could make thePurePosixPath andPureWindowsPath classes 'protocols', which supportisinstance() checks even when the passed object isn't a conventional subclass. But:
- The implementation will be more complex
- It could open a can of worms about whether
PurePosixPathandPureWindowsPathshould be proper protocols, and not directly instantiable.
A further alternative would be to add anis_posix() method, which gels pretty nicely with the existingis_*() andas_posix() methods.