← Back to context

Comment by IshKebab

1 day ago

Well I agree with them. The second code is clearly better. Exceptions should be used for error handling and if those keys are actually optional then you should explicitly check if they exist (or use something like `bar.get("baz")`).

You are welcome to that opinion, but type checkers should not be opinionated, especially if they push people to write non-idiomatic Python. If you think this should be written a different way, that’s what lint rules are for.

  • Actually Python type checkers must be opinionated. Python doesn't define the semantics of type annotations so type checkers have to choose them.

    They don't always choose the same options, so some Python code may type check in one type checker and not in another.

    Yes this is a dumb situation but that's how it is. So Pyright has to make a choice here, and they chose the most sensible option.

    You're free to disagree of course.