Skip to content
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

Callable type is reported as not callable when used as a decorator #11639

Closed
sobolevn opened this issue Nov 29, 2021 · 1 comment
Closed

Callable type is reported as not callable when used as a decorator #11639

sobolevn opened this issue Nov 29, 2021 · 1 comment
Labels
bug mypy got something wrong

Comments

@sobolevn
Copy link
Member

Example:

from typing import Callable, Union, Any, overload, Protocol

class DCall(Protocol):
    def __call__(self, arg: Union[int, str]) -> None:
        ...

class D:
    def __getattr__(self, attr: str) -> DCall:
        # This method might contain something like during runtime:
        # if attr == '__call__':
        #     return some_func
        # assert False
        ...

def dec_d(f: Callable[..., Any]) -> D:
    return D()

@overload
def f_d(arg: int) -> None: ...
@overload
def f_d(arg: str) -> None: ...
@dec_d
def f_d(arg: Union[int, str]) -> None: ...

Outputs:

out/ex.py:18: error: "D" not callable

This happens here:

mypy/mypy/checker.py

Lines 510 to 520 in 4f59ca4

if inner_type is not None and not isinstance(inner_type, AnyType):
if isinstance(inner_type, CallableType):
impl_type = inner_type
elif isinstance(inner_type, Instance):
inner_call = get_proper_type(
find_member('__call__', inner_type, inner_type, is_operator=True)
)
if isinstance(inner_call, CallableType):
impl_type = inner_call
if impl_type is None:
self.msg.not_callable(inner_type, defn.impl)

Refs #11638
Refs #11630

@sobolevn sobolevn added the bug mypy got something wrong label Nov 29, 2021
@sobolevn
Copy link
Member Author

Mypy is right here. See #11638 (review)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant