[Python] Why does mypy issubclass type narrowing only work on "type" instances?

Discussão em 'Python' iniciado por Stack, Agosto 26, 2025.

  1. Stack

    Stack Membro Participativo

    Code snippet:

    from typing import Any

    class MyClass:
    pass

    def f(o: Any) -> None:
    if isinstance(o, type) and issubclass(o, MyClass):
    reveal_type(o) # Revealed type is "Type[MyClass]"

    if issubclass(o, MyClass):
    reveal_type(o) # Revealed type is "Any"


    Is it a bug in mypy or is there a reason why the 2nd example is not expected to work?

    Continue reading...

Compartilhe esta Página