Skip to content

Commit 9ab622c

Browse files
authored
Fix partial type crash during protocol checking (#9495)
In particular, this affected hashables. Fixes #9437 Co-authored-by: hauntsaninja <>
1 parent 8bf770d commit 9ab622c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

mypy/subtypes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,10 @@ def f(self) -> A: ...
543543
# print(member, 'of', right, 'has type', supertype)
544544
if not subtype:
545545
return False
546+
if isinstance(subtype, PartialType):
547+
subtype = NoneType() if subtype.type is None else Instance(
548+
subtype.type, [AnyType(TypeOfAny.unannotated)] * len(subtype.type.type_vars)
549+
)
546550
if not proper_subtype:
547551
# Nominal check currently ignores arg names
548552
# NOTE: If we ever change this, be sure to also change the call to

test-data/unit/check-protocols.test

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,3 +2536,41 @@ class EmptyProto(Protocol): ...
25362536
def hh(h: EmptyProto) -> None: pass
25372537
hh(None)
25382538
[builtins fixtures/tuple.pyi]
2539+
2540+
2541+
[case testPartialTypeProtocol]
2542+
from typing import Protocol
2543+
2544+
class Flapper(Protocol):
2545+
def flap(self) -> int: ...
2546+
2547+
class Blooper:
2548+
flap = None
2549+
2550+
def bloop(self, x: Flapper) -> None:
2551+
reveal_type([self, x]) # N: Revealed type is 'builtins.list[builtins.object*]'
2552+
2553+
class Gleemer:
2554+
flap = [] # E: Need type annotation for 'flap' (hint: "flap: List[<type>] = ...")
2555+
2556+
def gleem(self, x: Flapper) -> None:
2557+
reveal_type([self, x]) # N: Revealed type is 'builtins.list[builtins.object*]'
2558+
[builtins fixtures/tuple.pyi]
2559+
2560+
2561+
[case testPartialTypeProtocolHashable]
2562+
# flags: --no-strict-optional
2563+
from typing import Protocol
2564+
2565+
class Hashable(Protocol):
2566+
def __hash__(self) -> int: ...
2567+
2568+
class ObjectHashable:
2569+
def __hash__(self) -> int: ...
2570+
2571+
class DataArray(ObjectHashable):
2572+
__hash__ = None
2573+
2574+
def f(self, x: Hashable) -> None:
2575+
reveal_type([self, x]) # N: Revealed type is 'builtins.list[builtins.object*]'
2576+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)