Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Subclass of pathlib.Path doesn't support "/" operator

I’m attempting to create a subclass of pathlib.Path that will do some manipulation to the passed string path value before passing it along to the base class.

class MyPath(Path):
    def __init__(self, str_path):
        str_path = str_path.upper() # just representative, not what I'm actually doing

        super().__init__(str_path)

However, when I try to use this:

foo = MyPath("/path/to/my/file.txt")
bar = foo / "bar"

I get the following error: TypeError: unsupported operand type(s) for /: 'MyPath' and 'str'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am using Python 3.12 which I understand to have better support for subclassing Path

>Solution :

MyPath("foo") / "bar' goes through several steps:

  1. It invokes MyPath("foo").__truediv__("bar"), which
  2. Invokes MyPath("foo").with_segments("bar"), which
  3. Invokes MyPath(MyPath("foo"), "bar"), which
  4. Raises a TypeError because you overrode MyPath.__init__ to only accept a single additional positional argument, which
  5. Causes MyPath.__truediv__ to catch a TypeError and subsequently return NotImplemented

Your __init__ method must be careful to accept multiple path components, not just a single string, and it must not assume that each argument is an actual string, but rather objects that implement the os.PathLike interface. (In particular, don’t assume that a path component has an upper method. Call os.fspath on the component first to retrieve a str representation that does have an upper method.)

Something like

class MyPath(Path):
    def __init__(self, *components):

        str_path = (os.fspath(x).upper() for x in components)

        super().__init__(*str_path)

and thus

% py312/bin/python -i tmp.py
>>> bar
MyPath('/PATH/TO/MY/FILE.TXT/BAR')

Alternately, as others have alluded to, you can override with_segments to, for example, combine the segments yourself and pass the single result to MyPath, but I see no reason to restrict MyPath.__init__‘s signature as you currently do.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading