Skip to content

Commit 88f2e39

Browse files
committed
Fixed tests.
1 parent e9f3210 commit 88f2e39

2 files changed

Lines changed: 46 additions & 30 deletions

File tree

cmd2/annotated.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,16 @@ def do_paint(
181181
import functools
182182
import inspect
183183
import types
184-
from collections.abc import Callable, Container, Iterable, Sequence
185-
from dataclasses import dataclass, field
184+
from collections.abc import (
185+
Callable,
186+
Container,
187+
Iterable,
188+
Sequence,
189+
)
190+
from dataclasses import (
191+
dataclass,
192+
field,
193+
)
186194
from pathlib import Path
187195
from typing import (
188196
TYPE_CHECKING,
@@ -203,12 +211,21 @@ def do_paint(
203211
from rich.table import Column
204212

205213
from . import constants
206-
from .argparse_utils import DEFAULT_ARGUMENT_PARSER, Cmd2ArgumentParser, SubcommandSpec
214+
from .argparse_utils import (
215+
DEFAULT_ARGUMENT_PARSER,
216+
ApCommandSpec,
217+
Cmd2ArgumentParser,
218+
SubcommandSpec,
219+
)
207220
from .completion import CompletionItem
208221
from .decorators import _parse_positionals
209222
from .exceptions import Cmd2ArgparseError
210223
from .rich_utils import Cmd2HelpFormatter, HelpContent
211-
from .types import CmdOrSetT, UnboundChoicesProvider, UnboundCompleter
224+
from .types import (
225+
CmdOrSetT,
226+
UnboundChoicesProvider,
227+
UnboundCompleter,
228+
)
212229

213230
if TYPE_CHECKING:
214231
from .argparse_completer import ArgparseCompleter
@@ -1828,13 +1845,11 @@ def _filtered_namespace_kwargs(
18281845
exclude_subcommand: bool = False,
18291846
) -> dict[str, Any]:
18301847
"""Filter a parsed Namespace down to user-visible kwargs."""
1831-
from .constants import NS_ATTR_SUBCMD_HANDLER
1832-
18331848
filtered: dict[str, Any] = {}
18341849
for key, value in vars(ns).items():
18351850
if accepted is not None and key not in accepted:
18361851
continue
1837-
if key == NS_ATTR_SUBCMD_HANDLER:
1852+
if key == constants.NS_ATTR_SUBCOMMAND_FUNC:
18381853
continue
18391854
if exclude_subcommand and key == "subcommand":
18401855
continue
@@ -2244,15 +2259,15 @@ def decorator(fn: Callable[..., Any]) -> Callable[..., Any]:
22442259
base_command=base_command,
22452260
options=options,
22462261
)
2247-
spec = SubcommandSpec(
2262+
subcommand_spec = SubcommandSpec(
22482263
name=subcmd_name,
22492264
command=subcommand_to,
22502265
help=help,
22512266
aliases=tuple(aliases),
22522267
deprecated=deprecated,
22532268
parser_source=subcmd_parser_builder,
22542269
)
2255-
setattr(handler, constants.SUBCMD_ATTR_SPEC, spec)
2270+
setattr(handler, constants.SUBCOMMAND_ATTR_SPEC, subcommand_spec)
22562271
return handler
22572272

22582273
command_name = fn.__name__[len(constants.COMMAND_FUNC_PREFIX) :]
@@ -2296,7 +2311,7 @@ def cmd_wrapper(*args: Any, **kwargs: Any) -> bool | None:
22962311
raise Cmd2ArgparseError from exc
22972312

22982313
setattr(ns, constants.NS_ATTR_STATEMENT, statement)
2299-
handler = getattr(ns, constants.NS_ATTR_SUBCMD_HANDLER, None)
2314+
handler = getattr(ns, constants.NS_ATTR_SUBCOMMAND_FUNC, None)
23002315
if base_command and handler is not None:
23012316
handler = functools.partial(handler, ns)
23022317
ns.cmd2_handler = handler
@@ -2312,8 +2327,11 @@ def cmd_wrapper(*args: Any, **kwargs: Any) -> bool | None:
23122327
)
23132328
return result
23142329

2315-
setattr(cmd_wrapper, constants.CMD_ATTR_PARSER_SOURCE, parser_builder)
2316-
setattr(cmd_wrapper, constants.CMD_ATTR_PRESERVE_QUOTES, preserve_quotes)
2330+
ap_command_spec = ApCommandSpec(
2331+
parser_source=parser_builder,
2332+
preserve_quotes=preserve_quotes,
2333+
)
2334+
setattr(cmd_wrapper, constants.AP_COMMAND_ATTR_SPEC, ap_command_spec)
23172335

23182336
return cmd_wrapper
23192337

tests/test_annotated.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import pytest
2424

2525
import cmd2
26-
from cmd2 import (
27-
CompletionItem,
28-
)
26+
from cmd2 import CompletionItem
2927
from cmd2.annotated import (
3028
Argument,
3129
Group,
@@ -1121,7 +1119,7 @@ class MyCompleter(ArgparseCompleter):
11211119
@with_annotated(ap_completer_type=MyCompleter)
11221120
def do_run(self, name: str) -> None: ...
11231121

1124-
builder = getattr(do_run, constants.CMD_ATTR_PARSER_SOURCE)
1122+
builder = getattr(do_run, constants.AP_COMMAND_ATTR_SPEC).parser_source
11251123
assert builder().ap_completer_type is MyCompleter
11261124

11271125
def test_ap_completer_type_threads_to_subcommand(self) -> None:
@@ -1134,7 +1132,7 @@ class MyCompleter(ArgparseCompleter):
11341132
@with_annotated(subcommand_to="team", ap_completer_type=MyCompleter)
11351133
def team_create(self, name: str) -> None: ...
11361134

1137-
spec = getattr(team_create, constants.SUBCMD_ATTR_SPEC)
1135+
spec = getattr(team_create, constants.SUBCOMMAND_ATTR_SPEC)
11381136
assert spec.parser_source().ap_completer_type is MyCompleter
11391137

11401138
def test_customization_via_decorator(self) -> None:
@@ -1178,7 +1176,7 @@ def team_add(self, name: str) -> None:
11781176

11791177
from cmd2 import constants
11801178

1181-
spec = getattr(App.team_add, constants.SUBCMD_ATTR_SPEC)
1179+
spec = getattr(App.team_add, constants.SUBCOMMAND_ATTR_SPEC)
11821180
subparser = spec.parser_source()
11831181
assert subparser.description == "add desc"
11841182
assert subparser.epilog == "add epilog"
@@ -1678,12 +1676,12 @@ def test_int_subclass_uses_int_converter(self) -> None:
16781676

16791677
class TestFilteredNamespaceKwargs:
16801678
def test_excludes_subcmd_handler_key(self) -> None:
1679+
from cmd2 import constants
16811680
from cmd2.annotated import _filtered_namespace_kwargs
1682-
from cmd2.constants import NS_ATTR_SUBCMD_HANDLER
16831681

1684-
ns = argparse.Namespace(**{NS_ATTR_SUBCMD_HANDLER: lambda: None, "name": "Alice"})
1682+
ns = argparse.Namespace(**{constants.NS_ATTR_SUBCOMMAND_FUNC: lambda: None, "name": "Alice"})
16851683
result = _filtered_namespace_kwargs(ns)
1686-
assert NS_ATTR_SUBCMD_HANDLER not in result
1684+
assert constants.NS_ATTR_SUBCOMMAND_FUNC not in result
16871685
assert result == {"name": "Alice"}
16881686

16891687
def test_excludes_subcommand_key(self) -> None:
@@ -2376,7 +2374,7 @@ def test_subcommand_spec_attributes(self, decorator_kwargs, expected_help, expec
23762374
@with_annotated(subcommand_to="team", **decorator_kwargs)
23772375
def team_create(self, name: str = "") -> None: ...
23782376

2379-
spec = getattr(team_create, constants.SUBCMD_ATTR_SPEC)
2377+
spec = getattr(team_create, constants.SUBCOMMAND_ATTR_SPEC)
23802378
assert spec.command == "team"
23812379
assert spec.name == "create"
23822380
assert spec.help == expected_help
@@ -2390,7 +2388,7 @@ def test_subcommand_deprecated_flows_to_spec(self, deprecated) -> None:
23902388
@with_annotated(subcommand_to="team", deprecated=deprecated)
23912389
def team_create(self, name: str = "") -> None: ...
23922390

2393-
spec = getattr(team_create, constants.SUBCMD_ATTR_SPEC)
2391+
spec = getattr(team_create, constants.SUBCOMMAND_ATTR_SPEC)
23942392
assert spec.deprecated is deprecated
23952393

23962394

@@ -3026,7 +3024,7 @@ def _base_parser(**subcommand_kwargs):
30263024
@with_annotated(base_command=True, **subcommand_kwargs)
30273025
def do_root(self, cmd2_handler) -> None: ...
30283026

3029-
builder = getattr(do_root, constants.CMD_ATTR_PARSER_SOURCE)
3027+
builder = getattr(do_root, constants.AP_COMMAND_ATTR_SPEC).parser_source
30303028
return builder()
30313029

30323030
@staticmethod
@@ -3139,7 +3137,7 @@ def do_run(self, name: str) -> None:
31393137
Extra detail.
31403138
"""
31413139

3142-
builder = getattr(do_run, constants.CMD_ATTR_PARSER_SOURCE)
3140+
builder = getattr(do_run, constants.AP_COMMAND_ATTR_SPEC).parser_source
31433141
assert builder().description == "Run the thing."
31443142

31453143
def test_subcommand_uses_docstring(self) -> None:
@@ -3149,7 +3147,7 @@ def test_subcommand_uses_docstring(self) -> None:
31493147
def team_add(self, name: str) -> None:
31503148
"""Add a member to the team."""
31513149

3152-
spec = getattr(team_add, constants.SUBCMD_ATTR_SPEC)
3150+
spec = getattr(team_add, constants.SUBCOMMAND_ATTR_SPEC)
31533151
assert spec.parser_source().description == "Add a member to the team."
31543152

31553153

@@ -3240,7 +3238,7 @@ def test_decorator_passes_parser_kwargs(self) -> None:
32403238
@with_annotated(prog="myprog", usage="usage line")
32413239
def do_run(self, name: str) -> None: ...
32423240

3243-
builder = getattr(do_run, constants.CMD_ATTR_PARSER_SOURCE)
3241+
builder = getattr(do_run, constants.AP_COMMAND_ATTR_SPEC).parser_source
32443242
parser = builder()
32453243
assert parser.prog == "myprog"
32463244
assert parser.usage == "usage line"
@@ -3259,7 +3257,7 @@ def test_usage_allowed_on_subcommand(self) -> None:
32593257
@with_annotated(subcommand_to="team", usage="team add NAME")
32603258
def team_add(self, name: str) -> None: ...
32613259

3262-
spec = getattr(team_add, constants.SUBCMD_ATTR_SPEC)
3260+
spec = getattr(team_add, constants.SUBCOMMAND_ATTR_SPEC)
32633261
assert spec.parser_source().usage == "team add NAME"
32643262

32653263
def test_parents_allowed_on_subcommand(self) -> None:
@@ -3271,7 +3269,7 @@ def test_parents_allowed_on_subcommand(self) -> None:
32713269
@with_annotated(subcommand_to="team", parents=[parent])
32723270
def team_add(self, name: str) -> None: ...
32733271

3274-
spec = getattr(team_add, constants.SUBCMD_ATTR_SPEC)
3272+
spec = getattr(team_add, constants.SUBCOMMAND_ATTR_SPEC)
32753273
dests = {a.dest for a in spec.parser_source()._actions}
32763274
assert "shared" in dests
32773275

@@ -3354,7 +3352,7 @@ def test_decorator_threads_all_low_level_kwargs(self) -> None:
33543352
)
33553353
def do_run(self, name: str) -> None: ...
33563354

3357-
builder = getattr(do_run, constants.CMD_ATTR_PARSER_SOURCE)
3355+
builder = getattr(do_run, constants.AP_COMMAND_ATTR_SPEC).parser_source
33583356
parser = builder()
33593357
assert parser.prefix_chars == "+-"
33603358
assert parser.fromfile_prefix_chars == "@"

0 commit comments

Comments
 (0)