I use TypedDict with JSON so I get good hints in my IDE when dealing with complex JSON structures coming in from web APIs. This includes hints of key names as well as methods to use with the types of data in the structure. from typing import List, TypedDict class SubDataTypedDict(TypedDict): id: int name: str widgets: List[int] class DataTypedDict(TypedDict): id: int subDatas: List[SubDataTypedDict] foo: str def data_doer(data: DataTypedDict): # using `data` in this function will give me completion on key names # as well as being aware of the types of the values and giving me # completion on relevant methods. On Wed, May 20, 2020 at 2:52 PM Luciano Ramalho <luciano@ramalho.org> wrote:
I hope you're all well.
I am writing the 2nd edition of Fluent Python and right now I am trying to wrap up a section on typing.TypedDict, but I am having a very hard time understanding its value in real world scenarios.
PEP 589 acknowledges that JSON is a "canonical use case" for dicts used as records. But since the whole point of a TypedDict is to provide syntax for type hints, I can't find a connection or any benefit of using TypedDict with code that deals with JSON—where all the action is at runtime.
All the examples in the PEP use literal dicts as values when assigning to variables annotated with a TypedDict type.
If I annotate a function parameter with a TypedDict, and I am forced to spell out all the keys to provide a dict argument that can be type checked, I don't see why not use keyword arguments in the function definition in the first place...
My testing shows that Mypy is unable to do much validation beyond assignment from dict displays or dict constructor calls with keyword arguments.
More realistic use cases that I was able to imagine often need casts and don't perform any useful static checking.
My future readers and I could really use more insight about real world use cases where TypedDict adds value.
Thanks for any help!
Cheers,
Luciano
-- Luciano Ramalho | Author of Fluent Python (O'Reilly, 2015) | http://shop.oreilly.com/product/0636920032519.do | Technical Principal at ThoughtWorks | Twitter: @ramalhoorg _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: dustin.wyatt@gmail.com