Skip to content

Union with TypeVar becomes generic #14258

Closed as not planned
Closed as not planned
@ikonst

Description

@ikonst

With disallow_any_generics, this code:

from typing import TypeVar, Union

T1 = TypeVar('T1')
U1 = Union[T1, int]

x: U1 = 5

results in

main.py:6: error: Missing type parameters for generic type "U1"  [type-arg]

The U1 type alias becomes generic and has a single argument. Curiously, it doesn't matter what you put there. For example, this passes:

x: U1[str] = 5
#     ^^^

One might think it passes because T1 is unbound, but even if we bind it (e.g. to float), it still passes:

T1 = TypeVar('T1', bound=float)
#                        ^^^^^
U1 = Union[T1, int]

x: U1[str] = 5
#     ^^^

Playground:
https://mypy-play.net/?mypy=latest&python=3.11&flags=disallow-any-generics&gist=68115745200d34208786d8267d2d324a

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-aliasTypeAlias and other type alias issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions