4 ms·
I was surprised to find out that MyPy will throw an error if you use a variable to access a key (https://github.com/python/mypy/issues/7178 https://github.com/p
by avremel 7y ago
I was surprised to find out that MyPy will throw an error if you use a variable to access a key (https://github.com/python/mypy/issues/7178 https://github.com/python/mypy/issues/7178).
Optional keys are also a nuisance.
- dharmab 7y agoHow are optional keys a nuisance? I find that using the patterns: if "optional_key" in my_dict: do_something_with(my_dict["optional_key"]) Or: optional_value = my_dict.get("optional_key") if optional_value: do_something_with(optional value) Works as expected. Nested optional keys can be a bit annoying, although: my_dict.get("optional_parent", {}).get("optional_child") seems to work.
- avremel 7y agoI was referring to defining optional keys. If you have a dict with some keys which are optional, you need to create a separate subclass (with `total=False`) just for those optional keys. With TypeScript, I can just use `key?: type`. https://mypy.readthedocs.io/en/latest/more_types.html#mixing-required-and-non-required-items https://mypy.readthedocs.io/en/latest/more_types.html#mixing...