@@ -1098,6 +1098,12 @@ These can be used as types in annotations. They all support subscription using
1098
1098
1099
1099
Union[Union[int, str], float] == Union[int, str, float]
1100
1100
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
+
1101
1107
* Unions of a single argument vanish, e.g.::
1102
1108
1103
1109
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
1230
1236
is allowed as type argument to ``Literal[...] ``, but type checkers may
1231
1237
impose restrictions. See :pep: `586 ` for more details about literal types.
1232
1238
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
+
1233
1265
.. versionadded :: 3.8
1234
1266
1235
1267
.. versionchanged :: 3.9.1
@@ -1400,6 +1432,14 @@ These can be used as types in annotations. They all support subscription using
1400
1432
int, ValueRange(3, 10), ctype("char")
1401
1433
]
1402
1434
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
+
1403
1443
Duplicated metadata elements are not removed::
1404
1444
1405
1445
assert Annotated[int, ValueRange(3, 10)] != Annotated[
0 commit comments