Skip to content

Commit

Permalink
Reduce complexity of logging try-catch + add typing
Browse files Browse the repository at this point in the history
Summary: 'TryExcept 6' is too complex (13) See https://www.flake8rules.com/rules/C901.html

Differential Revision: D64511308
  • Loading branch information
craymichael authored and facebook-github-bot committed Oct 17, 2024
1 parent ea876e1 commit 5b08cac
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 43 deletions.
52 changes: 9 additions & 43 deletions captum/log/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3

# pyre-strict
from typing import Any

try:
from captum.log.fb.internal_log import (
Expand All @@ -19,49 +18,16 @@
"TimedLog",
"set_environment",
"disable_detailed_logging",
"patch_methods",
]

except ImportError:
from functools import wraps

def log(*args: Any, **kwargs: Any) -> None: # type: ignore
pass

# bug with mypy: https://github.com/python/mypy/issues/1153
class TimedLog: # type: ignore
# pyre-fixme[2]: Parameter must be annotated.
def __init__(self, *args, **kwargs) -> None:
pass

def __enter__(self) -> "TimedLog":
return self

# pyre-fixme[2]: Parameter must be annotated.
def __exit__(self, exception_type, exception_value, traceback) -> bool:
return exception_value is not None

# pyre-fixme[3]: Return type must be annotated.
def log_usage(*log_args: Any, **log_kwargs: Any):
# pyre-fixme[3]: Return type must be annotated.
# pyre-fixme[2]: Parameter must be annotated.
def _log_usage(func):
@wraps(func)
# pyre-fixme[53]: Captured variable `func` is not annotated.
# pyre-fixme[3]: Return type must be annotated.
def wrapper(*args: Any, **kwargs: Any):
return func(*args, **kwargs)

return wrapper

return _log_usage

# pyre-fixme[2]: Parameter must be annotated.
def set_environment(env) -> None: # type: ignore
pass

def disable_detailed_logging() -> None:
pass

# pyre-fixme[2]: Parameter must be annotated.
def patch_methods(tester, patch_log: bool = True) -> None: # type: ignore
pass
from captum.log.dummy_log import ( # type: ignore
disable_detailed_logging,
log,
log_usage,
patch_methods,
set_environment,
TimedLog,
)
56 changes: 56 additions & 0 deletions captum/log/dummy_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3

# pyre-strict

from functools import wraps
from types import TracebackType
from typing import Any, Callable, List, Optional, Union


def log(*args: Any, **kwargs: Any) -> None:
pass


class TimedLog:
def __init__(self, *args: Any, **kwargs: Any) -> None:
pass

def __enter__(self) -> "TimedLog":
return self

def __exit__(
self,
exception_type: Optional[BaseException],
exception_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> bool:
return exception_value is not None


# pyre-ignore[3]: Return type cannot contain Any.
def log_usage(
*log_args: Any, **log_kwargs: Any
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
# pyre-ignore[2]: Callable cannot return Any.
# pyre-ignore[3]: Return type cannot contain Any.
def _log_usage(func: Callable[..., Any]) -> Callable[..., Any]:
@wraps(func)
# pyre-ignore[3]: Return type cannot contain Any.
def wrapper(*args: Any, **kwargs: Any) -> Any:
return func(*args, **kwargs)

return wrapper

return _log_usage


def set_environment(env: Union[None, List[str], str]) -> None:
pass


def disable_detailed_logging() -> None:
pass


def patch_methods(_, patch_log: bool = True) -> None:
pass

0 comments on commit 5b08cac

Please sign in to comment.