Skip to content

Commit 6e444f4

Browse files
committed
Revert "Added "from __future__ import annotations" to each cmd2 module."
This reverts commit bcf0466.
1 parent bcf0466 commit 6e444f4

20 files changed

+93
-152
lines changed

cmd2/argparse_completer.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
See the header of argparse_custom.py for instructions on how to use these features.
44
"""
55

6-
from __future__ import annotations
7-
86
import argparse
97
import dataclasses
108
import inspect
@@ -24,31 +22,33 @@
2422
cast,
2523
)
2624

25+
from rich.text import Text
26+
27+
from .constants import INFINITY
28+
from .rich_utils import Cmd2GeneralConsole
29+
30+
if TYPE_CHECKING: # pragma: no cover
31+
from .cmd2 import Cmd
32+
2733
from rich.box import SIMPLE_HEAD
2834
from rich.table import (
2935
Column,
3036
Table,
3137
)
32-
from rich.text import Text
3338

3439
from .argparse_custom import (
3540
ChoicesCallable,
3641
generate_range_error,
3742
)
43+
from .command_definition import CommandSet
3844
from .completion import (
3945
CompletionItem,
4046
Completions,
4147
all_display_numeric,
4248
)
43-
from .constants import INFINITY
4449
from .exceptions import CompletionError
45-
from .rich_utils import Cmd2GeneralConsole
4650
from .styles import Cmd2Style
4751

48-
if TYPE_CHECKING: # pragma: no cover
49-
from .cmd2 import Cmd
50-
from .command_definition import CommandSet
51-
5252
# If no table header is supplied, then this will be used instead
5353
DEFAULT_TABLE_HEADER: Sequence[str | Column] = ['Description']
5454

@@ -166,7 +166,7 @@ class ArgparseCompleter:
166166
def __init__(
167167
self,
168168
parser: argparse.ArgumentParser,
169-
cmd2_app: Cmd,
169+
cmd2_app: 'Cmd',
170170
*,
171171
parent_tokens: Mapping[str, MutableSequence[str]] | None = None,
172172
) -> None:

cmd2/argparse_custom.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,6 @@ def get_choices(self) -> Choices:
261261
sub-parser from a sub-parsers group. See _SubParsersAction_remove_parser` for more details.
262262
"""
263263

264-
from __future__ import annotations
265-
266264
import argparse
267265
import re
268266
import sys
@@ -886,7 +884,7 @@ def _match_argument_wrapper(self: argparse.ArgumentParser, action: argparse.Acti
886884
ATTR_AP_COMPLETER_TYPE = 'ap_completer_type'
887885

888886

889-
def _ArgumentParser_get_ap_completer_type(self: argparse.ArgumentParser) -> type[ArgparseCompleter] | None: # noqa: N802
887+
def _ArgumentParser_get_ap_completer_type(self: argparse.ArgumentParser) -> type['ArgparseCompleter'] | None: # noqa: N802
890888
"""Get the ap_completer_type attribute of an argparse ArgumentParser.
891889
892890
This function is added by cmd2 as a method called ``get_ap_completer_type()`` to ``argparse.ArgumentParser`` class.
@@ -902,7 +900,7 @@ def _ArgumentParser_get_ap_completer_type(self: argparse.ArgumentParser) -> type
902900
setattr(argparse.ArgumentParser, 'get_ap_completer_type', _ArgumentParser_get_ap_completer_type)
903901

904902

905-
def _ArgumentParser_set_ap_completer_type(self: argparse.ArgumentParser, ap_completer_type: type[ArgparseCompleter]) -> None: # noqa: N802
903+
def _ArgumentParser_set_ap_completer_type(self: argparse.ArgumentParser, ap_completer_type: type['ArgparseCompleter']) -> None: # noqa: N802
906904
"""Set the ap_completer_type attribute of an argparse ArgumentParser.
907905
908906
This function is added by cmd2 as a method called ``set_ap_completer_type()`` to ``argparse.ArgumentParser`` class.
@@ -1188,7 +1186,7 @@ def __init__(
11881186
suggest_on_error: bool = False,
11891187
color: bool = False,
11901188
*,
1191-
ap_completer_type: type[ArgparseCompleter] | None = None,
1189+
ap_completer_type: type['ArgparseCompleter'] | None = None,
11921190
) -> None:
11931191
"""Initialize the Cmd2ArgumentParser instance, a custom ArgumentParser added by cmd2.
11941192

cmd2/clipboard.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Module provides basic ability to copy from and paste to the clipboard/pastebuffer."""
22

3-
from __future__ import annotations
4-
53
import typing
64

75
import pyperclip # type: ignore[import-untyped]

cmd2/cmd2.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
Documentation: https://cmd2.readthedocs.io/
2323
"""
2424

25-
from __future__ import annotations
26-
25+
# This module has many imports, quite a few of which are only
26+
# infrequently utilized. To reduce the initial overhead of
27+
# import this module, many of these imports are lazy-loaded
28+
# i.e. we only import the module when we use it.
2729
import argparse
2830
import contextlib
2931
import copy
@@ -54,13 +56,14 @@
5456
dataclass,
5557
field,
5658
)
59+
from types import FrameType
5760
from typing import (
5861
IO,
5962
TYPE_CHECKING,
6063
Any,
6164
TextIO,
62-
TypeAlias,
6365
TypeVar,
66+
Union,
6467
cast,
6568
)
6669

@@ -93,6 +96,7 @@
9396
)
9497
from . import rich_utils as ru
9598
from . import string_utils as su
99+
from .argparse_custom import Cmd2ArgumentParser
96100
from .clipboard import (
97101
get_paste_buffer,
98102
write_to_paste_buffer,
@@ -143,6 +147,13 @@
143147
RichPrintKwargs,
144148
)
145149
from .styles import Cmd2Style
150+
from .types import (
151+
ChoicesProviderUnbound,
152+
CmdOrSet,
153+
CompleterBound,
154+
CompleterUnbound,
155+
Matchable,
156+
)
146157

147158
with contextlib.suppress(ImportError):
148159
from IPython import start_ipython
@@ -187,24 +198,6 @@ def __init__(self, msg: str = '') -> None:
187198
suggest_similar,
188199
)
189200

190-
if TYPE_CHECKING: # pragma: no cover
191-
StaticArgParseBuilder = staticmethod[[], argparse.ArgumentParser]
192-
ClassArgParseBuilder = classmethod['Cmd' | CommandSet, [], argparse.ArgumentParser]
193-
from types import FrameType
194-
195-
from .argparse_custom import Cmd2ArgumentParser
196-
from .types import (
197-
ChoicesProviderUnbound,
198-
CmdOrSet,
199-
CompleterBound,
200-
CompleterUnbound,
201-
Matchable,
202-
)
203-
204-
else:
205-
StaticArgParseBuilder = staticmethod
206-
ClassArgParseBuilder = classmethod
207-
208201

209202
class _SavedCmd2Env:
210203
"""cmd2 environment settings that are backed up when entering an interactive Python shell."""
@@ -218,13 +211,21 @@ def __init__(self) -> None:
218211
DisabledCommand = namedtuple('DisabledCommand', ['command_function', 'help_function', 'completer_function']) # noqa: PYI024
219212

220213

214+
if TYPE_CHECKING: # pragma: no cover
215+
StaticArgParseBuilder = staticmethod[[], argparse.ArgumentParser]
216+
ClassArgParseBuilder = classmethod['Cmd' | CommandSet, [], argparse.ArgumentParser]
217+
else:
218+
StaticArgParseBuilder = staticmethod
219+
ClassArgParseBuilder = classmethod
220+
221+
221222
class _CommandParsers:
222223
"""Create and store all command method argument parsers for a given Cmd instance.
223224
224225
Parser creation and retrieval are accomplished through the get() method.
225226
"""
226227

227-
def __init__(self, cmd: Cmd) -> None:
228+
def __init__(self, cmd: 'Cmd') -> None:
228229
self._cmd = cmd
229230

230231
# Keyed by the fully qualified method names. This is more reliable than
@@ -1004,7 +1005,7 @@ def check_parser_uninstallable(parser: argparse.ArgumentParser) -> None:
10041005
if command_parser is not None:
10051006
check_parser_uninstallable(command_parser)
10061007

1007-
def _register_subcommands(self, cmdset: CommandSet | Cmd) -> None:
1008+
def _register_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
10081009
"""Register subcommands with their base command.
10091010
10101011
:param cmdset: CommandSet or cmd2.Cmd subclass containing subcommands
@@ -1097,7 +1098,7 @@ def find_subcommand(
10971098

10981099
break
10991100

1100-
def _unregister_subcommands(self, cmdset: CommandSet | Cmd) -> None:
1101+
def _unregister_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
11011102
"""Unregister subcommands from their base command.
11021103
11031104
:param cmdset: CommandSet containing subcommands
@@ -2194,8 +2195,8 @@ def _determine_ap_completer_type(parser: argparse.ArgumentParser) -> type[argpar
21942195
:param parser: the parser to examine
21952196
:return: type of ArgparseCompleter
21962197
"""
2197-
CompleterType: TypeAlias = type[argparse_completer.ArgparseCompleter] | None
2198-
completer_type: CompleterType = parser.get_ap_completer_type() # type: ignore[attr-defined]
2198+
Completer = type[argparse_completer.ArgparseCompleter] | None # noqa: N806
2199+
completer_type: Completer = parser.get_ap_completer_type() # type: ignore[attr-defined]
21992200

22002201
if completer_type is None:
22012202
completer_type = argparse_completer.DEFAULT_AP_COMPLETER
@@ -5660,7 +5661,7 @@ def register_cmdfinalization_hook(
56605661
def _resolve_func_self(
56615662
self,
56625663
cmd_support_func: Callable[..., Any],
5663-
cmd_self: CommandSet | Cmd | None,
5664+
cmd_self: Union[CommandSet, 'Cmd', None],
56645665
) -> object | None:
56655666
"""Attempt to resolve a candidate instance to pass as 'self'.
56665667

cmd2/colors.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Provides a convenient StrEnum for Rich color names."""
22

3-
from __future__ import annotations
4-
53
import sys
64

75
if sys.version_info >= (3, 11):

cmd2/command_definition.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Supports the definition of commands in separate classes to be composed into cmd2.Cmd."""
22

3-
from __future__ import annotations
4-
53
from collections.abc import (
64
Callable,
75
Mapping,
@@ -17,10 +15,10 @@
1715
COMMAND_FUNC_PREFIX,
1816
)
1917
from .exceptions import CommandSetRegistrationError
18+
from .utils import Settable
2019

2120
if TYPE_CHECKING: # pragma: no cover
2221
from .cmd2 import Cmd
23-
from .utils import Settable
2422

2523
#: Callable signature for a basic command function
2624
#: Further refinements are needed to define the input parameters
@@ -100,7 +98,7 @@ def __init__(self) -> None:
10098
self._settable_prefix = self.__class__.__name__
10199

102100
@property
103-
def _cmd(self) -> Cmd:
101+
def _cmd(self) -> 'Cmd':
104102
"""Property for child classes to access self.__cmd_internal.
105103
106104
Using this property ensures that self.__cmd_internal has been set
@@ -124,7 +122,7 @@ def _cmd(self) -> CustomCmdApp:
124122
raise CommandSetRegistrationError('This CommandSet is not registered')
125123
return self.__cmd_internal
126124

127-
def on_register(self, cmd: Cmd) -> None:
125+
def on_register(self, cmd: 'Cmd') -> None:
128126
"""First step to registering a CommandSet, called by cmd2.Cmd.
129127
130128
The commands defined in this class have not been added to the CLI object at this point.

cmd2/completion.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
"""Provides classes and functions related to command-line completion."""
22

3-
from __future__ import annotations
4-
53
import re
64
import sys
5+
from collections.abc import (
6+
Collection,
7+
Iterable,
8+
Iterator,
9+
Sequence,
10+
)
711
from dataclasses import (
812
dataclass,
913
field,
1014
)
1115
from typing import (
12-
TYPE_CHECKING,
1316
Any,
1417
cast,
1518
overload,
@@ -25,14 +28,7 @@
2528
from rich.protocol import is_renderable
2629

2730
from . import rich_utils as ru
28-
29-
if TYPE_CHECKING:
30-
from collections.abc import (
31-
Collection,
32-
Iterable,
33-
Iterator,
34-
Sequence,
35-
)
31+
from . import utils
3632

3733
# Regular expression to identify strings which we should sort numerically
3834
NUMERIC_RE = re.compile(
@@ -147,8 +143,6 @@ class CompletionResultsBase:
147143

148144
def __post_init__(self) -> None:
149145
"""Finalize the object after initialization."""
150-
from . import utils
151-
152146
unique_items = utils.remove_duplicates(self.items)
153147
if not self.is_sorted:
154148
if all_display_numeric(unique_items):

cmd2/constants.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
"""Constants used throughout ``cmd2``.
1+
"""Constants used throughout ``cmd2``."""
22

3-
Unless documented in https://cmd2.readthedocs.io/en/latest/api/index.html
4-
nothing here should be considered part of the public API of this module
5-
"""
6-
7-
from __future__ import annotations
3+
# Unless documented in https://cmd2.readthedocs.io/en/latest/api/index.html
4+
# nothing here should be considered part of the public API of this module
85

96
INFINITY = float('inf')
107

cmd2/decorators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Decorators for ``cmd2`` commands."""
22

3-
from __future__ import annotations
4-
53
import argparse
64
from collections.abc import (
75
Callable,
@@ -67,7 +65,7 @@ def cat_decorator(func: CommandFunc) -> CommandFunc:
6765
# in cmd2 command functions/callables. As long as the 2-ple of arguments we expect to be there can be
6866
# found we can swap out the statement with each decorator's specific parameters
6967
##########################
70-
def _parse_positionals(args: tuple[Any, ...]) -> tuple[Cmd, Statement | str]:
68+
def _parse_positionals(args: tuple[Any, ...]) -> tuple['Cmd', Statement | str]:
7169
"""Inspect the positional arguments until the cmd2.Cmd argument is found.
7270
7371
Assumes that we will find cmd2.Cmd followed by the command statement object or string.

cmd2/exceptions.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Custom exceptions for cmd2."""
22

3-
from __future__ import annotations
4-
53
from typing import Any
64

75
############################################################################################################

0 commit comments

Comments
 (0)