Skip to content

Commit fe18437

Browse files
miss-islingtonvberlierJelleZijlstra
authored
[3.13] gh-130117: Document why nested Union, Literal, and Annotated types referenced through a type alias are not flattened (GH-130119) (#133488)
gh-130117: Document why nested `Union`, `Literal`, and `Annotated` types referenced through a type alias are not flattened (GH-130119) (cherry picked from commit b936ccd) Co-authored-by: Valentin Berlier <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 1dfe5de commit fe18437

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Doc/library/typing.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,12 @@ These can be used as types in annotations. They all support subscription using
10981098

10991099
Union[Union[int, str], float] == Union[int, str, float]
11001100

1101+
However, this does not apply to unions referenced through a type
1102+
alias, to avoid forcing evaluation of the underlying :class:`TypeAliasType`::
1103+
1104+
type A = Union[int, str]
1105+
Union[A, float] != Union[int, str, float]
1106+
11011107
* Unions of a single argument vanish, e.g.::
11021108

11031109
Union[int] == int # The constructor actually returns int
@@ -1222,6 +1228,32 @@ These can be used as types in annotations. They all support subscription using
12221228
is allowed as type argument to ``Literal[...]``, but type checkers may
12231229
impose restrictions. See :pep:`586` for more details about literal types.
12241230

1231+
Additional details:
1232+
1233+
* The arguments must be literal values and there must be at least one.
1234+
1235+
* Nested ``Literal`` types are flattened, e.g.::
1236+
1237+
assert Literal[Literal[1, 2], 3] == Literal[1, 2, 3]
1238+
1239+
However, this does not apply to ``Literal`` types referenced through a type
1240+
alias, to avoid forcing evaluation of the underlying :class:`TypeAliasType`::
1241+
1242+
type A = Literal[1, 2]
1243+
assert Literal[A, 3] != Literal[1, 2, 3]
1244+
1245+
* Redundant arguments are skipped, e.g.::
1246+
1247+
assert Literal[1, 2, 1] == Literal[1, 2]
1248+
1249+
* When comparing literals, the argument order is ignored, e.g.::
1250+
1251+
assert Literal[1, 2] == Literal[2, 1]
1252+
1253+
* You cannot subclass or instantiate a ``Literal``.
1254+
1255+
* You cannot write ``Literal[X][Y]``.
1256+
12251257
.. versionadded:: 3.8
12261258

12271259
.. versionchanged:: 3.9.1
@@ -1392,6 +1424,14 @@ These can be used as types in annotations. They all support subscription using
13921424
int, ValueRange(3, 10), ctype("char")
13931425
]
13941426

1427+
However, this does not apply to ``Annotated`` types referenced through a type
1428+
alias, to avoid forcing evaluation of the underlying :class:`TypeAliasType`::
1429+
1430+
type From3To10[T] = Annotated[T, ValueRange(3, 10)]
1431+
assert Annotated[From3To10[int], ctype("char")] != Annotated[
1432+
int, ValueRange(3, 10), ctype("char")
1433+
]
1434+
13951435
Duplicated metadata elements are not removed::
13961436

13971437
assert Annotated[int, ValueRange(3, 10)] != Annotated[

0 commit comments

Comments
 (0)