4 ms·
> The typing module incorporates several new features: > A dictionary type with per-key types. Ah, I've been waiting for this. I've been able to use Pytho
by ohazi 7y ago
> The typing module incorporates several new features:
> A dictionary type with per-key types.
Ah, I've been waiting for this. I've been able to use Python's optional types pretty much everywhere except for dictionaries that are used as pseudo-objects, which is a fairly common pattern in Python. This should patch that hole nicely.
- j88439h84 7y agoIMO A better fix is to use http://attrs.org http://attrs.org or dataclasses to replace the dicts entirely.
- deleted 7y ago[deleted]
- emddudley 7y agoAgreed--if you're going to go through the trouble of adding detailed type hints to describe a dict, you're like 90% of the way to a dataclass with better usability.
- ben509 7y agoAnd then use a library like json-syntax[1] to translate those into JSON. [1]: https://pypi.org/project/json-syntax/ https://pypi.org/project/json-syntax/
- ledauphin 7y agothis looks interesting. we use cattrs, which has worked well for us but has a long-delayed 1.0 release.
- hermitdev 7y agoOne of the features I'm looking forward to using is the kwdargs support in dataclasses. Honestly, find it so much more useful and intuitive than the 3.7 positional only.
- ledauphin 7y agoi am actually pretty sad that TypedDict has made it out of typing_extensions in its current state. It was a major missed opportunity to provide a properly duck-typed Dict type, which by definition would allow untyped key-value pairs to be added to a "partially-typed" dictionary. Gradual typing in general is a massive win for many kinds of real-world problem solving, but when you make it as hard as Python has to introduce partial types to a plain data object, you're leaving a lot of developers out in the cold. I love MyPy and the static type hints since 3.6, but structural subtyping is superior and so obviously more Pythonic than nominal, yet support for structural subtyping keeps lagging behind.