Uh oh!
There was an error while loading. Please reload this page.
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.
tmke8
commented
Jun 6, 2023
This is a great improvement! There are still a few things that don't work, like this one: # flags: --warn-unreachablefromtypingimportOptional, TypeVarT=TypeVar("T", bound=Optional[int]) # `Union[None, int]` gives the same resultdeffoo(v: T) ->T:
ifvisNone:
returnv# E: Statement is unreachable [unreachable]returnvbut that's maybe a different problem. |
ikonst
commented
Jun 6, 2023
Might be nice to somehow indicate the updated upper bound in the repr |
I've added |
ikonst
commented
Jun 6, 2023
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
61b71e9Compare
This comment has been minimized.
This comment has been minimized.
61b71e9 to
b84e3f8Compare
This comment has been minimized.
This comment has been minimized.
ikonst
commented
Jun 7, 2023
@JelleZijlstra This implements the first option I've outlined in #15151 (comment), can take a look? |
JelleZijlstra
commented
Jun 7, 2023
I think I'll pass on this one, sorry. I'm mostly focusing my open-source time on projects other than mypy. |
ikonst
commented
Jun 12, 2023
@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] |
ilevkivskyi
commented
May 31, 2025
I propose to close this in favor of #19183 that offers a different approach. |
Fixes#15151.
When
isinstancenarrows a TypeVar, it should narrow it to a bounded TypeVar rather than to an Instance, so it could keep functioning as that TypeVar.