Skip to content

Commit b025991

Browse files
committed
Generalize method hook to work with Instances
1 parent 166a911 commit b025991

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

mypy/checkexpr.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,19 @@ def visit_call_expr(self, e: CallExpr, allow_none_return: bool = False) -> Type:
218218
and isinstance(e.callee, MemberExpr)
219219
and isinstance(callee_type, FunctionLike)):
220220
callee_expr_type = self.chk.type_map.get(e.callee.expr)
221-
if isinstance(callee_expr_type, TypedDictType):
221+
info = None
222+
# TODO: Support fallbacks of other kinds of types as well?
223+
if isinstance(callee_expr_type, Instance):
224+
info = callee_expr_type.type
225+
elif isinstance(callee_expr_type, TypedDictType):
222226
info = callee_expr_type.fallback.type.get_containing_type_info(e.callee.name)
223-
if info:
224-
fullname = '{}.{}'.format(info.fullname(), e.callee.name)
225-
object_type = callee_expr_type
226-
signature_hook = self.plugin.get_method_signature_hook(fullname)
227-
if signature_hook:
228-
callee_type = self.apply_method_signature_hook(
229-
e, callee_type, object_type, signature_hook)
227+
if info:
228+
fullname = '{}.{}'.format(info.fullname(), e.callee.name)
229+
object_type = callee_expr_type
230+
signature_hook = self.plugin.get_method_signature_hook(fullname)
231+
if signature_hook:
232+
callee_type = self.apply_method_signature_hook(
233+
e, callee_type, object_type, signature_hook)
230234
ret_type = self.check_call_expr_with_callee_type(callee_type, e, fullname, object_type)
231235
if isinstance(ret_type, UninhabitedType):
232236
self.chk.binder.unreachable()

test-data/unit/check-expressions.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,4 +1698,5 @@ reveal_type(a**(-0)) # E: Revealed type is 'builtins.int'
16981698
reveal_type(a**(-1)) # E: Revealed type is 'builtins.float'
16991699
reveal_type(a**(-2)) # E: Revealed type is 'builtins.float'
17001700
reveal_type(a**b) # E: Revealed type is 'Any'
1701+
reveal_type(a.__pow__(2)) # E: Revealed type is 'builtins.int'
17011702
[builtins fixtures/ops.pyi]

0 commit comments

Comments
 (0)