I am trying to accomplish the below (see mypy playground):
from typing import TypedDict, Final
account_schema: Final = {"name": str, "email": str}
Account = TypedDict("Account", account_schema)
AccountPatch = TypedDict("AccountPatch", account_schema, total=False)
Idea is that I can have my schema specified in one place, with a version requiring all fields (Account
, when inserting in database) and a version making all fields optional (AccountPatch
, when updating a database).
From PEP 586:
The
Final
qualifier serves as a shorthand for declaring that a variable is effectivelyLiteral
.
But mypy
errors with:
error: TypedDict() expects a dictionary literal as the second argument
Why does TypedDict
not allow a Final
dictionary as its second argument?
For my core problem, is there anyway I can use the same schema for two TypedDict
s (one with totality, one with no totality) without having to duplicate the schema?