Skip to content

Fix overloading on Type[...] #4037

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 1 commit into from
Oct 2, 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: 2 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2817,10 +2817,10 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
# Since Type[T] is covariant, check if actual = Type[A] is
# a subtype of formal = Type[F].
return overload_arg_similarity(actual.item, formal.item)
elif isinstance(actual, CallableType) and actual.is_type_obj():
elif isinstance(actual, FunctionLike) and actual.is_type_obj():
# Check if the actual is a constructor of some sort.
# Note that this is this unsound, since we don't check the __init__ signature.
return overload_arg_similarity(actual.ret_type, formal.item)
return overload_arg_similarity(actual.items()[0].ret_type, formal.item)
else:
return 0
if isinstance(actual, TypedDictType):
Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -1272,3 +1272,28 @@ a: Any
# The return type is not ambiguous so Any arguments cause no ambiguity.
reveal_type(f(a, 1, 1)) # E: Revealed type is 'builtins.str'
reveal_type(f(1, *a)) # E: Revealed type is 'builtins.str'

[case testOverloadOnOverloadWithType]
from typing import Any, Type, TypeVar, overload
from mod import MyInt
T = TypeVar('T')

@overload
def make(cls: Type[T]) -> T: pass
@overload
def make() -> Any: pass

def make(*args):
pass

c = make(MyInt)
reveal_type(c) # E: Revealed type is 'mod.MyInt*'

[file mod.pyi]
from typing import overload
class MyInt:
@overload
def __init__(self, x: str) -> None: pass
@overload
def __init__(self, x: str, y: int) -> None: pass
[out]