From cc116308995df9114f125afce62c4c5331ce6bd7 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sat, 30 Sep 2017 21:51:52 +0200 Subject: [PATCH] Fix overloading on Type[...] --- mypy/checkexpr.py | 4 ++-- test-data/unit/check-overloading.test | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index e35ab9044609..5fa577c7edf4 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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): diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index 74b39fdd9bc3..e7811fc8e350 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -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]