File tree 2 files changed +47
-0
lines changed
2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -1536,6 +1536,16 @@ def check_method_override_for_base_with_name(
1536
1536
original_type = self .function_type (original_node )
1537
1537
elif isinstance (original_node , Decorator ):
1538
1538
original_type = self .function_type (original_node .func )
1539
+ elif isinstance (original_node , Var ):
1540
+ # Super type can define method as an attribute.
1541
+ # See https://github.com/python/mypy/issues/10134
1542
+
1543
+ # We also check that sometimes `original_node.type` is None.
1544
+ # This is the case when we use something like `__hash__ = None`.
1545
+ if original_node .type is not None :
1546
+ original_type = get_proper_type (original_node .type )
1547
+ else :
1548
+ original_type = NoneType ()
1539
1549
else :
1540
1550
assert False , str (base_attr .node )
1541
1551
if isinstance (original_node , (FuncDef , OverloadedFuncDef )):
Original file line number Diff line number Diff line change @@ -114,6 +114,43 @@ class A:
114
114
A().f = None # E: Cannot assign to a method
115
115
116
116
117
+ [case testOverrideAttributeWithMethod]
118
+ # This was crashing:
119
+ # https://github.com/python/mypy/issues/10134
120
+ from typing import Protocol
121
+
122
+ class Base:
123
+ __hash__ = None
124
+
125
+ class Derived(Base):
126
+ def __hash__(self) -> int: # E: Signature of "__hash__" incompatible with supertype "Base"
127
+ pass
128
+
129
+ # Correct:
130
+
131
+ class CallableProtocol(Protocol):
132
+ def __call__(self, arg: int) -> int:
133
+ pass
134
+
135
+ class CorrectBase:
136
+ attr: CallableProtocol
137
+
138
+ class CorrectDerived(CorrectBase):
139
+ def attr(self, arg: int) -> int:
140
+ pass
141
+
142
+ [case testOverrideMethodWithAttribute]
143
+ # The reverse should not crash as well:
144
+ from typing import Callable
145
+
146
+ class Base:
147
+ def __hash__(self) -> int:
148
+ pass
149
+
150
+ class Derived(Base):
151
+ __hash__ = 1 # E: Incompatible types in assignment (expression has type "int", base class "Base" defined the type as "Callable[[Base], int]")
152
+
153
+
117
154
-- Attributes
118
155
-- ----------
119
156
You can’t perform that action at this time.
0 commit comments