What if function definitions could tell the caller "I raise Exceptions"?

Similar to Swift, I think it would add to the expressiveness of the language to be able to type-hint a function which might raise any type of Exception. For example:

def login(username: str, password: str) -> User raises:
    resp = requests.post("/login", {
        "user": username,
        "password": password,
    })
    resp.raise_for_status()
    return User(**resp.json())

Describes a function login which normally returns a User object, but may also raise an Exception.

Additionally, what if specific types of Exceptions could be returned?

def login(username: str, password: str) -> User raises ValidationError:
    try:
        resp = requests.post("/login", {
            "user": username,
            "password": password,
        })
        resp.raise_for_status()
    except Exception as e:
        # Recursively retry the request
        return login(username, password)
    # Return a User, but raise a ValidationError if the data doesn't conform to the User model
    return User(**resp.json())

I am curious what others think of this? Would this make the language better?

Most recently discussed last month Extend type hints to cover exceptions, as well as numerous times before that over the years. The answer is pretty definitively “not going to happen” at this point.

5 Likes

One thing I didn’t see in that thread is a discussion of the Nim “effect” system, which includes statically-checked exceptions among other things. Nim is a highly “procedural/imperative” language (with Python-inspired syntax), which seems to have learned good lessons from the maligned “checked exceptions” in Java, and in my opinion would be a good starting point for a design discussion of checked exceptions in Python.

I agree that it would be a great idea, but someone would probably need to implement it as a third-party Mypy plugin and a decorator. I don’t know how feasible this is to do, because I don’t know enough about writing Mypy plugins.