python / mypy

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

`unreachable` false positive when using `isinstance` on class with `__call__` when function parameter uses `ParamSpec` #16024

Open DetachHead opened 1 year ago

DetachHead commented 1 year ago
from typing import Callable, ParamSpec

P = ParamSpec("P")

class Foo:
    def __call__(self) -> None:
        ...

def foo(fn: Callable[P, None]) -> None:
    if isinstance(fn, Foo):
        print("reached") # error: Statement is unreachable  [unreachable]

foo(Foo())

playground

in this example, that line is always reached at runtime

DetachHead commented 1 year ago

minified further:

from typing import Callable

def foo(fn: Callable[[], object]) -> None:
    if isinstance(fn, type):
        print(fn)