Skip to content

Instantly share code, notes, and snippets.

@himanoa
Created January 19, 2017 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save himanoa/6839502e86c5655c71f5c2ff7817bcf4 to your computer and use it in GitHub Desktop.
Save himanoa/6839502e86c5655c71f5c2ff7817bcf4 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from typing import *
from functools import wraps
def exception(f: Callable[[Any], Any]) -> Callable[[Any], Union[Exception, Any]]:
@wraps(f)
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
return e
return wrapper
@exception
def hoge(fuga: int) -> Union[Exception, int]:
if fuga == 3:
raise Exception("だめだよ")
return fuga
if __name__ == '__main__':
a = hoge(3)
a = a + 3
# mypy fuck_try_catch.py
# testing.py:23: error: Unsupported operand types for + ("Union[Exception, Any]" and "int")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment