PEP 589: TypedDict field cannot be overwritten by inheritance (#1143)
This commit is contained in:
parent
19b5d9586a
commit
718fb74916
28
pep-0589.rst
28
pep-0589.rst
|
@ -274,6 +274,34 @@ The TypedDict ``XYZ`` has three items: ``x`` (type ``int``), ``y``
|
||||||
A TypedDict cannot inherit from both a TypedDict type and a
|
A TypedDict cannot inherit from both a TypedDict type and a
|
||||||
non-TypedDict base class.
|
non-TypedDict base class.
|
||||||
|
|
||||||
|
Additional notes on TypedDict class inheritance:
|
||||||
|
|
||||||
|
* Changing a field type of a parent TypedDict class in a subclass is not allowed.
|
||||||
|
Example::
|
||||||
|
|
||||||
|
class X(TypedDict):
|
||||||
|
x: str
|
||||||
|
|
||||||
|
class Y(X):
|
||||||
|
x: int # Type check error: cannot overwrite TypedDict field "x"
|
||||||
|
|
||||||
|
In the example outlined above TypedDict class annotations returns
|
||||||
|
type ``str`` for key ``x``::
|
||||||
|
|
||||||
|
print(Y.__annotations__) # {'x': <class 'str'>}
|
||||||
|
|
||||||
|
|
||||||
|
* Multiple inheritance does not allow conflict types for the same name field::
|
||||||
|
|
||||||
|
class X(TypedDict):
|
||||||
|
x: int
|
||||||
|
|
||||||
|
class Y(TypedDict):
|
||||||
|
x: str
|
||||||
|
|
||||||
|
class XYZ(X, Y): # Type check error: cannot overwrite TypedDict field "x" while merging
|
||||||
|
xyz: bool
|
||||||
|
|
||||||
|
|
||||||
Totality
|
Totality
|
||||||
--------
|
--------
|
||||||
|
|
Loading…
Reference in New Issue