Skip to content

Commit ce309a9

Browse files
committed
fall to metaclass in overload
1 parent ed67bfc commit ce309a9

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

mypy/checkexpr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,8 +2651,12 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
26512651
else:
26522652
return 0
26532653
elif isinstance(actual, TypeType):
2654+
item = actual.item
26542655
if formal.type.fullname() in {"builtins.object", "builtins.type"}:
26552656
return 2
2657+
elif isinstance(item, Instance):
2658+
# FIX: this does not handle e.g. Union of instances
2659+
return overload_arg_similarity(item.type.metaclass_type, formal)
26562660
else:
26572661
return 0
26582662
else:

test-data/unit/check-classes.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,6 +3290,24 @@ x3: M = cv
32903290

32913291
[builtins fixtures/classmethod.pyi]
32923292

3293+
[case testMetaclassOverloadResolution]
3294+
from typing import Type, overload
3295+
3296+
class EM(type): pass
3297+
class E(metaclass=EM): pass
3298+
3299+
@overload
3300+
def f(x: EM) -> int: return 0
3301+
@overload
3302+
def f(x: str) -> str: return ''
3303+
def f(x: object) -> object: return ''
3304+
3305+
e1: EM
3306+
reveal_type(f(e1)) # E: Revealed type is 'builtins.int'
3307+
3308+
e2: Type[E]
3309+
reveal_type(f(e2)) # E: Revealed type is 'builtins.int'
3310+
32933311
-- Synthetic types crashes
32943312
-- -----------------------
32953313

0 commit comments

Comments
 (0)