I am trying to create a few functions which will return values of different TypedDict types. Most of fields in them will be same so I want to generate base dictionary with same function in all cases. However I am getting stumped by typing this correctly.
My idea was to create base type Parent
and inherit from it, adding only NotRequired
fields.
from typing_extensions import NotRequired, TypedDict
class Parent(TypedDict):
parent_field: str
class Child(Parent):
child_field: NotRequired[bool | None]
def create_parent() -> Parent:
return {"parent_field": "example"}
child: Child = create_parent()
# Error:
# Expression of type "Parent" cannot be assigned to declared type "Child"
# "child_field" is missing from "Type[Parent]"
However this fails since field child_field
is missing, even though its type is NotRequired
. Why it fails and how to evade this problem?
EDIT: I am using pylance (so pyright) for typechecking.
mypy
(playground) gives similar error message:
Incompatible types in assignment (expression has type "Parent", variable has type "Child") [assignment]