Skip to content

Commit 8dc48a5

Browse files
craymichaelfacebook-github-bot
authored andcommitted
Reduce complexity of logging try-catch + add typing (#1380)
Summary: Pull Request resolved: #1380 'TryExcept 6' is too complex (13) See https://www.flake8rules.com/rules/C901.html Reviewed By: jjuncho Differential Revision: D64511308 fbshipit-source-id: c42173f6e7ea4b8732141640f4886b3e347f599a
1 parent 0f2170a commit 8dc48a5

File tree

2 files changed

+64
-43
lines changed

2 files changed

+64
-43
lines changed

captum/log/__init__.py

+9-43
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

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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, 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-fixme[3]: Return type must be annotated.
31+
def log_usage(*log_args: Any, **log_kwargs: Any):
32+
# pyre-fixme[3]: Return type must be annotated.
33+
# pyre-fixme[2]: Parameter must be annotated.
34+
def _log_usage(func):
35+
@wraps(func)
36+
# pyre-fixme[53]: Captured variable `func` is not annotated.
37+
# pyre-fixme[3]: Return type must be annotated.
38+
def wrapper(*args: Any, **kwargs: Any):
39+
return func(*args, **kwargs)
40+
41+
return wrapper
42+
43+
return _log_usage
44+
45+
46+
def set_environment(env: Union[None, List[str], str]) -> None:
47+
pass
48+
49+
50+
def disable_detailed_logging() -> None:
51+
pass
52+
53+
54+
def patch_methods(_, patch_log: bool = True) -> None:
55+
pass

0 commit comments

Comments
 (0)