From 105c3155584745c5346498082d5b2b3db9ab49f8 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Sat, 16 Aug 2025 22:16:38 -0400 Subject: [PATCH] Change utils.get_types to use inspect.get_annotations that was added in Python 3.10 since we dropped support for 3.9 Also: - Add Mac-specific .DS_Store to .gitignore - Upgrade dependencies in .pre-commit-config.yaml - Update utils.md to document the whole module so we don't miss anything --- .gitignore | 3 +++ .pre-commit-config.yaml | 4 +-- cmd2/utils.py | 19 ++++++-------- docs/api/utils.md | 56 +---------------------------------------- 4 files changed, 14 insertions(+), 68 deletions(-) diff --git a/.gitignore b/.gitignore index 51218eb83..10b3d3ce3 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,6 @@ uv.lock # Node/npm used for installing Prettier locally to override the outdated version that is bundled with the VSCode extension node_modules/ package-lock.json + +# macOS +.DS_Store diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8891368fe..bae9bb139 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: "v5.0.0" + rev: "v6.0.0" hooks: - id: check-case-conflict - id: check-merge-conflict @@ -9,7 +9,7 @@ repos: - id: trailing-whitespace - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.12.7" + rev: "v0.12.9" hooks: - id: ruff-format args: [--config=pyproject.toml] diff --git a/cmd2/utils.py b/cmd2/utils.py index 4327627b0..c4b37ab3a 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -16,7 +16,7 @@ from collections.abc import Callable, Iterable from difflib import SequenceMatcher from enum import Enum -from typing import TYPE_CHECKING, Any, TextIO, TypeVar, Union, cast, get_type_hints +from typing import TYPE_CHECKING, Any, TextIO, TypeVar, Union, cast from . import constants from .argparse_custom import ChoicesProviderFunc, CompleterFunc @@ -1245,24 +1245,21 @@ def suggest_similar( def get_types(func_or_method: Callable[..., Any]) -> tuple[dict[str, Any], Any]: - """Use typing.get_type_hints() to extract type hints for parameters and return value. + """Use inspect.get_annotations() to extract type hints for parameters and return value. - This exists because the inspect module doesn't have a safe way of doing this that works - both with and without importing annotations from __future__ until Python 3.10. - - TODO: Once cmd2 only supports Python 3.10+, change to use inspect.get_annotations(eval_str=True) + This is a thin convenience wrapper around inspect.get_annotations() that treats the return value + annotation separately. :param func_or_method: Function or method to return the type hints for - :return tuple with first element being dictionary mapping param names to type hints - and second element being return type hint, unspecified, returns None + :return: tuple with first element being dictionary mapping param names to type hints + and second element being the return type hint or None if there is no return value type hint + :raises ValueError: if the `func_or_method` argument is not a valid object to pass to `inspect.get_annotations` """ try: - type_hints = get_type_hints(func_or_method) # Get dictionary of type hints + type_hints = inspect.get_annotations(func_or_method, eval_str=True) # Get dictionary of type hints except TypeError as exc: raise ValueError("Argument passed to get_types should be a function or method") from exc ret_ann = type_hints.pop('return', None) # Pop off the return annotation if it exists if inspect.ismethod(func_or_method): type_hints.pop('self', None) # Pop off `self` hint for methods - if ret_ann is type(None): - ret_ann = None # Simplify logic to just return None instead of NoneType return type_hints, ret_ann diff --git a/docs/api/utils.md b/docs/api/utils.md index 93bbbf5b1..20a92e6b6 100644 --- a/docs/api/utils.md +++ b/docs/api/utils.md @@ -1,57 +1,3 @@ # cmd2.utils -## Settings - -::: cmd2.utils.Settable - -## Quote Handling - -::: cmd2.utils.is_quoted - -::: cmd2.utils.quote_string - -::: cmd2.utils.quote_string_if_needed - -::: cmd2.utils.strip_quotes - -## IO Handling - -::: cmd2.utils.StdSim - -::: cmd2.utils.ByteBuf - -::: cmd2.utils.ProcReader - -## Tab Completion - -::: cmd2.utils.CompletionMode - -::: cmd2.utils.CustomCompletionSettings - -## Text Alignment - -::: cmd2.utils.TextAlignment - -::: cmd2.utils.align_text - -::: cmd2.utils.align_left - -::: cmd2.utils.align_right - -::: cmd2.utils.align_center - -::: cmd2.utils.truncate_line - -## Miscellaneous - -::: cmd2.utils.to_bool - -::: cmd2.utils.categorize - -::: cmd2.utils.remove_duplicates - -::: cmd2.utils.alphabetical_sort - -::: cmd2.utils.natural_sort - -::: cmd2.utils.suggest_similar +::: cmd2.utils