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

Recognize callable in conditional type binding #1973

Closed
abarto-machinalis opened this issue Aug 1, 2016 · 2 comments
Closed

Recognize callable in conditional type binding #1973

abarto-machinalis opened this issue Aug 1, 2016 · 2 comments

Comments

@abarto-machinalis
Copy link

If I run mypy against the following code:

from typing import Callable, Union

SomeFieldType = Union[str, Callable[[str], int]]

class SomeClass(object):
    def __init__(self, some_field: SomeFieldType) -> None:
        self.some_field = some_field

    def do_something(self) -> None:
        if callable(self.some_field):
            self.some_field('foobar')

it complains about self.some_field not being callable:

test_mypy.py: note: In member "do_something" of class "SomeClass":
test_mypy.py:11: error: "str" not callable

Perhaps mypy could be made aware of the "callable(...)" statements? Thanks!

@gvanrossum
Copy link
Member

I see. Normally unions can be disambiguated using isinstance(), but that doesn't really work for Callable.

@dmoisset
Copy link
Contributor

dmoisset commented Aug 1, 2016

If this is desirable, probably adding some more code to the find_isinstance_check analysis might resolve this

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

No branches or pull requests

4 participants