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.__call__ leads to Callable is not callable #5079

Open
ethanhs opened this issue May 18, 2018 · 9 comments
Open

callable.__call__ leads to Callable is not callable #5079

ethanhs opened this issue May 18, 2018 · 9 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code

Comments

@ethanhs
Copy link
Collaborator

ethanhs commented May 18, 2018

In a quite amusing, confusing error:

mypy -c "callable.__call__"
# error: Callable[[Iterable[object]], bool] is not callable
@ethanhs ethanhs added bug mypy got something wrong priority-1-normal false-positive mypy gave an error on correct code labels May 18, 2018
@ezyang
Copy link

ezyang commented Jun 18, 2019

I'm affected by this too!

@ethanhs
Copy link
Collaborator Author

ethanhs commented Jun 18, 2019

@ezyang, do you have some real world code that triggers this?

@ezyang
Copy link

ezyang commented Jun 18, 2019

Actually, it turns out my bug is unrelated (and I was trying to (incorrectly) use __call__ to work around it). I'll file a different bug

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 27, 2022

The error message on 0.941 is arguably even worse:

"Callable[[object], TypeGuard[Callable[..., object]]]" not callable

But it doesn't seem like there's much real-world code affected by this, so I'm lowering the priority level.

@jmmaa
Copy link

jmmaa commented Apr 29, 2022

any updates to this problem?

@mastercoms
Copy link

mastercoms commented Oct 5, 2022

I am experiencing this problem in this PR: fastapi/fastapi#5461

@randolf-scholz
Copy link
Contributor

randolf-scholz commented Feb 21, 2023

I have some real-world example: I am writing a lazy function class, that takes a Callable and all of it's *args and **kwargs, but delays evaluation. To write a nice __repr__, I want to read the __annotations__, if they exist, to hint at what kind of object the function will return (i.e. something along the lines of LazyFunction<int> if the given function returns int.)

Now, regular functions and callable classes store __annotations__ at different places, hence it runs into this error

from types import FunctionType
from typing import Callable


def show_annotations(func: Callable) -> None:
    if isinstance(func, FunctionType):
        print(func.__annotations__)
    else:
        print(func.__call__.__annotations__)  # ✘ error [operator]


class Foo:
    def __call__(self) -> None:
        pass


def foo() -> None:
    pass


show_annotations(foo)
show_annotations(Foo())

@JelleZijlstra
Copy link
Member

@randolf-scholz note that your code isn't type-safe as there is no guarantee that the __call__ attribute will itself have .__annotations__; it may be another non-function callable.

Obviously the mypy error at issue here is still a bug.

@finite-state-machine
Copy link

This applies to any callable, as far as I can see.

Here's a simple test case: gist

def some_function() -> None:
    pass

some_function.__call__
        # error: "Callable[[], None]" not callable  [operator]

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

No branches or pull requests

8 participants