Is there a way to get keys defined in TypedDict
in order? Is it also possible to do so retaining the Literal
keys information?
Example:
class PersonDetail(TypedDict, total=True):
name: ReadOnly[str]
age: ReadOnly[int]
occupation: ReadOnly[str]
HEADER = ("name", "age", "occupation")
def write_csv(details: dict[str, PersonDetail], outpath: Path):
with outpath.open("w") as op:
writer = csv.DictWriter(op, HEADER)
writer.writeheader()
for position, detail in details.items():
do_something_with_position(position)
writer.writerow(detail)
I would like to eliminate the extra HEADER
definition. I want a method/function/attribute like the following:
reveal_type(magic_func(PersonDetail)) # tuple[Literal["name"], Literal["age"], Literal["occupation"]]
Is there a way to do this? It seems like it should be possible given the nature of TypedDict
s.