-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
bugmypy got something wrongmypy got something wrong
Description
Bug Report
MyPy doesn't recognize that isinstance(type_var, type) and isinstance(another_var, type_var) narrows another_var to whatever type_var is.
To Reproduce
class RemoteFunction(Generic[_T]):
def __init__(self, return_type: type[_T] | Callable[[Any], _T]):
self._return_type = return_type
def _validate(self, response: Any) -> _T:
if isinstance(self._return_type, type):
reveal_type(self._return_type)
# Revealed type is "builtins.type" Mypy
# Type of "self._return_type" is "type[object] | type" Pylance
if not isinstance(response, self._return_type):
raise TypeError
reveal_type(response)
# Revealed type is "builtins.object"Mypy
# Type of "response" is "Any" Pylance
return response # Incompatible return value type (got "object", expected "_T")
return self._return_type(response)
def __call__(self) -> _T:
remote_response: Any = None # actually from network/db/etc
return self._validate(remote_response)
# Usage example
my_remote_func = RemoteFunction(int)
response = my_remote_func()
reveal_type(response)
# Type of "response" is "int" Pylance
# Revealed type is "builtins.int" MypyExpected Behavior
mypy should correctly type response as _T
Actual Behavior
test333.py: note: In member "_validate" of class "RemoteFunction":
test333.py:20: note: Revealed type is "builtins.type"
test333.py:25: note: Revealed type is "builtins.object"
test333.py:28: error: Incompatible return value type (got "object", expected "_T") [return-value]
return response # Incompatible return value type (got "object", expected "_T")
Interestingly, pyright shows similar types for reveal_type, as annotated above, but does not give an error. Both correctly infer the type of the response in the outer scope, presumably because of the __call__ return type annotation.
Your Environment
- Mypy version used: 1.19.0
- Mypy command-line flags: --strict
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.11.4
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrong