Skip to content

Commit

Permalink
refactor: lower the default LogLevel for ape test command (#2212)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 12, 2024
1 parent bce0c18 commit dec6b28
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/ape/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def abort(msg: str, base_error: Optional[Exception] = None) -> NoReturn:


def verbosity_option(
cli_logger: Optional[ApeLogger] = None, default: str = DEFAULT_LOG_LEVEL
cli_logger: Optional[ApeLogger] = None, default: Union[str, LogLevel] = DEFAULT_LOG_LEVEL
) -> Callable:
"""A decorator that adds a `--verbosity, -v` option to the decorated
command.
Expand All @@ -67,12 +67,12 @@ def verbosity_option(


def _create_verbosity_kwargs(
_logger: Optional[ApeLogger] = None, default: str = DEFAULT_LOG_LEVEL
_logger: Optional[ApeLogger] = None, default: Union[str, LogLevel] = DEFAULT_LOG_LEVEL
) -> dict:
cli_logger = _logger or logger

def set_level(ctx, param, value):
cli_logger._load_from_sys_argv(default=value.upper())
cli_logger._load_from_sys_argv(default=value.upper() if isinstance(value, str) else value)

level_names = [lvl.name for lvl in LogLevel]
names_str = f"{', '.join(level_names[:-1])}, or {level_names[-1]}"
Expand All @@ -87,16 +87,17 @@ def set_level(ctx, param, value):


def ape_cli_context(
default_log_level: str = DEFAULT_LOG_LEVEL, obj_type: type = ApeCliContextObject
default_log_level: Union[str, LogLevel] = DEFAULT_LOG_LEVEL,
obj_type: type = ApeCliContextObject,
) -> Callable:
"""
A ``click`` context object with helpful utilities.
Use in your commands to get access to common utility features,
such as logging or accessing managers.
Args:
default_log_level (str): The log-level value to pass to
:meth:`~ape.cli.options.verbosity_option`.
default_log_level (Union[str, :class:`~ape.logging.LogLevel`]): The log-level
value to pass to :meth:`~ape.cli.options.verbosity_option`.
obj_type (Type): The context object type. Defaults to
:class:`~ape.cli.options.ApeCliContextObject`. Sub-class
the context to extend its functionality in your CLIs,
Expand Down
2 changes: 1 addition & 1 deletion src/ape/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def format(self, fmt: Optional[str] = None):
fmt = fmt or DEFAULT_LOG_FORMAT
_format_logger(self._logger, fmt)

def _load_from_sys_argv(self, default: Optional[Union[str, int]] = None):
def _load_from_sys_argv(self, default: Optional[Union[str, int, LogLevel]] = None):
"""
Load from sys.argv to beat race condition with `click`.
"""
Expand Down
3 changes: 2 additions & 1 deletion src/ape_test/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from watchdog.observers import Observer

from ape.cli import ape_cli_context
from ape.logging import LogLevel
from ape.utils import ManagerAccessMixin, cached_property

# Copied from https://github.com/olzhasar/pytest-watcher/blob/master/pytest_watcher/watcher.py
Expand Down Expand Up @@ -79,7 +80,7 @@ def _run_main_loop(delay: float, pytest_args: Sequence[str]) -> None:
short_help="Launches pytest and runs the tests for a project",
context_settings=dict(ignore_unknown_options=True),
)
@ape_cli_context()
@ape_cli_context(default_log_level=LogLevel.WARNING)
@click.option(
"-w",
"--watch",
Expand Down

0 comments on commit dec6b28

Please sign in to comment.