Replace overloads in os.path stubs with Unions of TypeVars#9474
Replace overloads in os.path stubs with Unions of TypeVars#9474FichteFoll wants to merge 1 commit intopython:mainfrom
os.path stubs with Unions of TypeVars#9474Conversation
|
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.pathstubs.Continues the efforts of #2053 (and the previous attempts referenced there).
Introduces a new TypeAlias
GenericOrLiteralPathand replaces functions of the following styles:with:
Note that quite unexpectedly running
mypy_test.pydoes 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