python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.5k stars 2.83k forks source link

Type-narrowing from parent to child class produces `Never` if inheriting from a concrete version of a generic parent class. #16494

Open theemathas opened 1 year ago

theemathas commented 1 year ago

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 using Parent[Any] or Parent[int] isn't an option, since that requires changing the type of self.

Possibly related to #14785

To Reproduce

Run mypy on the following code. Or use this playground link.

from typing import Generic, TypeVar, reveal_type
T = TypeVar("T")

class Parent(Generic[T]):
    pass
class Child(Parent[int]):
    pass

def foo(thing: Parent[T]) -> None:
    assert isinstance(thing, Child)
    reveal_type(thing)

Expected Behavior

The revealed type should be Child.

Actual Behavior

main.py:11: note: Revealed type is "Never"
Success: no issues found in 1 source file

Your Environment

KotlinIsland commented 11 months 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