On Wed, Dec 1, 2021 at 10:50 PM Christopher Barker <python...@gmail.com>
wrote:

> I know this isn't really the place for this conversation, but:
>
>
>> which is what `os.PathLike` represents, hence why `str` isn't covered by
>> it);
>>
>
> wait, what? It seems so clear to me that "PathLike" (as a type specifier)
> would mean: anything that can be passed into os.fspath to give me a path.
> (or, of course to the stdlib functions that take paths)
>
> Isn't the entire purpose of os.fspath that you can write code like:
>
> def fun(some_kind_of_path):
>    some_kind_of_path = os.fspath(some_kind_of_path)
>
>    (or just pass it to a function you takes PathLIke)
>
> and go on your merry way -- e.g. duck typing, baby!
>
> Is there really no way to annotate that simply now?
>

Assuming you want the return value of 'fun' to be covariant with the path
input, I believe you would say this:

def fun(some_kind_of_path: str) -> str: ...
def fun(some_kind_of_path: bytes) -> bytes: ...
def fun(some_kind_of_path: os.PathLike[AnyStr]) -> AnyStr:
    some_kind_of_path = os.fspath(some_kind_of_path)
    # transform it
    return some_kind_of_path

I would love to be shown how to do this with just a one-line declaration of
'fun', but I've given up trying to figure it out.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/PI6TR73ZQRDMKDNBUH3AQOK4IYUERB7F/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to