Skip to content

Commit c516fa6

Browse files
committed
Simplified some error checks.
1 parent 2c8354a commit c516fa6

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

cmd2/argparse_custom.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -396,26 +396,26 @@ def _action_set_custom_parameter(self: argparse.Action, value: Any) -> None:
396396
CUSTOM_ACTION_ATTRIBS.add(param_name)
397397

398398

399-
def _choices_callable_validator(self: argparse.Action, value: Any) -> Any:
399+
def _validate_completion_callable(self: argparse.Action, value: Any) -> Any:
400400
"""Validate choices_provider and completer values for potential conflicts."""
401401
if value is None:
402402
return None
403403

404404
if self.choices is not None:
405405
err_msg = "None of the following parameters can be used alongside a choices parameter:\nchoices_provider, completer"
406-
raise TypeError(err_msg)
406+
raise ValueError(err_msg)
407407
if self.nargs == 0:
408408
err_msg = (
409409
"None of the following parameters can be used on an action that takes no arguments:\nchoices_provider, completer"
410410
)
411-
raise TypeError(err_msg)
411+
raise ValueError(err_msg)
412412
return value
413413

414414

415415
# Add new attributes to argparse.Action.
416416
# See _ActionsContainer_add_argument() for details on these attributes.
417-
register_argparse_argument_parameter('choices_provider', validator=_choices_callable_validator)
418-
register_argparse_argument_parameter('completer', validator=_choices_callable_validator)
417+
register_argparse_argument_parameter('choices_provider', validator=_validate_completion_callable)
418+
register_argparse_argument_parameter('completer', validator=_validate_completion_callable)
419419
register_argparse_argument_parameter('table_columns')
420420
register_argparse_argument_parameter('nargs_range')
421421
register_argparse_argument_parameter('suppress_tab_hint')
@@ -470,12 +470,8 @@ def _ActionsContainer_add_argument( # noqa: N802
470470
:raises ValueError: on incorrect parameter usage
471471
"""
472472
# Verify consistent use of arguments
473-
choices_callables = [choices_provider, completer]
474-
num_params_set = len(choices_callables) - choices_callables.count(None)
475-
476-
if num_params_set > 1:
477-
err_msg = "Only one of the following parameters may be used at a time:\nchoices_provider, completer"
478-
raise (ValueError(err_msg))
473+
if choices_provider is not None and completer is not None:
474+
raise ValueError("Only one of the following parameters may be used at a time:\nchoices_provider, completer")
479475

480476
# Pre-process special ranged nargs
481477
nargs_range = None

tests/test_argparse_custom.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def fake_func() -> None:
5454
({'choices_provider': fake_func, 'completer': fake_func}, False),
5555
],
5656
)
57-
def test_apcustom_choices_callable_count(kwargs, is_valid) -> None:
57+
def test_apcustom_completion_callable_count(kwargs, is_valid) -> None:
5858
parser = Cmd2ArgumentParser()
5959
if is_valid:
6060
parser.add_argument('name', **kwargs)
@@ -65,17 +65,17 @@ def test_apcustom_choices_callable_count(kwargs, is_valid) -> None:
6565

6666

6767
@pytest.mark.parametrize('kwargs', [({'choices_provider': fake_func}), ({'completer': fake_func})])
68-
def test_apcustom_no_choices_callables_alongside_choices(kwargs) -> None:
68+
def test_apcustom_no_completion_callable_alongside_choices(kwargs) -> None:
6969
parser = Cmd2ArgumentParser()
70-
with pytest.raises(TypeError) as excinfo:
70+
with pytest.raises(ValueError) as excinfo:
7171
parser.add_argument('name', choices=['my', 'choices', 'list'], **kwargs)
7272
assert 'None of the following parameters can be used alongside a choices parameter' in str(excinfo.value)
7373

7474

7575
@pytest.mark.parametrize('kwargs', [({'choices_provider': fake_func}), ({'completer': fake_func})])
76-
def test_apcustom_no_choices_callables_when_nargs_is_0(kwargs) -> None:
76+
def test_apcustom_no_completion_callable_when_nargs_is_0(kwargs) -> None:
7777
parser = Cmd2ArgumentParser()
78-
with pytest.raises(TypeError) as excinfo:
78+
with pytest.raises(ValueError) as excinfo:
7979
parser.add_argument('--name', action='store_true', **kwargs)
8080
assert 'None of the following parameters can be used on an action that takes no arguments' in str(excinfo.value)
8181

0 commit comments

Comments
 (0)