Skip to content

Commit 51a8443

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

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,6 +3290,26 @@ 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 1
3301+
@overload
3302+
def f(x: str) -> str: return ''
3303+
3304+
e1: EM
3305+
reveal_type(f(e1)) # E: Revealed type is 'builtins.int'
3306+
3307+
e2: Type[E]
3308+
reveal_type(f(e2)) # E: Revealed type is 'builtins.int'
3309+
3310+
[out]
3311+
main:6: error: An overloaded function outside a stub file must have an implementation
3312+
32933313
-- Synthetic types crashes
32943314
-- -----------------------
32953315

0 commit comments

Comments
 (0)