You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: peps/pep-0705.rst
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -140,7 +140,7 @@ The ``movie_string`` function in the first motivating example can then be typed
140
140
else:
141
141
return f'{movie["name"]} ({movie["year"]})'
142
142
143
-
A mixture of read-only and non-read-only items is permitted, allowing the second motivating example to be correctly type-hinted::
143
+
A mixture of read-only and non-read-only items is permitted, allowing the second motivating example to be correctly annotated::
144
144
145
145
class HasTimestamp(TypedDict):
146
146
timestamp: float
@@ -183,7 +183,7 @@ The ``typing.ReadOnly`` type qualifier is used to indicate that an item declared
183
183
name: str
184
184
members: ReadOnly[list[str]]
185
185
186
-
blur: Band = {"name": "blur", members: []}
186
+
blur: Band = {"name": "blur", "members": []}
187
187
blur["name"] = "Blur" # OK: "name" is not read-only
188
188
blur["members"] = ["Damon Albarn"] # Type check error: "members" is read-only
189
189
blur["members"].append("Damon Albarn") # OK: list is mutable
@@ -226,7 +226,7 @@ Subclasses can redeclare read-only items as non-read-only, allowing them to be m
226
226
name: str
227
227
year: int
228
228
229
-
album: Album = { name: "Flood", year: 1990 }
229
+
album: Album = { "name": "Flood", "year": 1990 }
230
230
album["year"] = 1973
231
231
album["name"] = "Dark Side Of The Moon" # OK: "name" is not read-only in Album
232
232
@@ -235,7 +235,7 @@ If a read-only item is not redeclared, it remains read-only::
235
235
class Album(NamedDict):
236
236
year: int
237
237
238
-
album: Album = { name: "Flood", year: 1990 }
238
+
album: Album = { "name": "Flood", "year": 1990 }
239
239
album["name"] = "Dark Side Of The Moon" # Type check error: "name" is read-only in Album
240
240
241
241
Subclasses can narrow value types of read-only items::
@@ -282,7 +282,7 @@ A TypedDict type ``A`` is consistent with TypedDict ``B`` if ``A`` is structural
282
282
283
283
Discussion:
284
284
285
-
* All non-specified items in a TypedDict implicitly have value type ``ReadOnly[NotRequired[Any]]`` (or ``ReadOnly[NotRequired[Unknown]]`` in pyright).
285
+
* All non-specified items in a TypedDict implicitly have value type ``ReadOnly[NotRequired[object]]``.
286
286
287
287
* Read-only items behave covariantly, as they cannot be mutated. This is similar to container types such as ``Sequence``, and different from non-read-only items, which behave invariantly. Example::
288
288
@@ -298,7 +298,7 @@ Discussion:
298
298
b: B = {"x": 1}
299
299
f(b) # Accepted by type checker
300
300
301
-
* A TypedDict type ``A`` with no explicit key ``'x'`` is not consistent with a TypedDict type ``B`` with a non-required key ``'x'``, since at runtime the key ``'x'`` could be present and have an incompatible type (which may not be visible through ``A`` due to structural subtyping). The only exception to this rule is if the item in ``B`` is read-only, and the value type is a top type (``object``, ``Any`` or pylance's ``Unknown``). For example::
301
+
* A TypedDict type ``A`` with no explicit key ``'x'`` is not consistent with a TypedDict type ``B`` with a non-required key ``'x'``, since at runtime the key ``'x'`` could be present and have an incompatible type (which may not be visible through ``A`` due to structural subtyping). The only exception to this rule is if the item in ``B`` is read-only, and the value type is of top type (``object``). For example::
302
302
303
303
class A(TypedDict):
304
304
x: int
@@ -330,7 +330,7 @@ The merge operation (``d1 | d2``) creates a new dictionary with the merged keys
330
330
331
331
Type checkers should conform to the following rules, even if they are not expressed correctly in the typeshed due to language constraints.
332
332
333
-
If the merger of two TypedDict objects of type ``A`` and ``B`` are assigned to a TypedDict of type ``C``, the type checker should verify that:
333
+
If the merger of two TypedDict objects of type ``A`` and ``B`` is assigned to a TypedDict of type ``C``, the type checker should verify that:
334
334
335
335
* for each key in ``C``, one of the following holds:
336
336
@@ -405,7 +405,7 @@ If a shallow copy of TypedDict type ``A`` is assigned to TypedDict type ``B``, t
405
405
406
406
* for each key in ``B``, one of the following holds:
407
407
408
-
* the associated value type in of top type
408
+
* the associated value type is of top type
409
409
* the key is in ``A``, and the associated value type in ``A`` is consistent with the value type in ``B``
410
410
411
411
* if a key is required in ``B``, it is required in ``A``
@@ -414,7 +414,7 @@ If a deep copy of TypedDict type ``A`` is assigned to TypedDict type ``B``, the
414
414
415
415
* for each key in ``B``, one of the following holds:
416
416
417
-
* the associated value type in of top type
417
+
* the associated value type is of top type
418
418
* the key is in ``A``, and a deep copy of the associated value type in ``A`` could be assigned to the value type in ``B``
419
419
420
420
* if a key is required in ``B``, it is required in ``A``
0 commit comments