3 ms·
It has warnings that steer you away from idiomatic Python towards less idiomatic Python. Idiomatic Python favours EAFP (Easier to Ask Forgiveness than Permissi
by JimDabell 2y ago
It has warnings that steer you away from idiomatic Python towards less idiomatic Python.
Idiomatic Python favours EAFP (Easier to Ask Forgiveness than Permission) over LBYL (Look Before You Leap), so you might write something like this when using TypedDicts:
try:
foo = bar["baz"]["qux"]
...
except KeyError:
...
Pyright warns you about this and pushes you to write this:
if "baz" in bar and "qux" in bar["baz"]:
foo = bar["baz"]["qux"]
...
else:
...
They identified this as a bug and fixed it, then changed course and decided to reinstate the behaviour. So if you want to write idiomatic Python, you need to disable typechecking TypedDicts.
— https://github.com/microsoft/pyright/issues/1739 https://github.com/microsoft/pyright/issues/1739
Now if this were a linter then I would understand. But a type checker should not be opinionated to the point of pushing people not to use idiomatic Python.
- NeutralForest 2y agoFor maximum safety, I would write: if bar.get("baz", {}).get("qux") is not None: ...
- nikisweeting 2y agoany more than 2 levels deep of this pattern and I reach for python-benedict
- NeutralForest 2y agoNice, I didn't know about that lib! Usually if I have very nested content I recursively parse it and I'll have some business logic in the function to handle cases here and there. Or I'll have a validation library like Pydantic.