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

"object" not callable (another thorny partial issue) #2779

Closed
rknLA opened this issue Jan 31, 2017 · 6 comments
Closed

"object" not callable (another thorny partial issue) #2779

rknLA opened this issue Jan 31, 2017 · 6 comments
Labels
bug mypy got something wrong priority-2-low

Comments

@rknLA
Copy link

rknLA commented Jan 31, 2017

I suspect that this will be closed in favor of #1484, but it seemed that creating a separate issue would be more convenient for tidiness.

I have some code which is yielding a mypy error:

error: "object" not callable

The code looks similar to this (with the line of the error denoted by <---):

from functools import partial
from another_module import another_mutator

TAnyDict = Dict[str, Any]

def mutator1(bar: TAnyDict, baz: TAnyDict) -> TAnyDict:
    # ...
    return bar

def mutator2(bar: TAnyDict, baz: TAnyDict) -> TAnyDict:
    # ...
    return bar

# ... etc, more mutator functions

some_local_var = 'foo'
some_functions = [  # type: List[Callable[[TAnyDict, TAnyDict], TAnyDict]] 
    mutator1,
    mutator2,
    partial(another_mutator, some_local_var),
    mutator3,
    mutator4
]

for d in some_other_dicts:
    res = {}
    for func in some_functions:
        res = func(res, some_dict)  # <--- this is the line that causes the error
    do_something_with(res)

It was only after fully writing out an Issue for this that I realized that the core issue was related to lack of partial support, rather than an issue with my Callable declaration, or a different bug in mypy.

Obviously this will become a moot point when partial is properly supported, but in the meantime, I would have expected the mypy error to occur on the partial(...) line within the list, rather than within the for loop.

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 31, 2017

The latest mypy version complains about the example (the type annotation for some_functions is on the wrong line):

t.py:17: error: misplaced type annotation

If the annotation is left out, I can reproduce the issue -- the inferred type of some_functions will be List[object]. This is because the join of the result of partial(...) and a callable is object.

The recommended workaround is to use lambda or define a function using def instead of using partial when the latter doesn't work correctly.

The fix to #797 may also fix this issue.

@gvanrossum
Copy link
Member

gvanrossum commented Jan 31, 2017 via email

gvanrossum pushed a commit that referenced this issue Feb 1, 2017
JukkaL pushed a commit that referenced this issue Feb 1, 2017
Always process modules/packages given with -m/-p.

Fixes #2779.
@rknLA
Copy link
Author

rknLA commented Feb 1, 2017

Looks like a typo snuck in there? #2786 looks like it addresses #2778

@gvanrossum
Copy link
Member

Whoops. Thanks for noticing!

@gvanrossum gvanrossum reopened this Feb 1, 2017
@elazarg
Copy link
Contributor

elazarg commented Mar 3, 2017

@gvanrossum I believe mypy indeed should special-case functools.partial, in a way that will make binding of self (including the treatment of selftype) "fall out" of the implementation.

@ilevkivskyi
Copy link
Member

I think this should be closed in favour of #1484 (especially taking into account that there is partial support of partial due to protocols landed, so that the only way now is to write a plugin, see #1484).

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

No branches or pull requests

5 participants