Skip to content

Argument type Union[T, List[T]] fails to infer constaints #11149

Open
@sobolevn

Description

@sobolevn

While working on #11128 I got hit by this.

This code does not type check:

from typing import TypeVar, Union, List

T1 = TypeVar("T1")

def promote_list(item: Union[T1, List[T1]]) -> List[T1]:
    ...

exprs: List[int]
reveal_type(promote_list(exprs))  # error
# ex.py:9: note: Revealed type is "builtins.list[<nothing>]"
# ex.py:9: error: Argument 1 to "promote_list" has incompatible type "List[int]"; expected "List[<nothing>]"

reveal_type(promote_list(1))  # ok
# Revealed type is "builtins.list[builtins.int*]"

Which is not what I expect. For example, TypeScript solves this properly:

function protomoteList<T>(input: T | T[]): T[] {
    return []
}

function test(input: number[]) {}

test(protomoteList(1))  # ok

Link: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABABwE5ynAthgpgGRgGcoAeAFQD4AKGMZEKALkXMQB9WBtAXQEoW5XogDeAWABQiaYlS4oIVEl6SAvpMmhIsBIii4SteoxZgQWAEa5UvPqPUTJ+w2gzY8hQwEY+fSUA

Cause

It happens somewhere here:

mypy/mypy/constraints.py

Lines 168 to 171 in b3ff2a6

return any_constraints(
[infer_constraints_if_possible(t_item, actual, direction)
for t_item in template.items],
eager=False)

I will try to solve this, but I know that this is going to be complicated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions