Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PathLike to typing #402

Closed
ilevkivskyi opened this issue Mar 21, 2017 · 8 comments
Closed

Add PathLike to typing #402

ilevkivskyi opened this issue Mar 21, 2017 · 8 comments

Comments

@ilevkivskyi
Copy link
Member

While thinking about protocols, it came to my mind that we could add PathLike to typing, also we could make it a (runtime) protocol class. As I understand PEP 519, it could be typed like this:

S = TypeVar('S', bound=Union[str, bytes])
class PathLike(Generic[S], extra=os.PathLike): # or Protocol[S] in future
    def __fspath__(self) -> S:
        ...

So that there will be three allowed types: PathLike[str], PathLike[bytes], and PathLike[Union[str, bytes]]. Or we actually don't need PathLike[Union[str, bytes]]? In this case we could just use AnyStr instead of S.

CC @brettcannon

@JelleZijlstra
Copy link
Member

FYI, PEP 519 explicitly rejects adding typing.PathLike (https://www.python.org/dev/peps/pep-0519/#provide-specific-type-hinting-support). But maybe the situation will change once we have protocols in the language.

I've also been thinking about this recently. In addition to PathLike, it would be nice to have a generic alias like typing.FSPath[AnyStr] so you don't have to write something like Union[AnyStr, PathLike[AnyStr]].

@brettcannon
Copy link
Member

There's already https://docs.python.org/3/library/os.html#os.PathLike so it's part way there already.

And if/when protocols come into Python then there will be a discussion as to whether we should back-fill the stdlib with protocols and I suspect PathLike would be one of them.

@gvanrossum
Copy link
Member

gvanrossum commented Mar 21, 2017 via email

@ilevkivskyi
Copy link
Member Author

What else is there to do?

Two things:

  • We could make it an actual generic, so that one can write path: PathLike[str]
  • In the (not so distant) future, we could make it a protocol class.

@gvanrossum
Copy link
Member

We could make it an actual generic, so that one can write path: PathLike[str]

Hm... So for a PathLike[str] the __fspath__() method returns str, and so on. This made me look more carefully at the code that I moved around, and I now realize that it returns AnyStr without taking any arguments using that type variable (nor is the class generic). That seems odd, and also it appears mypy arbitrarily picks str as the actual return type.

I think I know a fix and I'll send it to you.

@srittau
Copy link
Collaborator

srittau commented Apr 23, 2021

It seems os.PathLike now works as suggested here.

@srittau srittau closed this as completed Apr 23, 2021
@izzues
Copy link

izzues commented Sep 12, 2022

So, what is the correct way to do it now?

@Gobot1234
Copy link
Contributor

os.PathLike[str] or os.PathLike[bytes] as described in the original proposal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants