Bug Report
Since the merging of #9904 it is now possible to use runtime-checkable protocols with overloaded methods. While type narrowing now happens successfully with isinstance/issubclass checks in the positive case, the same does not hold for the negative case, be it either via not isinstance/not issubclass or a an else statement.
To Reproduce
from typing import overload, Protocol, runtime_checkable
from collections.abc import Sequence
class Foo:
def func(self, a: int) -> float: ...
class FooOverload:
@overload
def func(self, a: int) -> float: ...
@overload
def func(self, a: str) -> Sequence[float]: ...
@runtime_checkable
class SupportsFunc(Protocol):
@overload
def func(self, a: int) -> float: ...
@overload
def func(self, a: str) -> Sequence[float]: ...
foo_union: FooOverload | Foo
Expected Behavior
if isinstance(foo_union, SupportsFunc):
reveal_type(foo_union) # N: Revealed type is '__main__.FooOverload'
else:
reveal_type(foo_union) # N: Revealed type is '__main__.Foo'
Observed Behavior
# NOTE: a `not isinstance` check also reveals a `Union`
if isinstance(foo_union, SupportsFunc):
reveal_type(foo_union) # N: Revealed type is '__main__.FooOverload'
else:
reveal_type(foo_union) # N: Revealed type is 'Union[__main__.FooOverload, __main__.Foo]'
Your Environment
- Mypy version used:
master branch (post-b22c4e4)
- Mypy command-line flags: n.a.
- Mypy configuration options from
mypy.ini (and other config files): n.a.
- Python version used: 3.10.2
- Operating system and version: Windows 10
Bug Report
Since the merging of #9904 it is now possible to use runtime-checkable protocols with overloaded methods. While type narrowing now happens successfully with
isinstance/issubclasschecks in the positive case, the same does not hold for the negative case, be it either vianot isinstance/not issubclassor a anelsestatement.To Reproduce
Expected Behavior
Observed Behavior
Your Environment
masterbranch (post-b22c4e4)mypy.ini(and other config files): n.a.