From 66c2ba068b8a7f64e853d3f9a347c4bf0a855e05 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 31 Jul 2020 11:30:52 +0300 Subject: [PATCH] Fix untyped decorator overload error on class decorator with __call__ overloads --- mypy/checker.py | 5 ++++- test-data/unit/check-overloading.test | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mypy/checker.py b/mypy/checker.py index 2558440168e3..75739fe87a00 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -5757,7 +5757,10 @@ def is_untyped_decorator(typ: Optional[Type]) -> bool: elif isinstance(typ, Instance): method = typ.type.get_method('__call__') if method: - return not is_typed_callable(method.type) + if isinstance(method.type, Overloaded): + return any(is_untyped_decorator(item) for item in method.type.items()) + else: + return not is_typed_callable(method.type) else: return False elif isinstance(typ, Overloaded): diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index 4b3fbfdda851..959983db4495 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -4955,6 +4955,33 @@ def g(name: str) -> int: reveal_type(f) # N: Revealed type is 'def (name: builtins.str) -> builtins.int' reveal_type(g) # N: Revealed type is 'def (name: builtins.str) -> builtins.int' +[case testDisallowUntypedDecoratorsOverloadDunderCall] +# flags: --disallow-untyped-decorators +from typing import Any, Callable, overload, TypeVar + +F = TypeVar('F', bound=Callable[..., Any]) + +class Dec: + @overload + def __call__(self, x: F) -> F: ... + @overload + def __call__(self, x: str) -> Callable[[F], F]: ... + def __call__(self, x) -> Any: + pass + +dec = Dec() + +@dec +def f(name: str) -> int: + return 0 + +@dec('abc') +def g(name: str) -> int: + return 0 + +reveal_type(f) # N: Revealed type is 'def (name: builtins.str) -> builtins.int' +reveal_type(g) # N: Revealed type is 'def (name: builtins.str) -> builtins.int' + [case testOverloadBadArgumentsInferredToAny1] from typing import Union, Any, overload