You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"object" not callable (another thorny partial issue) #2779
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 <---):
fromfunctoolsimportpartialfromanother_moduleimportanother_mutatorTAnyDict=Dict[str, Any]
defmutator1(bar: TAnyDict, baz: TAnyDict) ->TAnyDict:
# ...returnbardefmutator2(bar: TAnyDict, baz: TAnyDict) ->TAnyDict:
# ...returnbar# ... etc, more mutator functionssome_local_var='foo'some_functions= [ # type: List[Callable[[TAnyDict, TAnyDict], TAnyDict]] mutator1,
mutator2,
partial(another_mutator, some_local_var),
mutator3,
mutator4
]
fordinsome_other_dicts:
res= {}
forfuncinsome_functions:
res=func(res, some_dict) # <--- this is the line that causes the errordo_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.
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.
@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.
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).
Activity
JukkaL commentedon Jan 31, 2017
The latest mypy version complains about the example (the type annotation for
some_functionsis on the wrong line):If the annotation is left out, I can reproduce the issue -- the inferred type of
some_functionswill beList[object]. This is because the join of the result ofpartial(...)and a callable isobject.The recommended workaround is to use
lambdaor define a function usingdefinstead of usingpartialwhen the latter doesn't work correctly.The fix to #797 may also fix this issue.
gvanrossum commentedon Jan 31, 2017
Always process modules/packages given with -m/-p
Always process modules/packages given with -m/-p (#2786)
rknLA commentedon Feb 1, 2017
Looks like a typo snuck in there? #2786 looks like it addresses #2778
gvanrossum commentedon Feb 1, 2017
Whoops. Thanks for noticing!
elazarg commentedon 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 commentedon Aug 19, 2017
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).