Skip to content

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

@sobolevn

Description

@sobolevn
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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sobolevn

        Issue actions

          Callable type is reported as not callable when used as a decorator · Issue #11639 · python/mypy