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

Support for detecting the flavour of pathlib subclasses #100502

Closed
Labels
@barneygale

Description

@barneygale

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 thirdpartypath package)

Alternatives

We could make thePurePosixPath andPureWindowsPath classes 'protocols', which supportisinstance() checks even when the passed object isn't a conventional subclass. But:

  1. The implementation will be more complex
  2. It could open a can of worms about whetherPurePosixPath andPureWindowsPath should 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.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp