Skip to content

Commit

Permalink
Fixed inconsistent ARG: and missing ENV: in help when default_env=True.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauvilsa committed Jul 19, 2023
1 parent 453a961 commit 3d62555
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Fixed
- Invalid environment variable names when ``env_prefix`` is derived from
a ``prog`` containing dashes.
- Pylance unable to resolve types from ``jsonargparse.typing``.
- Inconsistent ``ARG:`` and missing ``ENV:`` in help when ``default_env=True``.


v4.22.1 (2023-07-07)
Expand Down
6 changes: 4 additions & 2 deletions jsonargparse/_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
ActionConfigFile,
ActionYesNo,
_ActionConfigLoad,
_ActionHelpClassPath,
_ActionPrintConfig,
_ActionSubCommands,
_find_action,
filter_default_actions,
Expand Down Expand Up @@ -97,10 +99,10 @@ def _format_usage(self, *args, **kwargs) -> str:

def _format_action_invocation(self, action: Action) -> str:
parser = parent_parser.get()
if action.option_strings == [] or action.default == SUPPRESS or not parser.default_env:
if not (parser.default_env and action.option_strings):
return super()._format_action_invocation(action)
extr = ""
if parser.default_env:
if not isinstance(action, (_ActionHelpClassPath, _ActionPrintConfig, _HelpAction)):
extr += "\n ENV: " + get_env_var(self, action)
return "ARG: " + super()._format_action_invocation(action) + extr

Expand Down
9 changes: 8 additions & 1 deletion jsonargparse_tests/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ def parser() -> ArgumentParser:
return ArgumentParser(prog="app", default_env=True)


def test_help_basics(parser):
help_str = get_parser_help(parser)
assert "ARG: -h, --help" in help_str
assert "APP_HELP" not in help_str


def test_help_action_config_file(parser):
parser.add_argument("-c", "--cfg", help="Config in yaml/json.", action=ActionConfigFile)
help_str = get_parser_help(parser)
assert "--print_config" in help_str
assert "ARG: --print_config" in help_str
assert "ARG: -c CFG, --cfg CFG" in help_str
assert "ENV: APP_CFG" in help_str
assert "Config in yaml/json." in help_str
assert "APP_PRINT_CONFIG" not in help_str


def test_help_required_and_default(parser):
Expand Down
11 changes: 11 additions & 0 deletions jsonargparse_tests/test_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ def test_add_class_with_default(parser):
assert defaults == Namespace(cls=Namespace(p1=2, p2="-"))


def test_add_class_env_help(parser):
parser.env_prefix = "APP"
parser.default_env = True
parser.add_class_arguments(WithDefault, "cls")
help_str = get_parser_help(parser)
assert "ARG: --cls CONFIG" in help_str
assert "ARG: --cls.p1 P1" in help_str
assert "ENV: APP_CLS\n" in help_str
assert "ENV: APP_CLS__P1" in help_str


class NoParams:
pass

Expand Down
26 changes: 26 additions & 0 deletions jsonargparse_tests/test_subclasses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
import os
import textwrap
import warnings
from calendar import Calendar, HTMLCalendar, January, TextCalendar # type: ignore
Expand All @@ -9,6 +10,7 @@
from gzip import GzipFile
from pathlib import Path
from typing import Any, Dict, Iterable, List, Mapping, Optional, Union
from unittest.mock import patch

import pytest
import yaml
Expand Down Expand Up @@ -324,6 +326,30 @@ def test_subclass_in_subcommand_with_global_default_config_file(parser, subparse
assert cfg.fit.model.foo == 123


# environment tests


def test_subclass_env_help(parser):
parser.env_prefix = "APP"
parser.default_env = True
parser.add_argument("--cal", type=Calendar)
help_str = get_parser_help(parser)
assert "ARG: --cal CAL" in help_str
assert "ARG: --cal.help" in help_str
assert "ENV: APP_CAL" in help_str
assert "APP_CAL_HELP" not in help_str


def test_subclass_env_config(parser):
parser.env_prefix = "APP"
parser.default_env = True
parser.add_argument("--cal", type=Calendar)
env = {"APP_CAL": "{'class_path': 'TextCalendar', 'init_args': {'firstweekday': 4}}"}
with patch.dict(os.environ, env):
cfg = parser.parse_env()
assert cfg.cal == Namespace(class_path="calendar.TextCalendar", init_args=Namespace(firstweekday=4))


# nested subclass tests


Expand Down

0 comments on commit 3d62555

Please sign in to comment.