Skip to content

Commit 0525fd2

Browse files
authored
Fix: --print_shtab crashing on failure to get signature parameters from one class (#537, lightning#10858 comment)
1 parent 82273f9 commit 0525fd2

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Fixed
1919
^^^^^
2020
- Resolving of import paths for some ``torch`` functions not working (`#535
2121
<https://github.com/omni-us/jsonargparse/pull/535>`__).
22+
- ``--print_shtab`` crashing on failure to get signature parameters from one
23+
class (`lightning#10858 comment
24+
<https://github.com/Lightning-AI/pytorch-lightning/discussions/10858#discussioncomment-9846252>`__).
2225

2326
Changed
2427
^^^^^^^

jsonargparse/_completions.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
)
2626
from ._util import NoneType, Path, import_object, unique
2727

28-
shtab_shell: ContextVar = ContextVar("shtab_shell")
29-
shtab_prog: ContextVar = ContextVar("shtab_prog")
30-
shtab_preambles: ContextVar = ContextVar("shtab_preambles")
31-
3228

3329
def handle_completions(parser):
3430
if find_spec("argcomplete") and "_ARGCOMPLETE" in os.environ:
@@ -76,6 +72,10 @@ def argcomplete_warn_redraw_prompt(prefix, message):
7672

7773
# shtab
7874

75+
shtab_shell: ContextVar = ContextVar("shtab_shell")
76+
shtab_prog: ContextVar = ContextVar("shtab_prog")
77+
shtab_preambles: ContextVar = ContextVar("shtab_preambles")
78+
7979

8080
class ShtabAction(argparse.Action):
8181
def __init__(
@@ -236,7 +236,7 @@ def get_typehint_choices(typehint, prefix, parser, skip, choices=None, added_sub
236236
origin = get_typehint_origin(typehint)
237237
if origin == Union:
238238
for subtype in typehint.__args__:
239-
if subtype in added_subclasses:
239+
if subtype in added_subclasses or subtype is object:
240240
continue
241241
get_typehint_choices(subtype, prefix, parser, skip, choices, added_subclasses)
242242
elif ActionTypeHint.is_subclass_typehint(typehint):
@@ -261,8 +261,12 @@ def add_subactions_and_get_subclass_choices(typehint, prefix, parser, skip, adde
261261
subclasses = defaultdict(list)
262262
for path in paths:
263263
choices.append(path)
264-
cls = import_object(path)
265-
params = get_signature_parameters(cls)
264+
try:
265+
cls = import_object(path)
266+
params = get_signature_parameters(cls, None, parser._logger)
267+
except Exception as ex:
268+
parser._logger.debug(f"Unable to get signature parameters for '{path}': {ex}")
269+
continue
266270
num_skip = next((s for s in skip if isinstance(s, int)), 0)
267271
if num_skip > 0:
268272
params = params[num_skip:]

jsonargparse_tests/test_shtab.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
from jsonargparse import ArgumentParser
1515
from jsonargparse._completions import norm_name
16+
from jsonargparse._parameter_resolvers import get_signature_parameters
1617
from jsonargparse._typehints import type_to_str
1718
from jsonargparse.typing import Path_drw, Path_fr
18-
from jsonargparse_tests.conftest import get_parse_args_stdout
19+
from jsonargparse_tests.conftest import capture_logs, get_parse_args_stdout
1920

2021

2122
@pytest.fixture(autouse=True)
@@ -203,6 +204,22 @@ def __init__(self, p1: int, p3: float):
203204
pass
204205

205206

207+
def test_bash_subclasses_fail_get_perams(parser, logger):
208+
def get_params_patch(cls, method, logger):
209+
if cls == SubB:
210+
raise Exception("test get params failure")
211+
return get_signature_parameters(cls, method, logger)
212+
213+
parser.logger = logger
214+
parser.add_argument("--cls", type=Base)
215+
with capture_logs(logger) as logs, patch("jsonargparse._completions.get_signature_parameters", get_params_patch):
216+
shtab_script = get_shtab_script(parser, "bash")
217+
assert "'--cls' '--cls.p1' '--cls.p2'" in shtab_script
218+
assert f"'{__name__}.SubB'" in shtab_script
219+
assert "'--cls.p3'" not in shtab_script
220+
assert "test_shtab.SubB': test get params failure" in logs.getvalue()
221+
222+
206223
def test_bash_subclasses_help(parser):
207224
parser.add_argument("--cls", type=Base)
208225
shtab_script = get_shtab_script(parser, "bash")

0 commit comments

Comments
 (0)