Skip to content

List[Union[...]] is not always satisfied by valid inputs. #3351

Closed
@tomchristie

Description

@tomchristie

When a list of unions is used, mypy will incorrectly error when a subset of the possible union types is used...

example.py:

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)

console:

$ mypy --version
mypy 0.470
$ mypy example.py 
example.py:7: error: Argument 1 to "mean" has incompatible type List[int]; expected List[Union[int, float]]

Presumably the inferred List[int] ought to be able to satisfy List[Union[int, float]], right?

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