Skip to content

Generic type not inferred when valid constraint available #18837

Open
@dj-foxxy

Description

@dj-foxxy
from typing import reveal_type


def foo[T: (int, str, int | str)](*args: T) -> tuple[T, ...]:
    return args


a = foo(0)
b = foo('')
c = foo(0, '')
d = foo('', 0)

reveal_type(a)
reveal_type(b)
reveal_type(c)
reveal_type(d)
$ mypy example.py
example.py:10: error: Value of type variable "T" of "foo" cannot be "object"  [type-var]
example.py:11: error: Value of type variable "T" of "foo" cannot be "object"  [type-var]
example.py:13: note: Revealed type is "builtins.tuple[builtins.int, ...]"
example.py:14: note: Revealed type is "builtins.tuple[builtins.str, ...]"
example.py:15: note: Revealed type is "builtins.tuple[builtins.object, ...]"
example.py:16: note: Revealed type is "builtins.tuple[builtins.object, ...]"
Found 2 errors in 1 file (checked 1 source file)

I'd expect c and d to be of type tuple[int | str, ...] as allowed by the constraints on T

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions