8000 gh-130117: Document why nested `Union`, `Literal`, and `Annotated` ty… · python/cpython@b936ccd · GitHub
[go: up one dir, main page]

Skip to content

Commit b936ccd

Browse files
gh-130117: Document why nested Union, Literal, and Annotated types referenced through a type alias are not flattened (#130119)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 529012e commit b936ccd

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
@@ -1230,6 +1236,32 @@ These can be used as types in annotations. They all support subscription using
12301236
is allowed as type argument to ``Literal[...]``, but type checkers may
12311237
impose restrictions. See :pep:`586` for more details about literal types.
12321238

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

12351267
.. versionchanged:: 3.9.1
@@ -1400,6 +1432,14 @@ These can be used as types in annotations. They all support subscription using
14001432
int, ValueRange(3, 10), ctype("char")
14011433
]
14021434

1435+
However, this does not apply to ``Annotated`` types referenced through a type
1436+
alias, to avoid forcing evaluation of the underlying :class:`TypeAliasType`::
1437+
1438+
type From3To10[T] = Annotated[T, ValueRange(3, 10)]
1439+
assert Annotated[From3To10[int], ctype("char")] != Annotated[
1440+
int, ValueRange(3, 10), ctype("char")
1441+
]
1442+
14031443
Duplicated metadata elements are not removed::
14041444

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

0 commit comments

Comments
 (0)
0