10000 Ensure instances of `CallableType` can always be hashed by AlexWaygood · Pull Request #12741 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Ensure instances of CallableType can always be hashed #12741

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 1 commit into from
May 8, 2022
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
9 changes: 7 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import mypy.nodes
from mypy.state import state
from mypy.nodes import (
INVARIANT, SymbolNode, FuncDef,
INVARIANT, SymbolNode, FuncDef, FakeInfo,
ArgKind, ARG_POS, ARG_STAR, ARG_STAR2,
)
from mypy.util import IdMapper
Expand Down Expand Up @@ -1766,7 +1766,12 @@ def expand_param_spec(self,
variables=[*variables, *self.variables])

def __hash__(self) -> int:
return hash((self.ret_type, self.is_type_obj(),
# self.is_type_obj() will fail if self.fallback.type is a FakeInfo
if isinstance(self.fallback.type, FakeInfo):
is_type_obj = 2
else:
is_type_obj = self.is_type_obj()
return hash((self.ret_type, is_type_obj,
self.is_ellipsis_args, self.name,
tuple(self.arg_types), tuple(self.arg_names), tuple(self.arg_kinds)))

Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ reveal_type(d) # N: Revealed type is "TypedDict('__main__.D', {'y': builtins.in
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictWithClassmethodAlternativeConstructorDoesNotCrash]
# https://github.com/python/mypy/issues/5653
from typing import TypedDict

class Foo(TypedDict):
bar: str
@classmethod # E: Invalid statement in TypedDict definition; expected "field_name: field_type"
def baz(cls) -> "Foo": ...
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testCanCreateTypedDictTypeWithUnderscoreItemName]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int, '_fallback': object})
Expand Down
0