Closed as not planned
Description
I get this strange error whereby adding a return annotation causes mypy to fail to identify the code after the "isinstance" block.
Mypy gives an error on the last line of ErrorIfReturnIsAnnotated.foo() while it doesn't give any errors for WorksIfNoReturnAnnotation.foo()
The only difference in those two classes are that one of them declares an explicit None return annotation.
Environment
- Mypy version used: 0.950 and 0.960
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini
(and other config files): None - Python version used: Python 3.10.4
- Operating system and version: Windows 10 (Version 21H2)
Full simplified test code to reproduce the bug
from abc import ABCMeta
from typing import Generic, TypeVar, Union
class BaseEngine(metaclass=ABCMeta):
pass
class BaseEngineVariantA(BaseEngine):
pass
class BaseEngineVariantB(BaseEngine):
pass
class VariantAConcreteEngine(BaseEngineVariantA):
pass
T = TypeVar('T', bound=Union[BaseEngineVariantA, BaseEngineVariantB])
class IBase(Generic[T], metaclass=ABCMeta):
def __init__(self, engine: T):
self.engine = engine
class IAction(IBase[T], metaclass=ABCMeta):
pass
class ErrorIfReturnIsAnnotated(IBase[T]):
def __init__(self, something: T, actions_injected: type[IAction[T]]) -> None:
self.something = something
self.actions_injected = actions_injected
def foo(self) -> None:
if not isinstance(self.something, VariantAConcreteEngine):
raise Exception("HI")
self.actions_injected(self.something) # Gives a mypy error of - error: Argument 1 to "IAction" has incompatible type "VariantAConcreteEngine"; expected "T"
class WorksIfNoReturnAnnotation(IBase[T]):
def __init__(self, something: T, actions_injected: type[IAction[T]]) -> None:
self.something = something
self.actions_injected = actions_injected
def foo(self):
if not isinstance(self.something, VariantAConcreteEngine):
raise Exception("HI")
self.actions_injected(self.something) # No errors