-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix handling of NoReturn in Union #11996
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
Fix handling of NoReturn in Union #11996
Conversation
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Nikita Sobolev <[email protected]>
Diff from mypy_primer, showing the effect of this PR on open source code: steam.py (https://github.com/Gobot1234/steam.py)
- steam/state.py:842: error: Incompatible types in assignment (expression has type "Optional[User]", variable has type "CMsgClientFriendsListFriend") [assignment]
+ steam/state.py:842: error: Incompatible types in assignment (expression has type "User", variable has type "CMsgClientFriendsListFriend") [assignment]
spark (https://github.com/apache/spark)
+ python/pyspark/pandas/series.py:4246: error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior [str-bytes-safe]
pip (https://github.com/pypa/pip)
- src/pip/_internal/cli/progress_bars.py:99: error: Incompatible types in assignment (expression has type "Callable[[int, FrameType], None]", variable has type "Union[Callable[[int, Optional[FrameType]], Any], int, Handlers, None]")
+ src/pip/_internal/cli/progress_bars.py:99: error: Incompatible types in assignment (expression has type "Callable[[int, FrameType], None]", variable has type "Union[Callable[[int, Optional[FrameType]], Any], int, None]")
|
@kreathon can you please analyze primer output? Some types have changed. Is it right? |
@sobolevn Yes, it seems that something changed. But the changes are not obvious to me. I am going to have a closer look. |
mypy_primer Analysissteam.py
The project uses Code: def get_user(self, id64: int) -> User | None:
return self._users.get(id64)
...
friend = self.get_user(steam_id.id64)
spark
Simplified version of the code:
On the new implementation (where
On current master we get:
I think the new behavior is actually correct, because If the For a nested I could try to investigate this on another branch, but I am not sure if we can get much out of it, since pip
|
This looks like a separate bug, do you want to work on it after this one? 🙂 |
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.
LGTM! Thank you! Great analysis!
Going to wait for some else to double check and merge 🙂
Yes, I can do that. I am going to open a new issue as soon as I have collected more information. |
For consistency, the constructor of from typing import Union, NoReturn
x: Union[str, NoReturn]
x + 's' # Unsupported left operand type for + ("NoReturn") We could perhaps do this in (Additional rationale: I think @kreathon What do you think? Would you like to try to work on the general case? We can also just create a follow-up issue where this can be discussed. |
There are several discussions and comments describing the following problem (references can be found at the end of the PR summary): ```python def func() -> str | NoReturn: ... func().lower() ``` At the moment the code results in: `"NoReturn" of "Union[str, NoReturn]" has no attribute "lower"` Make `Union[int, NoReturn]` equivalent to `int` in a return type, because in case the function returns it must be `int`.
Description
There are several discussions and comments describing the following problem (references can be found at the end of this post):
At the moment the code results in:
"NoReturn" of "Union[str, NoReturn]" has no attribute "lower"
Union[int, NoReturn]
should be equivalent toint
, because in case the function returns it must beint
.Implementation
For every call expression apply
make_simplified_union
, which is already capable of transformingUnion[int, NoReturn]
toint
.Test Plan
The following test case was added to verify the implementation is working as intended:
References
Union[X, NoReturn]
isn't properly narrowed toX
#11876