-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Replace overloads in os.path
stubs with Unions of TypeVars
#9474
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that a) typeshed CI currently runs with mypy 0.991, b) mypy doesn't support LiteralString (it currently mostly just treats it like str), so PRs like this will need careful testing with pyright
Fails in pyright. I'll take a look tomorrow on how to setup a pyright environment for me to test locally. |
This comment has been minimized.
This comment has been minimized.
Support for this has been added in a recently merged mypy PR and this commit makes full use of it, getting rid of all overloads in the `os.path` stubs. Introduces a new TypeAlias `GenericOrLiteralPath` and replaces functions of the following styles: ```py @overload def a(path: PathLike[AnyStr]) -> AnyStr: ... @overload def a(path: AnyStr) -> AnyStr: ... @overload def b(path: PathLike[AnyStr]) -> AnyStr: ... @overload def b(path: AnyOrLiteralStr) -> AnyOrLiteralStr: ... ``` with: ```py def a(path: GenericPath[AnyStr]) -> AnyStr: ... def b(path: GenericPath[AnyOrLiteralStr]) -> AnyOrLiteralStr: ... ```
96f7c40
to
ca1a9d6
Compare
Problems have been addressed. What remains to be tested is whether a
|
Diff from mypy_primer, showing the effect of this PR on open source code: vision (https://github.com/pytorch/vision)
+ torchvision/prototype/datasets/_home.py:10: error: Returning Any from function declared to return "str" [no-any-return]
+ torchvision/prototype/datasets/_home.py:16: error: Returning Any from function declared to return "str" [no-any-return]
werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/utils.py:436: error: Value of type variable "AnyOrLiteralStr" of "join" cannot be "Union[str, PathLike[Any], Any]" [type-var]
+ src/werkzeug/utils.py:436: error: Incompatible types in assignment (expression has type "Union[str, PathLike[Any], Any]", variable has type "Optional[str]") [assignment]
+ src/werkzeug/utils.py:438: error: Value of type variable "AnyOrLiteralStr" of "abspath" cannot be "Union[str, PathLike[Any], Any]" [type-var]
+ src/werkzeug/utils.py:438: error: Incompatible types in assignment (expression has type "Union[str, PathLike[Any], Any]", variable has type "Optional[str]") [assignment]
+ src/werkzeug/utils.py:440: error: Argument 1 to "stat" has incompatible type "Optional[str]"; expected "Union[int, Union[str, bytes, PathLike[str], PathLike[bytes]]]" [arg-type]
+ src/werkzeug/utils.py:585: error: Argument 1 to "isfile" has incompatible type "Optional[str]"; expected "Union[Union[str, bytes, PathLike[str], PathLike[bytes]], int]" [arg-type]
+ src/werkzeug/utils.py:591: error: Argument 1 to "send_file" has incompatible type "Optional[str]"; expected "Union[PathLike[Any], str, IO[bytes]]" [arg-type]
streamlit (https://github.com/streamlit/streamlit)
+ lib/streamlit/file_util.py: note: In function "get_streamlit_file_path":
+ lib/streamlit/file_util.py:137:5: error: Returning Any from function declared to return "str" [no-any-return]
jinja (https://github.com/pallets/jinja)
+ src/jinja2/environment.py:857: error: Value of type variable "AnyOrLiteralStr" of "join" cannot be "Union[str, PathLike[Any], Any]" [type-var]
|
Primer results:
|
The streamlit and pytorch changes are definitely not unrelated: they're the direct result of changes made by this PR. What seems to be going on is that mypy now infers Any for cases where it previously inferred a non-Any type, which means that this PR would cause a regression. |
Right, it appears to be the same issue as with werkzeug where the typevar is infered to Unfortunately I have no idea or time to figure out how to proceed with this, for the moment at least. Edit: Feel free to close the PR if you deem it not useful or want to clean up stale PRs in the future. |
os.path
stubs with Unions of TypeVarsos.path
stubs with Unions of TypeVars
os.path
stubs with Unions of TypeVarsos.path
stubs with Unions of TypeVars
This has a lot of merge conflicts now, and it doesn't look like maintainers have been given permission to push to the PR branch, so I can't resolve them. As such, I'm closing this as stale for now -- but feel free to open a new PR if you'd like to fix the conflicts and see what |
I suspect that Github doesn't support that when creating pull requests from organizations as opposed to your personal account (because I use an organization for management of my forks). |
Support for this has been added in a recently merged mypy PR (python/mypy#14396) and this commit makes full use of it, getting rid of all overloads in the
os.path
stubs.Continues the efforts of #2053 (and the previous attempts referenced there).
Introduces a new TypeAlias
GenericOrLiteralPath
and replaces functions of the following styles:with:
Note that quite unexpectedly running
mypy_test.py
does not result in a failure on the latest stable locally and I don't know why (at least not at this time of day), so running against CI to find out more.Related: python/mypy#12554