[Python-Dev] Defining a path protocol

Koos Zevenhoven k7hoven at gmail.com
Fri Apr 8 19:05:41 EDT 2016

On Sat, Apr 9, 2016 at 12:53 AM, Brett Cannon <brett at python.org> wrote:
> On Fri, 8 Apr 2016 at 14:23 Koos Zevenhoven <k7hoven at gmail.com> wrote:
> At this point no one wants to touch bytes paths. If you need that level of
> control because of multiple encodings within a single file system then you
> will probably have to stick with managing bytes paths on your own to get the
> encoding right.

What does this mean? I assume you don't mean os.path.* would stop
dealing with bytes? And if not, then you seem to mean that os.fspath
would do nothing except call .__fspath__(). In that case, I think we
should go back to it being an attribute (or property) and a variation
of the now very famous idiom getattr(path, '__fspath__', path) and
perhaps have os.fspath do exactly that.

> And just because DirEntry supports bytes doesn't mean that any magic method
> it gains has to carry that forward (it can always raise a TypeError if
> necessary).

No, but what if some code gets pathnames from whatever other places
and passes them on to os.scandir. Whenever it happens to get a bytes
path, a TypeError gets raised, but only when it picks one of the
DirEntry objects and for instance tries to open(...) it. Of course,
I'm not sure how common this is.

> It really depends on how we choose to structure the
> function in terms of just doing the right thing for objects that follow the
> protocol or if we want to introduce some required structure for the
> resulting path and implement some type guarantees so you have a better idea
> of what you will be working with after calling the function.

Do you have an example of potential 'required structure'?

>> Then the options are either to return Union[str, bytes] or to
>> always return str. And if the latter does not cause any problems, I
>> like it way better, and it seems others would do too.
> You don't have to convert byte paths to str, you can simply raise an
> exception in the face of them.

I thought the point was for existing APIs to start supporting path
objects, wouldn't raising an exception break the API?


More information about the Python-Dev mailing list