logoalt Hacker News

codethiefyesterday at 11:17 AM5 repliesview on HN

Next step: Auto-inferring the correct (most narrow) TypedDict type from a frozendict. (Think `const foo = { … } as const` in TypeScript.) I miss this TS feature in Python on the daily.


Replies

mrweaselyesterday at 12:03 PM

What would that do exactly? Auto-generate a TypedDict class?

Bringing up TypedDict is sort of interesting, because it seems like a frozen dictionary could have been implemented by PEP 705, if typing.ReadOnly was enforced at runtime, and not just a hint to a static type checker.

show 1 reply
adzmyesterday at 11:56 AM

Curious what this means for typescript as well; honestly I've only reached for as const rarely.

anentropicyesterday at 12:43 PM

Agreed... but it shouldn't need a frozendict for this

IMHO TypedDict in Python are essentially broken/useless as is

What is needed is TS style structural matching, like a Protocol for dicts

virtue3yesterday at 11:32 AM

I also SUPER LOVE this feature. Especially when you make a type union of the keys for easier indexing.

tweakimpyesterday at 11:28 AM

Can you give a Python example of a use case for this?