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

isinstance(value, Callable) is flagged as an error #14928

Closed
mthuurne opened this issue Mar 20, 2023 · 2 comments
Closed

isinstance(value, Callable) is flagged as an error #14928

mthuurne opened this issue Mar 20, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@mthuurne
Copy link
Contributor

Bug Report

When using isinstance(value, Callable), mypy flags it as an error, claiming that Callable is a typing special form. While it is an error to use a specialized form of Callable such as Callable[[int], str], the generic class is valid for isinstance checks.

To Reproduce

Run mypy on the following code: (playground)

from collections.abc import Callable

def check(factory: Callable) -> None:
    if not isinstance(factory, Callable):
        raise TypeError("'factory' is not callable")

I also tried importing Callable from typing instead of collections.abc, but that doesn't make a difference.

Expected Behavior

No error is reported, as this construct executes fine and does what is expected: checking whether the object is callable.

Actual Behavior

testcase.py:4: error: Argument 2 to "isinstance" has incompatible type "<typing special form>"; expected "_ClassInfo"  [arg-type]
        if not isinstance(factory, Callable):
                                   ^~~~~~~~

Your Environment

  • Mypy version used: 1.1.1
  • Python version used: 3.10.6
@mthuurne mthuurne added the bug mypy got something wrong label Mar 20, 2023
@mthuurne mthuurne changed the title isinstance(value, Callable is flagged as an error isinstance(value, Callable) is flagged as an error Mar 20, 2023
mthuurne added a commit to ProtixIT/django-model-utils that referenced this issue Mar 20, 2023
This is slightly more compact, but the main motivation is to work
around the following mypy bug:

python/mypy#14928
@AlexWaygood
Copy link
Member

Duplicate of #3060

@AlexWaygood AlexWaygood marked this as a duplicate of #3060 Mar 20, 2023
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Mar 20, 2023
@AlexWaygood
Copy link
Member

Duplicate of #3060

(#3060 concerns typing.Type rather than Callable, but it's the same error message on mypy 1.1.1, and it's caused by the same underlying bug)

mthuurne added a commit to ProtixIT/django-model-utils that referenced this issue Mar 21, 2023
This is slightly more compact, but the main motivation is to work
around the following mypy bug:

python/mypy#14928
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

2 participants