Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2k
Remove unnecessary overloads from re#6762
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
Uh oh!
There was an error while loading. Please reload this page.
This comment has been minimized.
This comment has been minimized.
srittau commented Dec 30, 2021
The primer output looks a bit concerning. |
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Alex Waygood <[email protected]>
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: pandas (https://github.com/pandas-dev/pandas) + pandas/core/strings/object_array.py:216: error: Value of type variable "AnyStr" of "compile" cannot be "Union[str, Pattern[Any], Any]" [type-var]+ pandas/core/array_algos/replace.py:88: error: Value of type variable "AnyStr" of "search" cannot be "Union[str, Pattern[Any], Any]" [type-var] sphinx (https://github.com/sphinx-doc/sphinx) + sphinx/util/docstrings.py:20:22: error: Value of type variable "AnyStr" of "compile" cannot be "Union[str, Pattern[Any], Any]"+ sphinx/util/docstrings.py: note: In function "separate_metadata":+ sphinx/util/docstrings.py:39:30: error: Value of type "Union[str, Pattern[Any], Any]" is not indexable+ sphinx/testing/util.py: note: In function "assert_re_search":+ sphinx/testing/util.py:34:12: error: Value of type variable "AnyStr" of "search" cannot be "Union[str, Pattern[Any], Any]"+ sphinx/testing/util.py: note: In function "assert_not_re_search":+ sphinx/testing/util.py:39:8: error: Value of type variable "AnyStr" of "search" cannot be "Union[str, Pattern[Any], Any]" |
AlexWaygood commented Dec 30, 2021
Well, I can't say I really understand why a union-type causes mypy to produce these false-positives when overloads don't. It doesn't make much sense to me. |
hauntsaninja commented Dec 30, 2021 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
AnyStr is a type variable, so mypy might be struggling to solve |
hauntsaninja commented Dec 30, 2021 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Came back to this after eating a lunch, I'm now fairly sure this is a mypy bug, but I'd still be curious to see if other type checkers do the right thing. Looking at the output, my guess is that mypy isn't taking value restrictions into account when solving constraints and only using value restriction as a check later. That is, |
jpy-git commented Dec 30, 2021
What I find a bit weird about these mypy_primer errors is that the changes in this PR are basically the same as what we added for If we take this error from the mypy_primer output: |
hauntsaninja commented Dec 31, 2021 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
That's probably related to your setup, e.g. looks like you might be missing docutils-stubs (note, different from types-docutils) or custom typeshed dir. I can repro with your test.py just fine. |
jpy-git commented Dec 31, 2021 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Found this comment that seems to be referring to it being a known issue with mypy not being able to resolve Wasn't able to track down a corresponding issue in mypy though |
hauntsaninja commented Jan 1, 2022
I opened python/mypy#11880 to track this. As I mentioned, the issue seems clearly related to value restriction |
srittau commented Jan 2, 2022
I marked this as "deferred" for now, until the mypy problems are sorted out. PR seems good otherwise. |
The semantics of this proposed PR should be identical to the existing stub. I'm curious, though, to see whether primer shows false-positives like those we saw in python#6762
JelleZijlstra commented Feb 12, 2022
I'm not hopeful that the mypy bug will be resolved any time soon. It doesn't seem like this PR fixes any concrete problem, so can we just close it? |
This PR follows on from the discussion here where @AlexWaygood highlighted that overloads are unnecessary when a function has the same return type and therefore simple union-types should be used instead. This PR combines these unnecessary overloads in
re.pyiand forPatternintyping.pyi.Additionally I spotted that

Pattern.splitshould have a return type oflist[AnyStr | Any]rather thanlist[AnyStr]:(
re.splitis already correct)