Twitter user @DanyX23:

TIL: pyright, the python type checking engine that is used by VS Code, has support for exhaustiveness checking for match statements with union types!

If you add the following to your pyproject.toml, you’ll get the attached warning

[tool.pyright] reportMatchNotExhaustive = true

You are viewing a single thread.
View all comments View context
4 points
*

Same goes for the try/catch exception system where runtime errors can pop up bc you don’t have to handle exceptions

I see three possible ways to go about errors:

  1. Make the programmer actually handle (not just bubble up) all possible errors, which will at some point break abstractions and make writing any piece of code an exhausting task.
  2. Make it easy for programmers to ignore errors silently. The worst option IMO as it risks undefined behavior and creates bugs that are hard to replicate.
  3. Make it easy to surface errors to the caller and halt the execution if unhandled. This is what Python exceptions do and I believe to be the best compromise. Bubbling up errors is given by the language and does not depend on the programmer’s willingness to check for errors like in Go (also no err != nil checks all over the codebase).

The criticism I have to Python exceptions is that there should be a way to explicitly see the exceptions a function raises, maybe as part of the function signature like in Java.

To put it another way, I don’t think errors as returned values are immediately better than exceptions. I like the way Rust handles them for instance, but an unsuccessful .unwrap() is not that different from an unhandled exception. I just wish the possible exceptions raised were explicitly stated.

permalink
report
parent
reply

Python

!python@programming.dev

Create post

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events
Past

November 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
  • Pythörhead: a Python library for interacting with Lemmy
  • Plemmy: a Python package for accessing the Lemmy API
  • pylemmy pylemmy enables simple access to Lemmy’s API with Python
  • mastodon.py, a Python wrapper for the Mastodon API
Feeds

Community stats

  • 709

    Monthly active users

  • 428

    Posts

  • 2.2K

    Comments