Skip to content

Incompatible types in assignment when assigning tuple values of different type to _ #4831

@ddeville

Description

@ddeville

I often see code where a method returns a tuple of values with different type but the caller only cares about certain elements. In that case, it is common to specify _ as the variable name for these elements.

However, if two or more of these _ variables have a different type, mypy will complain that an expression is assigned to a variable of different type.

Repro:

def test():
    # type: () -> Tuple[str, int, float]
    return "hello", 1, 2.0

s, _, _ = test()

and the error:

error: Incompatible types in assignment (expression has type "float", variable has type "int")

It would be nice if mypy could detect that an unused tuple element is being assigned to _.

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