You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from typing import TypeVar
T = TypeVar("T")
def _checkType(arg: T, typ: type) -> T:
if isinstance(arg, typ):
return arg
raise TypeError(f"Argument is not a {typ}.")
reveal_type(_checkType(42, int))
Expected Behavior
Type checks to pass
Actual Behavior
The following error is printed: error: Incompatible return value type (got "object", expected "T")
Additionally, these two variants work around the issue (and pass without error):
def _checkType_cast(arg: T, typ: type) -> T:
if isinstance(arg, typ):
return cast(T, arg)
raise TypeError(f"Argument is not a {typ}.")
def _checkType_backup(arg: T, typ: type) -> T:
backup = arg
if isinstance(arg, typ):
return backup
raise TypeError(f"Argument is not a {typ}.")
Your Environment
Mypy version used: 0.971
Python version used: 3.9.7
The text was updated successfully, but these errors were encountered:
Bug Report
Run mypy against the following:
Expected Behavior
Type checks to pass
Actual Behavior
The following error is printed:
error: Incompatible return value type (got "object", expected "T")
Additionally, these two variants work around the issue (and pass without error):
Your Environment
The text was updated successfully, but these errors were encountered: