Skip to content

Commit 929121a

Browse files
committed
Same improvement to the documentation of Union and Literal
1 parent 5371b7a commit 929121a

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Doc/library/typing.rst

Lines changed: 33 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,33 @@ 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+
``Literal`` is very similar to :class:`Union`, the main difference is that its
1232+
arguments are literal values instead of types:
1233+
1234+
* The arguments must be literal values and there must be at least one.
1235+
1236+
* Nested ``Literal`` types are flattened, e.g.::
1237+
1238+
assert Literal[Literal[1, 2], 3] == Literal[1, 2, 3]
1239+
1240+
However, this does not apply to ``Literal`` types referenced through a type
1241+
alias, to avoid forcing evaluation of the underlying :class:`TypeAliasType`::
1242+
1243+
type A = Literal[1, 2]
1244+
assert Literal[A, 3] != Literal[1, 2, 3]
1245+
1246+
* Redundant arguments are skipped, e.g.::
1247+
1248+
assert Union[1, 2, 1] == Union[1, 2]
1249+
1250+
* When comparing literals, the argument order is ignored, e.g.::
1251+
1252+
assert Literal[1, 2] == Literal[2, 1]
1253+
1254+
* You cannot subclass or instantiate a ``Literal``.
1255+
1256+
* You cannot write ``Literal[X][Y]``.
1257+
12251258
.. versionadded:: 3.8
12261259

12271260
.. versionchanged:: 3.9.1

Lib/typing.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,13 @@ def Union(self, parameters):
765765
766766
assert Union[Union[int, str], float] == Union[int, str, float]
767767
768+
However, this does not apply to unions referenced through a type
769+
alias, to avoid forcing evaluation of the underlying
770+
TypeAliasType::
771+
772+
type A = Union[int, str]
773+
assert Union[A, float] != Union[int, str, float]
774+
768775
- Unions of a single argument vanish, e.g.::
769776
770777
assert Union[int] == int # The constructor actually returns int
@@ -830,6 +837,33 @@ def open_helper(file: str, mode: MODE) -> str:
830837
Literal[...] cannot be subclassed. At runtime, an arbitrary value
831838
is allowed as type argument to Literal[...], but type checkers may
832839
impose restrictions.
840+
841+
Literal is very similar to Union, the main difference is that its
842+
arguments are literal values instead of types:
843+
844+
- The arguments must be literal values and there must be at least one.
845+
846+
- Nested Literal types are flattened, e.g.::
847+
848+
assert Literal[Literal[1, 2], 3] == Literal[1, 2, 3]
849+
850+
However, this does not apply to Literal types referenced through a type
851+
alias, to avoid forcing evaluation of the underlying TypeAliasType::
852+
853+
type A = Literal[1, 2]
854+
assert Literal[A, 3] != Literal[1, 2, 3]
855+
856+
- Redundant arguments are skipped, e.g.::
857+
858+
assert Union[1, 2, 1] == Union[1, 2]
859+
860+
- When comparing literals, the argument order is ignored, e.g.::
861+
862+
assert Literal[1, 2] == Literal[2, 1]
863+
864+
- You cannot subclass or instantiate a Literal.
865+
866+
- You cannot write Literal[X][Y].
833867
"""
834868
# There is no '_type_check' call because arguments to Literal[...] are
835869
# values, not types.

0 commit comments

Comments
 (0)