[Python-ideas] PEP 428 - object-oriented filesystem paths

Nick Coghlan ncoghlan at gmail.com
Sat Oct 13 09:41:29 CEST 2012


On Sat, Oct 13, 2012 at 7:00 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> My point about the Path(...(str(...))) sandwich still applies, though, for
> every function that isn't built in to Path.  :)

It's the same situation we were in with the design of the new
ipaddress module, and the answer is the same: implicit coercion just
creates way too many opportunities for errors to pass silently. We had
to create a backwards incompatible version of the language to
eliminate the semantic confusion between binary data and text data,
we're not going to introduce a similar confusion between arbitrary
text strings and objects that actually behave like filesystem paths.

str has a *big* API, and much of it doesn't make any sense in the
particular case of path objects. In particular, path objects shouldn't
be iterable, because it isn't clear what iteration should mean: it
could be path segments, it could be parent paths, or it could be
directory contents. It definitely *shouldn't* be individual
characters, but that's what we would get if it inherited from strings.

I do like the idea of introducing a "filesystem path" protocol though
(and Antoine's already considering that), which would give us the
implicit interoperability without the inheritance of an overbroad API.

Something else I've been thinking about is that it still feels wrong
to me to be making the Windows vs Posix behavioural decision at the
class level. It really feels more like a "decimal.Context" style API
would be more appropriate, where there was a PathContext that
determined how various operations on paths behaved. The default
context would then be determined by the current OS, but you could
write:

    with pathlib.PosixContext:
        # "\" is not a directory separator
        # "/" is used in representations
        # Comparison is case sensitive
        # expanduser() uses posix rules

    with pathlib.WindowsContext:
        # "\" and "/" are directory separators
        # "\" is used in representations
        # Comparison is case insensitive

Contexts could be tweaked for desired behaviour (e.g. using "/" in
representations on Windows)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list