-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathdataclasses_hash.py
More file actions
105 lines (67 loc) · 2.01 KB
/
dataclasses_hash.py
File metadata and controls
105 lines (67 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Tests the synthesis of the __hash__ method in a dataclass.
"""
from dataclasses import dataclass
from typing import Hashable, assert_type
@dataclass
class DC1:
a: int
assert_type(DC1.__hash__, None)
# These should generate errors because DC1 isn't hashable.
DC1(0).__hash__() # E
v1: Hashable = DC1(0) # E
@dataclass(eq=True, frozen=True)
class DC2:
a: int
# Because `DC2` is frozen, type checkers should synthesize
# a callable `__hash__` method for it, and therefore should
# emit a diagnostic here:
assert_type(DC2.__hash__, None) # E
DC2(0).__hash__() # OK
v2: Hashable = DC2(0) # OK
@dataclass(eq=True)
class DC3:
a: int
assert_type(DC3.__hash__, None)
# These should generate errors because DC3 isn't hashable.
DC3(0).__hash__() # E
v3: Hashable = DC3(0) # E
@dataclass(frozen=True)
class DC4:
a: int
# Because `DC4` is frozen, type checkers should synthesize
# a callable `__hash__` method for it, and therefore should
# emit a diagnostic here:
assert_type(DC4.__hash__, None) # E
DC4(0).__hash__() # OK
v4: Hashable = DC4(0) # OK
@dataclass(eq=True, unsafe_hash=True)
class DC5:
a: int
# Type checkers should synthesize a callable `__hash__`
# method for `DC5` due to `unsafe_hash=True`, and therefore
# should emit a diagnostic here:
assert_type(DC5.__hash__, None) # E
DC5(0).__hash__() # OK
v5: Hashable = DC5(0) # OK
@dataclass(eq=True)
class DC6:
a: int
def __hash__(self) -> int:
return 0
# Type checkers should respect the manually defined `__hash__`
# method for `DC6`, and therefore should emit a diagnostic here:
assert_type(DC6.__hash__, None) # E
DC6(0).__hash__() # OK
v6: Hashable = DC6(0) # OK
@dataclass(frozen=True)
class DC7:
a: int
def __eq__(self, other) -> bool:
return self.a == other.a
# Because `DC7` is frozen, type checkers should synthesize
# a callable `__hash__` method for it, and therefore should
# emit a diagnostic here:
assert_type(DC7.__hash__, None) # E
DC7(0).__hash__() # OK
v7: Hashable = DC7(0) # OK