-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Keep trivial instances and aliases during expansion #19543
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
This comment has been minimized.
This comment has been minimized.
The new error in
I will try to come up with a small repro, and look in more details. |
It has something to do with omitted generics ( Both on master and here: import numpy as np
from typing import TypeGuard
def is_smth(t: object) -> TypeGuard[int | np.int8]: ...
def check(a: int, b: int) -> int:
if not is_smth(a): assert False
if a < 0: a += b # E: Incompatible types in assignment (expression has type "int | signedinteger[_8Bit]", variable has type "int")
if not 0 <=a <= b: assert False
return a # E: Incompatible return value type (got "int | signedinteger[_8Bit]", expected "int") But replacing import numpy as np
from typing import TypeGuard
def is_smth(t: object) -> TypeGuard[int | np.integer]: ...
def check(a: int, b: int) -> int:
if not is_smth(a): assert False
if a < 0: a += b
if not 0 <=a <= b: assert False
return a # E: Incompatible return value type (got "Any | int | integer[Any]", expected "int") |
Nice, thanks for the pointer! |
OK, I figured it out. So two things here:
|
This comment has been minimized.
This comment has been minimized.
Hm, |
Diff from mypy_primer, showing the effect of this PR on open source code: pandas (https://github.com/pandas-dev/pandas)
+ pandas/io/parsers/python_parser.py:1552: error: Unused "type: ignore" comment [unused-ignore]
- pandas/core/series.py:2839: error: Argument 2 to "diff" has incompatible type "float | int | integer[Any]"; expected "int" [arg-type]
+ pandas/core/series.py:2839: error: Argument 2 to "diff" has incompatible type "float | int"; expected "int" [arg-type]
+ pandas/core/frame.py:5897: error: Redundant cast to "int" [redundant-cast]
+ pandas/core/groupby/groupby.py:5196: error: Redundant cast to "int" [redundant-cast]
mypy (https://github.com/python/mypy)
+ mypy/types.py:3217: error: Unused "type: ignore" comment [unused-ignore]
+ mypy/types.py:3242: error: Unused "type: ignore" comment [unused-ignore]
+ mypy/meet.py:119: error: Unused "type: ignore" comment [unused-ignore]
+ mypy/meet.py:311: error: Unused "type: ignore" comment [unused-ignore]
|
OK, I like the current version the most. Although it is not identical to the original semantics, it is IMO more consistent (and again, looking few changes in the primer, this is what people expect). |
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.
Now I like the primer results! The change itself LG, only copying explicitly if we need that is way more principled than current "hope it was already a copy" approach. There are likely other places assuming copies, but we'll find them eventually. Let's also wait for the mypy-issues check to see if there are any relevant regressions
Okay, nothing interesting. There's one changed output for the snippet in this comment: With |
Yeah, this is because |
This weirdly looking change consistently shows 1% performance improvement on my machine (Python 3.12, compiled). The key here is to not create new objects for trivial instances and aliases (i.e. those with no
.args
). The problem however is that some callers modify expanded type aliases in place (for better error locations). I updated couple places discovered by tests, but there may be more.I think we should go ahead, and then fix bugs exposed by this using following strategy:
semanal.py
).t.copy_modified()
followed byt.set_line()
manually.