From 718fb7491648ac5a1fb6eb22916d8aecf450aab4 Mon Sep 17 00:00:00 2001 From: Sergey Date: Sun, 11 Aug 2019 21:21:31 +0300 Subject: [PATCH] PEP 589: TypedDict field cannot be overwritten by inheritance (#1143) --- pep-0589.rst | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pep-0589.rst b/pep-0589.rst index 94f7c2fd8..a007adfa1 100644 --- a/pep-0589.rst +++ b/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 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': } + + +* 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 --------