Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative type narrowing fails for protocols with overloaded methods #12228

Closed
BvB93 opened this issue Feb 21, 2022 · 2 comments
Closed

Negative type narrowing fails for protocols with overloaded methods #12228

BvB93 opened this issue Feb 21, 2022 · 2 comments
Labels
bug mypy got something wrong topic-overloads topic-type-narrowing Conditional type narrowing / binder

Comments

@BvB93
Copy link
Contributor

BvB93 commented Feb 21, 2022

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
@BvB93 BvB93 added the bug mypy got something wrong label Feb 21, 2022
@JelleZijlstra JelleZijlstra added topic-overloads topic-type-narrowing Conditional type narrowing / binder labels Mar 19, 2022
@erictraut
Copy link

This bug was apparently fixed in mypy 0.981. Since then, it produces results consistent with the "expected behavior".

@hauntsaninja
Copy link
Collaborator

I think fixed by #13303

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-overloads topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

4 participants