Skip to content

mypy error: Cannot infer type argument 1 of "sum" #3889

Closed

Description

@levsa

When using sum() to concatenate lists, mypy fails to infer the type argument of "sum", even if types are declared explicitly.

sum([[1],[2],[3]], []

or

foo: List[List[int]] = [[1],[2],[3]]
sum(foo, [])

both give:

error: Cannot infer type argument 1 of "sum"

Activity

  1. JukkaL commented on Aug 30, 2017

    @JukkaL
    Collaborator

    This looks like a duplicate of #230. Here are two workarounds:

    • You can use a local variable to hold the empty list and annotate the variable (empty: List[int] = []) and pass the variable as the second argument to sum.
    • You can cast the empty list to the desired type: sum(foo, cast(List[int], []))
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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions