-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Narrow TypeVar to bounded TypeVar #15377
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
Narrow TypeVar to bounded TypeVar #15377
Conversation
This comment has been minimized.
This comment has been minimized.
I think I've broken mypy primer, will fix |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This is a great improvement! There are still a few things that don't work, like this one: # flags: --warn-unreachable
from typing import Optional, TypeVar
T = TypeVar("T", bound=Optional[int]) # `Union[None, int]` gives the same result
def foo(v: T) -> T:
if v is None:
return v # E: Statement is unreachable [unreachable]
return v but that's maybe a different problem. |
Might be nice to somehow indicate the updated upper bound in the repr |
I've added |
Surprisingly all mypy_primer results are expected :) I'm making this do slightly more than originally planned by changing how narrowed TypeVars are stringified. |
f195e01
to
61b71e9
Compare
This comment has been minimized.
This comment has been minimized.
61b71e9
to
b84e3f8
Compare
This comment has been minimized.
This comment has been minimized.
@JelleZijlstra This implements the first option I've outlined in #15151 (comment), can take a look? |
I think I'll pass on this one, sorry. I'm mostly focusing my open-source time on projects other than mypy. |
@ilevkivskyi What do you think? |
Diff from mypy_primer, showing the effect of this PR on open source code: mitmproxy (https://github.com/mitmproxy/mitmproxy)
+ mitmproxy/coretypes/serializable.py:186: error: Unused "type: ignore" comment [unused-ignore]
steam.py (https://github.com/Gobot1234/steam.py)
- steam/ext/commands/commands.py:287: error: Argument 1 to "append" of "list" has incompatible type "Command[Any, Any, Any]"; expected "Self" [arg-type]
- steam/ext/commands/commands.py:940: error: Incompatible return value type (got "Command[Any, Any, Any] | MaybeCommandT", expected "MaybeBool | MaybeCommandT") [return-value]
mypy (https://github.com/python/mypy)
+ mypy/typeops.py:342: error: Incompatible types in assignment (expression has type "CallableType", variable has type "F") [assignment]
pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
+ tests/__init__.py:30: error: Unused "type: ignore" comment [unused-ignore]
+ tests/__init__.py:44: error: Unused "type: ignore" comment [unused-ignore]
|
I propose to close this in favor of #19183 that offers a different approach. |
Fixes #15151.
When
isinstance
narrows a TypeVar, it should narrow it to a bounded TypeVar rather than to an Instance, so it could keep functioning as that TypeVar.