Skip to content

This PR forces the use of from __future__ import annotations throughout cmd2 #1444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.6.2 (TBD)

- TBD

## 2.6.1 (June 8, 2025)

- Bug Fixes
Expand Down
2 changes: 2 additions & 0 deletions cmd2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Import certain things for backwards compatibility."""

from __future__ import annotations

import argparse
import contextlib
import importlib.metadata as importlib_metadata
Expand Down
19 changes: 10 additions & 9 deletions cmd2/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
These are used for things like applying style to text, setting the window title, and asynchronous alerts.
"""

from __future__ import annotations

import functools
import re
from enum import (
Expand All @@ -11,7 +13,6 @@
from typing import (
IO,
Any,
Optional,
cast,
)

Expand Down Expand Up @@ -951,14 +952,14 @@ def __str__(self) -> str:
def style(
value: Any,
*,
fg: Optional[FgColor] = None,
bg: Optional[BgColor] = None,
bold: Optional[bool] = None,
dim: Optional[bool] = None,
italic: Optional[bool] = None,
overline: Optional[bool] = None,
strikethrough: Optional[bool] = None,
underline: Optional[bool] = None,
fg: FgColor | None = None,
bg: BgColor | None = None,
bold: bool | None = None,
dim: bool | None = None,
italic: bool | None = None,
overline: bool | None = None,
strikethrough: bool | None = None,
underline: bool | None = None,
) -> str:
"""Apply ANSI colors and/or styles to a string and return it.

Expand Down
27 changes: 14 additions & 13 deletions cmd2/argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
See the header of argparse_custom.py for instructions on how to use these features.
"""

from __future__ import annotations

import argparse
import inspect
import numbers
Expand All @@ -11,7 +13,6 @@
)
from typing import (
TYPE_CHECKING,
Optional,
Union,
cast,
)
Expand All @@ -28,16 +29,16 @@
from .cmd2 import (
Cmd,
)
from .command_definition import (
CommandSet,
)

from .argparse_custom import (
ChoicesCallable,
ChoicesProviderFuncWithTokens,
CompletionItem,
generate_range_error,
)
from .command_definition import (
CommandSet,
)
from .exceptions import (
CompletionError,
)
Expand Down Expand Up @@ -104,8 +105,8 @@ class _ArgumentState:

def __init__(self, arg_action: argparse.Action) -> None:
self.action = arg_action
self.min: Union[int, str]
self.max: Union[float, int, str]
self.min: int | str
self.max: float | int | str
self.count = 0
self.is_remainder = self.action.nargs == argparse.REMAINDER

Expand Down Expand Up @@ -162,7 +163,7 @@ class ArgparseCompleter:
"""Automatic command line tab completion based on argparse parameters."""

def __init__(
self, parser: argparse.ArgumentParser, cmd2_app: 'Cmd', *, parent_tokens: Optional[dict[str, list[str]]] = None
self, parser: argparse.ArgumentParser, cmd2_app: Cmd, *, parent_tokens: dict[str, list[str]] | None = None
) -> None:
"""Create an ArgparseCompleter.

Expand Down Expand Up @@ -202,7 +203,7 @@ def __init__(
self._subcommand_action = action

def complete(
self, text: str, line: str, begidx: int, endidx: int, tokens: list[str], *, cmd_set: Optional[CommandSet] = None
self, text: str, line: str, begidx: int, endidx: int, tokens: list[str], *, cmd_set: CommandSet | None = None
) -> list[str]:
"""Complete text using argparse metadata.

Expand All @@ -227,10 +228,10 @@ def complete(
skip_remaining_flags = False

# _ArgumentState of the current positional
pos_arg_state: Optional[_ArgumentState] = None
pos_arg_state: _ArgumentState | None = None

# _ArgumentState of the current flag
flag_arg_state: Optional[_ArgumentState] = None
flag_arg_state: _ArgumentState | None = None

# Non-reusable flags that we've parsed
matched_flags: list[str] = []
Expand Down Expand Up @@ -522,7 +523,7 @@ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matche

return matches

def _format_completions(self, arg_state: _ArgumentState, completions: Union[list[str], list[CompletionItem]]) -> list[str]:
def _format_completions(self, arg_state: _ArgumentState, completions: list[str] | list[CompletionItem]) -> list[str]:
"""Format CompletionItems into hint table."""
# Nothing to do if we don't have at least 2 completions which are all CompletionItems
if len(completions) < 2 or not all(isinstance(c, CompletionItem) for c in completions):
Expand Down Expand Up @@ -652,15 +653,15 @@ def _complete_arg(
arg_state: _ArgumentState,
consumed_arg_values: dict[str, list[str]],
*,
cmd_set: Optional[CommandSet] = None,
cmd_set: CommandSet | None = None,
) -> list[str]:
"""Tab completion routine for an argparse argument.

:return: list of completions
:raises CompletionError: if the completer or choices function this calls raises one.
"""
# Check if the arg provides choices to the user
arg_choices: Union[list[str], ChoicesCallable]
arg_choices: list[str] | ChoicesCallable
if arg_state.action.choices is not None:
arg_choices = list(arg_state.action.choices)
if not arg_choices:
Expand Down
Loading
Loading