[Python-Dev] Defining a path protocol

Ethan Furman ethan at stoneleaf.us
Wed Apr 6 14:54:08 EDT 2016


On 04/06/2016 11:32 AM, Brett Cannon wrote:
> On Wed, 6 Apr 2016 at 11:06 Ethan Furman wrote:
>> On 04/06/2016 10:26 AM, Brett Cannon wrote:

>>> Now we need clear details. :) Some open questions are:
>>>
>>>  1. Name: __path__, __fspath__, or something else?
>>
>> __fspath__
>
> +1 for __path__, +0 for __fspath__ (I don't know how widespread the
> notion that "fs" means "file system" is).

Maybe __os_path__ then?  I would rather be explicit about the type of 
path we are dealing with -- who knows if we won't have __url_path__ in 
the future (besides Guido, of course ;)


>    def fspath(path):
>        try:
>            return path.__path__()
>        except AttributeError:
>            if isinstance(path, str):
>                return path
>        raise TypeError  # Or NotImplementedError?
>
> Or you can drop the isinstance() check and [...]

If the purpose of fspath() is to return a usable path-as-string then we 
should raise if unable to do it.

> If we add str.__fspath__ then the function becomes:
>
>    def fspath(path):
>        return path.__fspath__()
>
> Which might be too simplistic for a built-in, but that also means adding
> it on str would potentially negate the need for a built-in.

That is an attractive option.

--
~Ethan~


More information about the Python-Dev mailing list