Skip to content

Commit 5b08cac

Browse files
craymichaelfacebook-github-bot
authored andcommitted
Reduce complexity of logging try-catch + add typing
Summary: 'TryExcept 6' is too complex (13) See https://www.flake8rules.com/rules/C901.html Differential Revision: D64511308
1 parent ea876e1 commit 5b08cac

File tree

2 files changed

+65
-43
lines changed

2 files changed

+65
-43
lines changed

captum/log/__init__.py

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python3
22

33
# pyre-strict
4-
from typing import Any
54

65
try:
76
from captum.log.fb.internal_log import (
@@ -19,49 +18,16 @@
1918
"TimedLog",
2019
"set_environment",
2120
"disable_detailed_logging",
21+
"patch_methods",
2222
]
2323

2424
except ImportError:
25-
from functools import wraps
26-
27-
def log(*args: Any, **kwargs: Any) -> None: # type: ignore
28-
pass
29-
3025
# bug with mypy: https://github.com/python/mypy/issues/1153
31-
class TimedLog: # type: ignore
32-
# pyre-fixme[2]: Parameter must be annotated.
33-
def __init__(self, *args, **kwargs) -> None:
34-
pass
35-
36-
def __enter__(self) -> "TimedLog":
37-
return self
38-
39-
# pyre-fixme[2]: Parameter must be annotated.
40-
def __exit__(self, exception_type, exception_value, traceback) -> bool:
41-
return exception_value is not None
42-
43-
# pyre-fixme[3]: Return type must be annotated.
44-
def log_usage(*log_args: Any, **log_kwargs: Any):
45-
# pyre-fixme[3]: Return type must be annotated.
46-
# pyre-fixme[2]: Parameter must be annotated.
47-
def _log_usage(func):
48-
@wraps(func)
49-
# pyre-fixme[53]: Captured variable `func` is not annotated.
50-
# pyre-fixme[3]: Return type must be annotated.
51-
def wrapper(*args: Any, **kwargs: Any):
52-
return func(*args, **kwargs)
53-
54-
return wrapper
55-
56-
return _log_usage
57-
58-
# pyre-fixme[2]: Parameter must be annotated.
59-
def set_environment(env) -> None: # type: ignore
60-
pass
61-
62-
def disable_detailed_logging() -> None:
63-
pass
64-
65-
# pyre-fixme[2]: Parameter must be annotated.
66-
def patch_methods(tester, patch_log: bool = True) -> None: # type: ignore
67-
pass
26+
from captum.log.dummy_log import ( # type: ignore
27+
disable_detailed_logging,
28+
log,
29+
log_usage,
30+
patch_methods,
31+
set_environment,
32+
TimedLog,
33+
)

captum/log/dummy_log.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
3+
# pyre-strict
4+
5+
from functools import wraps
6+
from types import TracebackType
7+
from typing import Any, Callable, List, Optional, Union
8+
9+
10+
def log(*args: Any, **kwargs: Any) -> None:
11+
pass
12+
13+
14+
class TimedLog:
15+
def __init__(self, *args: Any, **kwargs: Any) -> None:
16+
pass
17+
18+
def __enter__(self) -> "TimedLog":
19+
return self
20+
21+
def __exit__(
22+
self,
23+
exception_type: Optional[BaseException],
24+
exception_value: Optional[BaseException],
25+
traceback: Optional[TracebackType],
26+
) -> bool:
27+
return exception_value is not None
28+
29+
30+
# pyre-ignore[3]: Return type cannot contain Any.
31+
def log_usage(
32+
*log_args: Any, **log_kwargs: Any
33+
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
34+
# pyre-ignore[2]: Callable cannot return Any.
35+
# pyre-ignore[3]: Return type cannot contain Any.
36+
def _log_usage(func: Callable[..., Any]) -> Callable[..., Any]:
37+
@wraps(func)
38+
# pyre-ignore[3]: Return type cannot contain Any.
39+
def wrapper(*args: Any, **kwargs: Any) -> Any:
40+
return func(*args, **kwargs)
41+
42+
return wrapper
43+
44+
return _log_usage
45+
46+
47+
def set_environment(env: Union[None, List[str], str]) -> None:
48+
pass
49+
50+
51+
def disable_detailed_logging() -> None:
52+
pass
53+
54+
55+
def patch_methods(_, patch_log: bool = True) -> None:
56+
pass

0 commit comments

Comments
 (0)