Skip to content

Commit db2654c

Browse files
authored
Change utils.get_types to use inspect.get_annotations that was added in Python 3.10 since we dropped support for 3.9 (#1476)
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
1 parent 8e0a1a2 commit db2654c

File tree

4 files changed

+14
-68
lines changed

4 files changed

+14
-68
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,6 @@ uv.lock
5454
# Node/npm used for installing Prettier locally to override the outdated version that is bundled with the VSCode extension
5555
node_modules/
5656
package-lock.json
57+
58+
# macOS
59+
.DS_Store

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: "v5.0.0"
3+
rev: "v6.0.0"
44
hooks:
55
- id: check-case-conflict
66
- id: check-merge-conflict
@@ -9,7 +9,7 @@ repos:
99
- id: trailing-whitespace
1010

1111
- repo: https://github.com/astral-sh/ruff-pre-commit
12-
rev: "v0.12.7"
12+
rev: "v0.12.9"
1313
hooks:
1414
- id: ruff-format
1515
args: [--config=pyproject.toml]

cmd2/utils.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from collections.abc import Callable, Iterable
1717
from difflib import SequenceMatcher
1818
from enum import Enum
19-
from typing import TYPE_CHECKING, Any, TextIO, TypeVar, Union, cast, get_type_hints
19+
from typing import TYPE_CHECKING, Any, TextIO, TypeVar, Union, cast
2020

2121
from . import constants
2222
from .argparse_custom import ChoicesProviderFunc, CompleterFunc
@@ -1245,24 +1245,21 @@ def suggest_similar(
12451245

12461246

12471247
def get_types(func_or_method: Callable[..., Any]) -> tuple[dict[str, Any], Any]:
1248-
"""Use typing.get_type_hints() to extract type hints for parameters and return value.
1248+
"""Use inspect.get_annotations() to extract type hints for parameters and return value.
12491249
1250-
This exists because the inspect module doesn't have a safe way of doing this that works
1251-
both with and without importing annotations from __future__ until Python 3.10.
1252-
1253-
TODO: Once cmd2 only supports Python 3.10+, change to use inspect.get_annotations(eval_str=True)
1250+
This is a thin convenience wrapper around inspect.get_annotations() that treats the return value
1251+
annotation separately.
12541252
12551253
:param func_or_method: Function or method to return the type hints for
1256-
:return tuple with first element being dictionary mapping param names to type hints
1257-
and second element being return type hint, unspecified, returns None
1254+
:return: tuple with first element being dictionary mapping param names to type hints
1255+
and second element being the return type hint or None if there is no return value type hint
1256+
:raises ValueError: if the `func_or_method` argument is not a valid object to pass to `inspect.get_annotations`
12581257
"""
12591258
try:
1260-
type_hints = get_type_hints(func_or_method) # Get dictionary of type hints
1259+
type_hints = inspect.get_annotations(func_or_method, eval_str=True) # Get dictionary of type hints
12611260
except TypeError as exc:
12621261
raise ValueError("Argument passed to get_types should be a function or method") from exc
12631262
ret_ann = type_hints.pop('return', None) # Pop off the return annotation if it exists
12641263
if inspect.ismethod(func_or_method):
12651264
type_hints.pop('self', None) # Pop off `self` hint for methods
1266-
if ret_ann is type(None):
1267-
ret_ann = None # Simplify logic to just return None instead of NoneType
12681265
return type_hints, ret_ann

docs/api/utils.md

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,3 @@
11
# cmd2.utils
22

3-
## Settings
4-
5-
::: cmd2.utils.Settable
6-
7-
## Quote Handling
8-
9-
::: cmd2.utils.is_quoted
10-
11-
::: cmd2.utils.quote_string
12-
13-
::: cmd2.utils.quote_string_if_needed
14-
15-
::: cmd2.utils.strip_quotes
16-
17-
## IO Handling
18-
19-
::: cmd2.utils.StdSim
20-
21-
::: cmd2.utils.ByteBuf
22-
23-
::: cmd2.utils.ProcReader
24-
25-
## Tab Completion
26-
27-
::: cmd2.utils.CompletionMode
28-
29-
::: cmd2.utils.CustomCompletionSettings
30-
31-
## Text Alignment
32-
33-
::: cmd2.utils.TextAlignment
34-
35-
::: cmd2.utils.align_text
36-
37-
::: cmd2.utils.align_left
38-
39-
::: cmd2.utils.align_right
40-
41-
::: cmd2.utils.align_center
42-
43-
::: cmd2.utils.truncate_line
44-
45-
## Miscellaneous
46-
47-
::: cmd2.utils.to_bool
48-
49-
::: cmd2.utils.categorize
50-
51-
::: cmd2.utils.remove_duplicates
52-
53-
::: cmd2.utils.alphabetical_sort
54-
55-
::: cmd2.utils.natural_sort
56-
57-
::: cmd2.utils.suggest_similar
3+
::: cmd2.utils

0 commit comments

Comments
 (0)