Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sobolevn opened this issue Sep 20, 2021 · 2 comments
Open

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

sobolevn opened this issue Sep 20, 2021 · 2 comments
Labels
bug mypy got something wrong topic-union-types

Comments

@sobolevn
Copy link
Member

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.

@Thanayaby
Copy link

☠️👿🧟‍♀️💃🤫

@ilevkivskyi
Copy link
Member

Adding an upper bound on T1 fixes this, see #6417. Although I agree it would be great if this worked without a bound. Proper solution for this is complex however.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-union-types
Projects
None yet
Development

No branches or pull requests

4 participants