Skip to content

Fix overload resolution for metaclass #3511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2651,8 +2651,12 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
else:
return 0
elif isinstance(actual, TypeType):
item = actual.item
if formal.type.fullname() in {"builtins.object", "builtins.type"}:
return 2
elif isinstance(item, Instance):
# FIX: this does not handle e.g. Union of instances
return overload_arg_similarity(item.type.metaclass_type, formal)
else:
return 0
else:
Expand Down
32 changes: 32 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -3290,6 +3290,38 @@ x3: M = cv

[builtins fixtures/classmethod.pyi]

[case testMetaclassOverloadResolution]
from typing import Type, overload
class A: pass

class EM(type): pass
class E(metaclass=EM): pass

class EM1(type): pass
class E1(metaclass=EM1): pass

@overload
def f(x: EM) -> int: ...
@overload
def f(x: EM1) -> A: ...
@overload
def f(x: str) -> str: ...
def f(x: object) -> object: return ''

e: EM
reveal_type(f(e)) # E: Revealed type is 'builtins.int'

et: Type[E]
reveal_type(f(et)) # E: Revealed type is 'builtins.int'

e1: EM1
reveal_type(f(e1)) # E: Revealed type is '__main__.A'

e1t: Type[E1]
reveal_type(f(e1t)) # E: Revealed type is '__main__.A'

reveal_type(f('')) # E: Revealed type is 'builtins.str'

-- Synthetic types crashes
-- -----------------------

Expand Down