PEP 705: Clean up examples in Inheritance section (#3495)

Clean up examples in Inheritance section
This commit is contained in:
Alice 2023-10-18 20:09:45 +01:00 committed by GitHub
parent ad131c6132
commit acecb12b9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 3 deletions

View File

@ -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