PEP 705: Clean up examples in Inheritance section (#3495)
Clean up examples in Inheritance section
This commit is contained in:
parent
ad131c6132
commit
acecb12b9c
|
@ -312,20 +312,28 @@ Inheritance
|
|||
|
||||
To avoid potential confusion, it is an error to have a read-only type extend a non-read-only type::
|
||||
|
||||
class BandAndAlbum(TypedDict):
|
||||
band: str
|
||||
album: ReadOnly[Album]
|
||||
|
||||
class BandAlbumAndLabel(BandAndAlbum, readonly=True): # Runtime error
|
||||
label: str
|
||||
|
||||
|
||||
It is also an error to have a type without ``other_keys`` specified extend a type with ``other_keys=Never``::
|
||||
|
||||
class NamedDict(TypedDict, readonly=True):
|
||||
class Building(TypedDict, other_keys=Never):
|
||||
name: str
|
||||
address: str
|
||||
|
||||
class Person(NamedDict): # Runtime error
|
||||
age: float
|
||||
class Museum(Building): # Runtime error
|
||||
pass
|
||||
|
||||
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::
|
||||
|
||||
class NamedDict(TypedDict, readonly=True):
|
||||
name: str
|
||||
|
||||
class Album(NamedDict, TypedDict):
|
||||
year: int
|
||||
|
||||
|
|
Loading…
Reference in New Issue