Skip to content

Commit 53c6e58

Browse files
elazargJukkaL
authored andcommitted
Fix overload resolution for metaclass (#3511)
Fix #3452.
1 parent 5d85832 commit 53c6e58

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

mypy/checkexpr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,8 +2691,12 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
26912691
else:
26922692
return 0
26932693
elif isinstance(actual, TypeType):
2694+
item = actual.item
26942695
if formal.type.fullname() in {"builtins.object", "builtins.type"}:
26952696
return 2
2697+
elif isinstance(item, Instance):
2698+
# FIX: this does not handle e.g. Union of instances
2699+
return overload_arg_similarity(item.type.metaclass_type, formal)
26962700
else:
26972701
return 0
26982702
else:

test-data/unit/check-classes.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3289,6 +3289,38 @@ x3: M = cv
32893289

32903290
[builtins fixtures/classmethod.pyi]
32913291

3292+
[case testMetaclassOverloadResolution]
3293+
from typing import Type, overload
3294+
class A: pass
3295+
3296+
class EM(type): pass
3297+
class E(metaclass=EM): pass
3298+
3299+
class EM1(type): pass
3300+
class E1(metaclass=EM1): pass
3301+
3302+
@overload
3303+
def f(x: EM) -> int: ...
3304+
@overload
3305+
def f(x: EM1) -> A: ...
3306+
@overload
3307+
def f(x: str) -> str: ...
3308+
def f(x: object) -> object: return ''
3309+
3310+
e: EM
3311+
reveal_type(f(e)) # E: Revealed type is 'builtins.int'
3312+
3313+
et: Type[E]
3314+
reveal_type(f(et)) # E: Revealed type is 'builtins.int'
3315+
3316+
e1: EM1
3317+
reveal_type(f(e1)) # E: Revealed type is '__main__.A'
3318+
3319+
e1t: Type[E1]
3320+
reveal_type(f(e1t)) # E: Revealed type is '__main__.A'
3321+
3322+
reveal_type(f('')) # E: Revealed type is 'builtins.str'
3323+
32923324
-- Synthetic types crashes
32933325
-- -----------------------
32943326

0 commit comments

Comments
 (0)