From b6a80a1e668a17f447de7296e18ef1bd3c992e90 Mon Sep 17 00:00:00 2001 From: Zane Date: Thu, 26 Sep 2024 14:22:14 -0700 Subject: [PATCH] feat: deprecate verbose flag --- schemachange/config/get_merged_config.py | 5 --- schemachange/config/parse_cli_args.py | 41 +++++++++++++++++++++++- tests/config/test_parse_cli_args.py | 15 +++++++++ tests/test_main.py | 2 ++ 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/schemachange/config/get_merged_config.py b/schemachange/config/get_merged_config.py index 607c09e..f34bbe9 100644 --- a/schemachange/config/get_merged_config.py +++ b/schemachange/config/get_merged_config.py @@ -31,11 +31,6 @@ def get_yaml_config_kwargs(config_file_path: Optional[Path]) -> dict: def get_merged_config() -> Union[DeployConfig, RenderConfig]: cli_kwargs = parse_cli_args(sys.argv[1:]) - - if "verbose" in cli_kwargs and cli_kwargs["verbose"]: - cli_kwargs["log_level"] = logging.DEBUG - cli_kwargs.pop("verbose") - cli_config_vars = cli_kwargs.pop("config_vars", None) if cli_config_vars is None: cli_config_vars = {} diff --git a/schemachange/config/parse_cli_args.py b/schemachange/config/parse_cli_args.py index f287cd5..ffbfa58 100644 --- a/schemachange/config/parse_cli_args.py +++ b/schemachange/config/parse_cli_args.py @@ -2,6 +2,8 @@ import argparse import json +import logging +import warnings from enum import Enum import structlog @@ -41,6 +43,34 @@ def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, value) +class LogLevel(Enum): + CRITICAL = logging.CRITICAL + ERROR = logging.ERROR + WARNING = logging.WARNING + INFO = logging.INFO + DEBUG = logging.DEBUG + + +def deprecate_verbose( + args: list[str], verbose: argparse.Action, parsed_args: argparse.Namespace +): + # If --verbose or -v were supplied, warn the user and interpret it as a + for option_string in verbose.option_strings: + if option_string not in args: + continue + + warnings.warn( + "Argument %s is deprecated and will be interpreted as a DEBUG log level." + % verbose.option_strings + ) + + parsed_args.log_level = logging.DEBUG + + break + + del parsed_args.verbose + + def parse_cli_args(args) -> dict: parser = argparse.ArgumentParser( prog="schemachange", @@ -80,12 +110,19 @@ def parse_cli_args(args) -> dict: required=False, ) parent_parser.add_argument( + "--log-level", + type=LogLevel, + action=EnumAction, + help="Set the log level. Defaults to INFO.", + ) + verbose = parent_parser.add_argument( "-v", "--verbose", action="store_const", const=True, default=None, - help="Display verbose debugging details during execution (the default is False)", + help="DEPRECATED: Use --log-level instead. Display verbose debugging details " + "during execution (the default is False)", required=False, ) @@ -199,6 +236,8 @@ def parse_cli_args(args) -> dict: parsed_args = parser.parse_args(args) + deprecate_verbose(args=args, verbose=verbose, parsed_args=parsed_args) + parsed_kwargs = parsed_args.__dict__ if "log_level" in parsed_kwargs and isinstance(parsed_kwargs["log_level"], Enum): diff --git a/tests/config/test_parse_cli_args.py b/tests/config/test_parse_cli_args.py index 31e5fa7..a8beaa8 100644 --- a/tests/config/test_parse_cli_args.py +++ b/tests/config/test_parse_cli_args.py @@ -1,6 +1,9 @@ from __future__ import annotations import json +import logging + +import pytest from schemachange.config.parse_cli_args import parse_cli_args @@ -121,3 +124,15 @@ def test_parse_args_deploy_flags(): assert parsed_args["subcommand"] == "deploy" for expected_arg, expected_value in expected.items(): assert parsed_args[expected_arg] == expected_value + + +def test_parse_args_verbose_deprecation(): + args: list[str] = ["--verbose"] + with pytest.warns(UserWarning) as warning: + parsed_args = parse_cli_args(args) + assert parsed_args.get("verbose", None) is None + assert parsed_args["log_level"] is logging.DEBUG + assert ( + str(warning[0].message) + == "Argument ['-v', '--verbose'] is deprecated and will be interpreted as a DEBUG log level." + ) diff --git a/tests/test_main.py b/tests/test_main.py index 384c404..60480de 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -189,6 +189,8 @@ "schemachange", "deploy", *required_args, + "--log-level", + "INFO", ], { **default_deploy_config,