Skip to content

Fix untyped decorator overload error on decorator with __call__ overloads #9232

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
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
5 changes: 4 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
27 changes: 27 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down