Skip to content
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
8 changes: 8 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4470,6 +4470,14 @@ def infer_variable_type(
is_lvalue_final=name.is_final,
is_lvalue_member=isinstance(lvalue, MemberExpr),
)
and not (
# Trust None assignments to dunder methods
# This is a bit ad-hoc, but it improves protocol
# (non-)assignability, for instance `__hash__ = None`
self.scope.active_class()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add comment briefly explaining the special case. This could seem mysterious.

and is_dunder(name.name)
and isinstance(get_proper_type(init_type), NoneType)
)
and not self.no_partial_types
):
# We cannot use the type of the initialization expression for full type
Expand Down
20 changes: 19 additions & 1 deletion test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -2939,7 +2939,25 @@ class Gleemer:


[case testPartialTypeProtocolHashable]
# flags: --no-strict-optional
# flags: --no-strict-optional --no-local-partial-types
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]


[case testPartialTypeProtocolHashableLocalPartialTypes]
# flags: --no-strict-optional --local-partial-types
from typing import Protocol

class Hashable(Protocol):
Expand Down
Loading