Skip to content
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

Fix partial type crash during protocol checking #9495

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
35 changes: 35 additions & 0 deletions test-data/unit/check-protocols.test
Expand Up @@ -2536,3 +2536,38 @@ 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: --strict-optional
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this? I thought it was the default. Or are the tests run multiple times with different default flags?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I meant to use --no-strict-optional as in #9437 (comment) — but a test a couple lines up used --strict-optional and I copied it by accident. Thanks!

Hmm. It actually looks like --no-strict-optional is the default in tests. I just discovered this file lurking in the shadows:

Strict optional is disabled be default

Also object in the test stubs doesn't have __hash__... so this test didn't actually test anything different from Blooper (and crashes regardless of --strict-optional). Fixed now (ie, the new test and old code only crashes with --no-strict-optional).

from typing import Protocol

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

class DataArray:
__hash__ = None

def f(self, x: Hashable) -> None:
reveal_type([self, x]) # N: Revealed type is 'builtins.list[builtins.object*]'
[builtins fixtures/tuple.pyi]
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved