Skip to content

Commit

Permalink
JSONARGPARSE_DEBUG follow environment variables standard of non-empty…
Browse files Browse the repository at this point in the history
… values.
  • Loading branch information
mauvilsa committed Oct 17, 2023
1 parent c96f181 commit bbeb9d1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Changed
- Subclass types no longer allow class instance to be set as default
(`lightning#18731
<https://github.com/Lightning-AI/lightning/issues/18731>`__).
- ``JSONARGPARSE_DEBUG`` must now have a non-empty value to enable debug mode.


v4.25.0 (2023-09-25)
Expand Down
12 changes: 6 additions & 6 deletions DOCUMENTATION.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions jsonargparse/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
Path,
argument_error,
change_to_path_dir,
debug_mode_active,
get_private_kwargs,
identity,
return_parser_if_captured,
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions jsonargparse/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion jsonargparse_tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bbeb9d1

Please sign in to comment.