-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Remove unnecessary overloads from re #6762
Conversation
This comment has been minimized.
This comment has been minimized.
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]"
|
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. |
AnyStr is a type variable, so mypy might be struggling to solve |
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, |
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:
|
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. |
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 |
I opened python/mypy#11880 to track this. As I mentioned, the issue seems clearly related to value restriction |
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
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.pyi
and forPattern
intyping.pyi
.Additionally I spotted that
Pattern.split
should have a return type oflist[AnyStr | Any]
rather thanlist[AnyStr]
:(
re.split
is already correct)