Skip to content

Commit acecb12

Browse files
authored
PEP 705: Clean up examples in Inheritance section (#3495)
Clean up examples in Inheritance section
1 parent ad131c6 commit acecb12

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

peps/pep-0705.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,20 +312,28 @@ Inheritance
312312

313313
To avoid potential confusion, it is an error to have a read-only type extend a non-read-only type::
314314

315+
class BandAndAlbum(TypedDict):
316+
band: str
317+
album: ReadOnly[Album]
318+
315319
class BandAlbumAndLabel(BandAndAlbum, readonly=True): # Runtime error
316320
label: str
317321

318322

319323
It is also an error to have a type without ``other_keys`` specified extend a type with ``other_keys=Never``::
320324

321-
class NamedDict(TypedDict, readonly=True):
325+
class Building(TypedDict, other_keys=Never):
322326
name: str
327+
address: str
323328

324-
class Person(NamedDict): # Runtime error
325-
age: float
329+
class Museum(Building): # Runtime error
330+
pass
326331

327332
It is valid to have a non-read-only type extend a read-only one. The subclass will not be read-only, but any keys not redeclared in the subclass will remain read-only::
328333

334+
class NamedDict(TypedDict, readonly=True):
335+
name: str
336+
329337
class Album(NamedDict, TypedDict):
330338
year: int
331339

0 commit comments

Comments
 (0)