diff --git a/mypy/subtypes.py b/mypy/subtypes.py index 107a5abbebab..81726b1f9884 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -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 diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index 0c0865c0540e..30d33b917123 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -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[] = ...") + + 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]