Open theemathas opened 1 year ago
When the concrete parameter is None
, an error message is shown and becomes unreachable.
from typing import Generic, TypeVar
T = TypeVar("T")
class Base(Generic[T]): ...
class Sub1(Base[None]): ...
class Sub2(Base[int]): ...
def f(base: Base[T]) -> None:
if isinstance(base, Sub1): # Subclass of "Base[T]" and "Sub1" cannot exist: would have inconsistent method resolution order [unreachable]
reveal_type(self) # unreachable
if isinstance(self, Sub2):
reveal_type(self) # Never
Bug Report
If I have a generic parent class, and I have a child class inheriting from a concrete version of the parent class, then I cannot type-narrow from the generic parent class to the child class. Trying to do the narrowing unexpectedly produces
Never
.In my original code (not included here), this type-narrowing is after business logic that ensures that the object is an instance of the child class, and therefore the generic type is of the correct type. Additionally, this narrowing was done on
self
in a method in the parent class, so usingParent[Any]
orParent[int]
isn't an option, since that requires changing the type ofself
.Possibly related to #14785
To Reproduce
Run mypy on the following code. Or use this playground link.
Expected Behavior
The revealed type should be
Child
.Actual Behavior
Your Environment
--warn-unreachable
option produces the same output.