From bbeb9d1dd8921dbb53052482e1c4a3ab45c12ebb Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Tue, 17 Oct 2023 05:40:51 +0200 Subject: [PATCH] JSONARGPARSE_DEBUG follow environment variables standard of non-empty values. --- CHANGELOG.rst | 1 + DOCUMENTATION.rst | 12 ++++++------ jsonargparse/_core.py | 8 ++++---- jsonargparse/_util.py | 8 ++++++-- jsonargparse_tests/test_core.py | 2 +- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b899e639..28ec710d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -32,6 +32,7 @@ Changed - Subclass types no longer allow class instance to be set as default (`lightning#18731 `__). +- ``JSONARGPARSE_DEBUG`` must now have a non-empty value to enable debug mode. v4.25.0 (2023-09-25) diff --git a/DOCUMENTATION.rst b/DOCUMENTATION.rst index f634fd5c..39ca493f 100644 --- a/DOCUMENTATION.rst +++ b/DOCUMENTATION.rst @@ -1818,11 +1818,11 @@ arguments. .. note:: The parameter resolvers log messages of failures and unsupported cases. To - view these logs, set the environment variable ``JSONARGPARSE_DEBUG`` to any - value. The supported cases are limited and it is highly encouraged that - people create issues requesting the support for new ones. However, note that - when a case is highly convoluted it could be a symptom that the respective - code is in need of refactoring. + view these logs, set the environment variable ``JSONARGPARSE_DEBUG`` to a + non-empty truthy value. The supported cases are limited and it is highly + encouraged that people create issues requesting the support for new ones. + However, note that when a case is highly convoluted it could be a symptom + that the respective code is in need of refactoring. .. _stubs-resolver: @@ -2563,7 +2563,7 @@ during development since there is not enough information to track down the root of the problem. Without the need to change the source code, this default behavior can be changed such that in case of failure, a ParseError exception is raised and the full stack trace is printed. This is done by setting the -``JSONARGPARSE_DEBUG`` environment variable to any value. +``JSONARGPARSE_DEBUG`` environment variable to a non-empty truthy value. The parsers from jsonargparse log some basic events, though by default this is disabled. To enable, the ``logger`` argument should be set when creating an diff --git a/jsonargparse/_core.py b/jsonargparse/_core.py index 7178e73a..d04bfc0a 100644 --- a/jsonargparse/_core.py +++ b/jsonargparse/_core.py @@ -84,6 +84,7 @@ Path, argument_error, change_to_path_dir, + debug_mode_active, get_private_kwargs, identity, return_parser_if_captured, @@ -995,12 +996,11 @@ def error(self, message: str, ex: Optional[Exception] = None) -> NoReturn: self._error_handler(self, message) if not self.exit_on_error: raise argument_error(message) from ex - elif "JSONARGPARSE_DEBUG" in os.environ: + elif debug_mode_active(): self._logger.debug("Debug enabled, thus raising exception instead of exit.") raise argument_error(message) from ex - self.print_usage(sys.stderr) - args = {"prog": self.prog, "message": message} - sys.stderr.write("%(prog)s: error: %(message)s\n" % args) + self.print_usage() + sys.stderr.write(f"error: {message}\n") self.exit(2) def check_config( diff --git a/jsonargparse/_util.py b/jsonargparse/_util.py index e5b8eb9f..1e968f87 100644 --- a/jsonargparse/_util.py +++ b/jsonargparse/_util.py @@ -765,10 +765,14 @@ def logger(self, logger: Union[bool, str, dict, logging.Logger]): deprecation_warning((LoggerProperty.logger, None), logger_property_none_message) logger = False - if not logger and "JSONARGPARSE_DEBUG" in os.environ: + if not logger and debug_mode_active(): logger = {"level": "DEBUG"} self._logger = parse_logger(logger, type(self).__name__) -if "JSONARGPARSE_DEBUG" in os.environ: +def debug_mode_active() -> bool: + return os.getenv("JSONARGPARSE_DEBUG", "").lower() not in {"", "false", "no", "0"} + + +if debug_mode_active(): os.environ["LOGGER_LEVEL"] = "DEBUG" # pragma: no cover diff --git a/jsonargparse_tests/test_core.py b/jsonargparse_tests/test_core.py index 336bbb3c..314b3867 100644 --- a/jsonargparse_tests/test_core.py +++ b/jsonargparse_tests/test_core.py @@ -922,7 +922,7 @@ def test_version_print(): assert out == "app 1.2.3\n" -@patch.dict(os.environ, {"JSONARGPARSE_DEBUG": ""}) +@patch.dict(os.environ, {"JSONARGPARSE_DEBUG": "true"}) def test_debug_environment_variable(logger): parser = ArgumentParser(logger=logger) parser.add_argument("--int", type=int)