Search code examples
pythonpython-3.xerror-handlingtypingcode-documentation

Can I determine what errors a function will raise from typing?


Let's say I have the function:

def foo(integer: int) -> int:
    if integer < 0:
        raise ValueError("The integer must be positive")

    return integer * 2

If I look at the signature for this function, it will only tell me that it takes an int and returns an int.

However, it would be really nice to know that it also has the chance of raising a ValueError in case I need to catch that.

Is there any way to put what errors a function raises into the types, or is this just something that should go into the docstring?


Solution

  • Unfortunately, no. Docstrings would be a good practice.

    Also: Python type hinting with exceptions