Skip to content

Fix partial type crash during protocol checking #9495

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

Merged
merged 3 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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]
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy