Skip to content

Accessing private attributes / methods available on pathlib.Path classes? #339

Open

Description

@lmmx

Hi, I don’t know if I’m misunderstanding the nature of the ‘shim’ here but the 3.12 slots are not actually being populated in the UPath as I expected when I found this code. Specifically accessing _raw_paths fails.

I was really hoping to find a backport for this as it’s used in my library to preserve the leading ./ part of relative file paths like ./foo.txt which regular Path “normalises” to foo.txt.

Have I misunderstood the purpose of the shim class or is this a bug?

Activity

  1. ap-- commented on Feb 5, 2025

    @ap--
    Collaborator

    Hi @lmmx

    Thanks for reporting! The issue you're describing is happening because of the way local path handling in universal-pathlib was designed.

    To ensure that universal-pathlib's upath.UPath can be used as a drop-in replacement for pathlib.Path it was decided to guarantee as much as possible that the two are compatible for local paths. This is why there are three local path classes in universal-pathlib:

    PosixUPath, WindowsUPath and FileUPath.

    The first two are basically using the PosixPath and WindowsPath implementations of pathlib. That's why on python<=3.11 none of them populate _raw_paths. FileUPath on the other hand uses fsspec's LocalFileSystem for handling local paths and populates _raw_paths. The distinction is made because fsspec ensures that all relative paths are made absolute, and that changes some of the behavior.

    Compare the following on Python3.11

    >>> import sys
    >>> sys.version
    '3.12.8 (main, Dec  3 2024, 18:42:41) [Clang 16.0.0 (clang-1600.0.26.4)]'
    >>>
    >>> import upath
    >>> upath.__version__
    '0.2.6'
    >>>
    >>> p0 = upath.UPath("./abc.txt")
    >>> p0
    PosixUPath('abc.txt')
    >>> p0._raw_paths
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/andreaspoehlmann/Development/universal_pathlib/upath/core.py", line 463, in __getattr__
        raise AttributeError(item)
    AttributeError: _raw_paths
    >>>
    >>> p1 = upath.UPath("./abc.txt", protocol="file")
    >>> p1
    FilePath('file:///Users/andreaspoehlmann/Development/universal_pathlib/abc.txt')
    >>> p1._raw_paths
    ['./abc.txt']
    >>> 

    The next release of universal-pathlib will likely change how all of this handled, but it needs some more thought.

  2. changed the title [-]Python 3.12 slots not accessible in 3.11[/-] [+]Accessing private attributes / methods available on pathlib.Path classes on UPath?[/+] on May 9, 2025
  3. changed the title [-]Accessing private attributes / methods available on pathlib.Path classes on UPath?[/-] [+]Accessing private attributes / methods available on pathlib.Path classes?[/+] on May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question ❓Further information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions