Reputation: 469
I have written my own decorator add_warning
in order to print costom error messages when certain errors occur. The decorator takes a message and the type of errors for which to print this message. I would also like to add typing to this decorator and check it using mypy
. This works well in the case where I am just throwing a plain Exception
when I use Type[Exception]
. However, mypy
complains when I use other errors such as OSError
or AttributeError
by saying:
error: Argument "errors" to "add_warning" has incompatible type "Tuple[Type[OSError], Type[AttributeError]]"; expected "Union[str, Type[Exception], Tuple[Type[Any]]]"
.
Does anybody know if there is a better way than to use Any
or Tuple[Type[OSError], Type[AttributeError]]
here? Specifically, is there a more general type for all Python errors?
Below is the code:
from functools import wraps
from typing import Union, Tuple, Callable, Type
def add_warning(message: str, flag: str = 'Info',
errors: Union[str, Type[Exception], Tuple[Type[Exception]]] = 'all') -> Callable:
"""
Add a warning message to a function, when certain error types occur.
"""
if errors == 'all':
errors = Exception
def decorate(func: Callable):
@wraps(func)
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
except errors:
warn(message, flag)
return []
else:
return result
return wrapper
return decorate
def warn(message: str, flag: str = 'Info') -> None:
"""Print the colored warning message."""
print(f"{flag}: {message}")
if __name__ == '__main__':
@add_warning('This is another test warning.', flag='Error')
def test_func1():
raise Exception
@add_warning('This is a test warning.', flag='Error', errors=(OSError, AttributeError))
def test_func2():
raise OSError
test_func1()
test_func2()
Upvotes: 2
Views: 2008
Reputation: 96236
The issue is that Tuple[Type[Exception]
means a tuple with a single value. You want a variable-sized tuple, so use ellipses: Tuple[Type[Exception], ...]
The following works without mypy complaining:
from functools import wraps
from typing import Union, Tuple, Callable, Type
def add_warning(message: str, flag: str = 'Info',
errors: Union[str, Type[Exception], Tuple[Type[Exception], ...]] = 'all') -> Callable:
"""
Add a warning message to a function, when certain error types occur.
"""
if errors == 'all':
errors = Exception
def decorate(func: Callable):
@wraps(func)
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
except errors:
warn(message, flag)
return []
else:
return result
return wrapper
return decorate
def warn(message: str, flag: str = 'Info') -> None:
"""Print the colored warning message."""
print(f"{flag}: {message}")
if __name__ == '__main__':
@add_warning('This is another test warning.', flag='Error')
def test_func1():
raise Exception
@add_warning('This is a test warning.', flag='Error', errors=(OSError, AttributeError))
def test_func2():
raise OSError
test_func1()
test_func2()
Upvotes: 2