PEP 589: TypedDict field cannot be overwritten by inheritance (#1143)

This commit is contained in:
Sergey 2019-08-11 21:21:31 +03:00 committed by Guido van Rossum
parent 19b5d9586a
commit 718fb74916
1 changed files with 28 additions and 0 deletions

View File

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