Skip to content

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

Closed

Description

@rknLA

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.

Activity

  1. JukkaL commented on Jan 31, 2017

    @JukkaL
    Collaborator

    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.

  2. gvanrossum commented on Jan 31, 2017

    @gvanrossum
    Member
  3. added a commit that references this issue on Feb 1, 2017
    a5f3f7a
  4. added a commit that references this issue on Feb 1, 2017
    7c102f0
  5. rknLA commented on Feb 1, 2017

    @rknLA
    Author

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

  6. gvanrossum commented on Feb 1, 2017

    @gvanrossum
    Member

    Whoops. Thanks for noticing!

  7. elazarg commented on Mar 3, 2017

    @elazarg
    Contributor

    @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.

  8. ilevkivskyi commented on Aug 19, 2017

    @ilevkivskyi
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions