Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,9 @@ type IntList = list[int]
m: IntList = [1, 2, 3]
reveal_type(m) # revealed: list[int]

# TODO: this should type-check and avoid literal promotion
# error: [invalid-assignment] "Object of type `list[Unknown | int]` is not assignable to `list[Literal[1, 2, 3]]`"
n: list[typing.Literal[1, 2, 3]] = [1, 2, 3]
reveal_type(n) # revealed: list[Literal[1, 2, 3]]

# TODO: this should type-check and avoid literal promotion
# error: [invalid-assignment] "Object of type `list[Unknown | str]` is not assignable to `list[LiteralString]`"
o: list[typing.LiteralString] = ["a", "b", "c"]
reveal_type(o) # revealed: list[LiteralString]

Expand All @@ -160,6 +156,81 @@ a: list[str] = [1, 2, 3]
b: set[int] = {1, 2, "3"}
```

## Literal annnotations are respected

```toml
[environment]
python-version = "3.12"
```

```py
from enum import Enum
from typing_extensions import Literal, LiteralString

a: list[Literal[1]] = [1]
reveal_type(a) # revealed: list[Literal[1]]

b: list[Literal[True]] = [True]
reveal_type(b) # revealed: list[Literal[True]]

c: list[Literal["a"]] = ["a"]
reveal_type(c) # revealed: list[Literal["a"]]

d: list[LiteralString] = ["a", "b", "c"]
reveal_type(d) # revealed: list[LiteralString]

e: list[list[Literal[1]]] = [[1]]
reveal_type(e) # revealed: list[list[Literal[1]]]

class Color(Enum):
RED = "red"

f: dict[list[Literal[1]], list[Literal[Color.RED]]] = {[1]: [Color.RED, Color.RED]}
reveal_type(f) # revealed: dict[list[Literal[1]], list[Literal[Color.RED]]]

class X[T]:
def __init__(self, value: T): ...

g: X[Literal[1]] = X(1)
reveal_type(g) # revealed: X[Literal[1]]

h: X[int] = X(1)
reveal_type(h) # revealed: X[int]

i: dict[list[X[Literal[1]]], set[Literal[b"a"]]] = {[X(1)]: {b"a"}}
reveal_type(i) # revealed: dict[list[X[Literal[1]]], set[Literal[b"a"]]]

j: list[Literal[1, 2, 3]] = [1, 2, 3]
reveal_type(j) # revealed: list[Literal[1, 2, 3]]

k: list[Literal[1] | Literal[2] | Literal[3]] = [1, 2, 3]
reveal_type(k) # revealed: list[Literal[1, 2, 3]]

type Y[T] = list[T]

l: Y[Y[Literal[1]]] = [[1]]
reveal_type(l) # revealed: list[list[Literal[1]]]

m: list[tuple[Literal[1], Literal[2], Literal[3]]] = [(1, 2, 3)]
reveal_type(m) # revealed: list[tuple[Literal[1], Literal[2], Literal[3]]]

n: list[tuple[int, str, int]] = [(1, "2", 3), (4, "5", 6)]
reveal_type(n) # revealed: list[tuple[int, str, int]]

o: list[tuple[Literal[1], ...]] = [(1, 1, 1)]
reveal_type(o) # revealed: list[tuple[Literal[1], ...]]

p: list[tuple[int, ...]] = [(1, 1, 1)]
reveal_type(p) # revealed: list[tuple[int, ...]]

# literal promotion occurs based on assignability, an exact match is not required
q: list[int | Literal[1]] = [1]
reveal_type(q) # revealed: list[int]

r: list[Literal[1, 2, 3, 4]] = [1, 2]
reveal_type(r) # revealed: list[Literal[1, 2, 3, 4]]
```

## PEP-604 annotations are supported

```py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,6 @@ from typing import Literal
reveal_type(list((1, 2, 3))) # revealed: list[int]
reveal_type(list(((1, 2, 3),))) # revealed: list[tuple[int, int, int]]

# TODO: we could bidirectionally infer that the user does not want literals to be promoted here,
# and avoid this diagnostic
#
# error: [invalid-assignment] "`list[int]` is not assignable to `list[Literal[1, 2, 3]]`"
Comment on lines -528 to -531
Copy link
Member

@AlexWaygood AlexWaygood Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are some interesting examples above this for unannotated frozensets (which don't need to be fixed in this PR, I'm just mentioning it because it might be something you'd be interested in fixing in future work). frozenset, like tuple, is covariant, so for an unannotated x = frozenset((1, 2, 3)) call, we would ideally infer the type as frozenset[Literal[1, 2, 3]], the same as we would infer (1, 2, 3) as tuple[Literal[1], Literal[2], Literal[3]] rather than tuple[int, int, int]. Unlike with invariant generics, Literal types don't really cause any ergonomic issues when they appear in specializations of covariant types.

But we need to be careful: list(frozenset((1, 2, 3))) should still be inferred as list[int] rather than list[Literal[1, 2, 3]] if there's no declared type! And [frozenset((1, 2))] should be list[frozenset[int]]

x: list[Literal[1, 2, 3]] = list((1, 2, 3))
reveal_type(x) # revealed: list[Literal[1, 2, 3]]
```
Expand Down
Loading
Loading