Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
Expand Down
19 changes: 8 additions & 11 deletions cmd2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
56 changes: 1 addition & 55 deletions docs/api/utils.md
Original file line number Diff line number Diff line change
@@ -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
Loading