Skip to content

Add a _typeshed.pyi file and a PathLike alias #4161

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

Merged
merged 13 commits into from
Jun 4, 2020

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented Jun 3, 2020

This PR only replaces a few potential places where this alias could be
used.

Closes: #4131

This PR only replaces a few potential places where this alias could be
used.

Closes: python#4131

# StrPath and AnyPath can be used in places where a
# path can be used instead of a string, starting with Python 3.6.
if sys.version_info >= (3, 6):
from os import PathLike
StrPath = Union[str, PathLike[str]]
AnyPath = Union[str, bytes, PathLike[AnyStr]]
AnyPath = Union[str, bytes, PathLike[str], PathLike[bytes]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name AnyPath is mildly confusing because AnyStr is a typevar and this similarly named alias isn't. Not sure what to call it instead though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, Any is not a typevar either. We could go with just Path, but that is confusing with pathlib.Path. Maybe EitherPath, but I don't find that particularly clear. (I'm not suggesting WhateverPath, although it's kind of fitting. :) )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know at least the path modules (ntpath, posixpath, os.path, etc) also defines a BytesPath, is that worth moving here now, and updating those files?
For naming, I'm personally okay with AnyPath, as I can't come up with a clearer name

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added BytesPath for consistency.

@CraftSpider
Copy link
Contributor

For future improvements, what might be considered for _typeshed? Should we re-export Literal, to avoid the if sys.version_info checks in every file, and should we define some of the common TypeVars here? I'm kind of for the first one, as it makes using version-gated things slightly easier, and requires less effort to change them in the future (Single point of change, etc)

@JelleZijlstra
Copy link
Member

For Literal, I don't understand why we can't just do from typing_extensions import Literal unconditionally. At runtime you might not want that because it means you have to install typing-extensions as a library, but in typeshed that's not an issue.

Other things we could add here are common protocols, like SupportsLessThan from #4155

Not sure about typevars, at least for simple ones—they're usually pretty easy to define, and because using typevars is tricky, it's good to have them close to the definition sites.

Also, should wsgiref.types be moved in here? Perhaps we can make _typeshed a package and move it to _typeshed/wsgiref.pyi.

@srittau
Copy link
Collaborator Author

srittau commented Jun 3, 2020

For Literal, I don't understand why we can't just do from typing_extensions import Literal unconditionally.

Sounds fine to me.

Other things we could add here are common protocols, like SupportsLessThan from #4155

+1, although this can be done in a separate PR.

Not sure about typevars, at least for simple ones—they're usually pretty easy to define, and because using typevars is tricky, it's good to have them close to the definition sites.

Agreed.

Also, should wsgiref.types be moved in here? Perhaps we can make _typeshed a package and move it to _typeshed/wsgiref.pyi.

Good call. I will make it into a package.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I also have no good suggestions for AnyPath :-)

@sproshev
Copy link
Contributor

sproshev commented Jun 3, 2020

I'd suggest to move open function modes from io to _typeshed

@srittau srittau merged commit eea9be6 into python:master Jun 4, 2020
vishalkuo pushed a commit to vishalkuo/typeshed that referenced this pull request Jun 26, 2020
@srittau srittau deleted the typeshed-module branch July 30, 2020 16:48
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

Successfully merging this pull request may close these issues.

typeshed types module
5 participants