2

I'm trying to annotate a python codebase, and I have a piece of code conceptually similar to this:

def could_fail_on_bad_input(value):
    if isinstance(value, ValidType):
        ...
        return transformed_value
    else:
        raise TypeError("Unsupported type")

I've checked Python's typing module, typing-extensions, and mypy, not to mention SO, but couldn't find a proper way to describe the return type of this function.

So far, the best idea I had is to represent the output type as Union[NoReturn, ValidType], but I have a few issues with this. First, NoReturn means that the function doesn't return at all. Second, NoReturn feels too generic to me because I want to specify the exact error type this function can raise.

Note that I'm looking for a type hinting/annotation solution, not for a custom class to represent this.

0 Answers0