Skip to content

Commit

Permalink
Fix partial type crash during protocol checking (#9495)
Browse files Browse the repository at this point in the history
In particular, this affected hashables. Fixes #9437

Co-authored-by: hauntsaninja <>
  • Loading branch information
hauntsaninja committed Sep 29, 2020
1 parent 8bf770d commit 9ab622c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mypy/subtypes.py
Expand Up @@ -543,6 +543,10 @@ def f(self) -> A: ...
# print(member, 'of', right, 'has type', supertype)
if not subtype:
return False
if isinstance(subtype, PartialType):
subtype = NoneType() if subtype.type is None else Instance(
subtype.type, [AnyType(TypeOfAny.unannotated)] * len(subtype.type.type_vars)
)
if not proper_subtype:
# Nominal check currently ignores arg names
# NOTE: If we ever change this, be sure to also change the call to
Expand Down
38 changes: 38 additions & 0 deletions test-data/unit/check-protocols.test
Expand Up @@ -2536,3 +2536,41 @@ class EmptyProto(Protocol): ...
def hh(h: EmptyProto) -> None: pass
hh(None)
[builtins fixtures/tuple.pyi]


[case testPartialTypeProtocol]
from typing import Protocol

class Flapper(Protocol):
def flap(self) -> int: ...

class Blooper:
flap = None

def bloop(self, x: Flapper) -> None:
reveal_type([self, x]) # N: Revealed type is 'builtins.list[builtins.object*]'

class Gleemer:
flap = [] # E: Need type annotation for 'flap' (hint: "flap: List[<type>] = ...")

def gleem(self, x: Flapper) -> None:
reveal_type([self, x]) # N: Revealed type is 'builtins.list[builtins.object*]'
[builtins fixtures/tuple.pyi]


[case testPartialTypeProtocolHashable]
# flags: --no-strict-optional
from typing import Protocol

class Hashable(Protocol):
def __hash__(self) -> int: ...

class ObjectHashable:
def __hash__(self) -> int: ...

class DataArray(ObjectHashable):
__hash__ = None

def f(self, x: Hashable) -> None:
reveal_type([self, x]) # N: Revealed type is 'builtins.list[builtins.object*]'
[builtins fixtures/tuple.pyi]

0 comments on commit 9ab622c

Please sign in to comment.