Skip to content

Commit 1bed354

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

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

mypy/subtypes.py

+4
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

+38
Original file line numberDiff line numberDiff line change
@@ -2528,3 +2528,41 @@ class EmptyProto(Protocol): ...
25282528
def hh(h: EmptyProto) -> None: pass
25292529
hh(None)
25302530
[builtins fixtures/tuple.pyi]
2531+
2532+
2533+
[case testPartialTypeProtocol]
2534+
from typing import Protocol
2535+
2536+
class Flapper(Protocol):
2537+
def flap(self) -> int: ...
2538+
2539+
class Blooper:
2540+
flap = None
2541+
2542+
def bloop(self, x: Flapper) -> None:
2543+
reveal_type([self, x]) # N: Revealed type is 'builtins.list[builtins.object*]'
2544+
2545+
class Gleemer:
2546+
flap = [] # E: Need type annotation for 'flap' (hint: "flap: List[<type>] = ...")
2547+
2548+
def gleem(self, x: Flapper) -> None:
2549+
reveal_type([self, x]) # N: Revealed type is 'builtins.list[builtins.object*]'
2550+
[builtins fixtures/tuple.pyi]
2551+
2552+
2553+
[case testPartialTypeProtocolHashable]
2554+
# flags: --no-strict-optional
2555+
from typing import Protocol
2556+
2557+
class Hashable(Protocol):
2558+
def __hash__(self) -> int: ...
2559+
2560+
class ObjectHashable:
2561+
def __hash__(self) -> int: ...
2562+
2563+
class DataArray(ObjectHashable):
2564+
__hash__ = None
2565+
2566+
def f(self, x: Hashable) -> None:
2567+
reveal_type([self, x]) # N: Revealed type is 'builtins.list[builtins.object*]'
2568+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)