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

Tuple-like typing for lists in type hints #1267

Closed
TruncatedDinoSour opened this issue Oct 1, 2022 · 1 comment
Closed

Tuple-like typing for lists in type hints #1267

TruncatedDinoSour opened this issue Oct 1, 2022 · 1 comment
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@TruncatedDinoSour
Copy link

Hello python devs,

I always run into an issue like this while
working with Union data, so like I know
what elements will be in a list, but I don't
think there's a way to type them, so I always
have to use # type: ignore in certain places

Even though that description it's kinda meh,
here's what I mean:

x: Dict[str, List[int, int]]  # This is a `dict` of `list` which always
                              # has 2 elements -- int and int

For example

x: Dict[str, List[int, int]] = {
    "hi": [1, 2],
    "bye": [3, 1],
    "aaa": [79, 27],
    "haxxor": [1337, 420],
}

And things like this should fail:

x: Dict[str, List[int, int]] = {
    "hi": [1, 2],
    "bye": [3, 1],
    "aaa": [79, 27],
    "haxxor": [1337, 420, 69],  # error: Incompatible types in assignment (expression has type "List[int, int, int]", variable has type "List[int, int]")
}

Unions aren't as convenient...

@TruncatedDinoSour TruncatedDinoSour added the topic: feature Discussions about new features for Python's type annotations label Oct 1, 2022
@hauntsaninja
Copy link
Collaborator

Duplicate of #592

@hauntsaninja hauntsaninja marked this as a duplicate of #592 Oct 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

2 participants