Skip to content

Better messages when type is incompatible due to invariance #3352

Closed
@JukkaL

Description

@JukkaL

Users are often confused by compatibility of invariant collection types. Here is an example from #3351:

from typing import List, Union

def mean(numbers: List[Union[int, float]]) -> float:
    return sum(numbers) / len(numbers)

some_numbers = [1, 2, 3, 4]
mean(some_numbers)  # Argument 1 to "mean" has incompatible type List[int]; 
                    # expected List[Union[int, float]]

It might be helpful to add a note with more details, since the message doesn't really provide enough context to start googling for help. Here is one idea:

program.py:23:note: "List" is invariant -- see <link to docs>
program.py:23:note: Maybe you can use "Sequence" as the target type, which is covariant?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions