diff --git a/tests/test_annotated.py b/tests/test_annotated.py index 6436ad668..09072b3ae 100644 --- a/tests/test_annotated.py +++ b/tests/test_annotated.py @@ -2,6 +2,8 @@ from typer.testing import CliRunner from typing_extensions import Annotated +from .utils import needs_py310 + runner = CliRunner() @@ -21,6 +23,23 @@ def cmd(val: Annotated[int, typer.Argument()] = 0): assert "hello 42" in result.output +@needs_py310 +def test_annotated_argument_in_string_type_with_default(): + app = typer.Typer() + + @app.command() + def cmd(val: "Annotated[int, typer.Argument()]" = 0): + print(f"hello {val}") + + result = runner.invoke(app) + assert result.exit_code == 0, result.output + assert "hello 0" in result.output + + result = runner.invoke(app, ["42"]) + assert result.exit_code == 0, result.output + assert "hello 42" in result.output + + def test_annotated_argument_with_default_factory(): app = typer.Typer() diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 000000000..17d6de906 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,7 @@ +import sys + +import pytest + +needs_py310 = pytest.mark.skipif( + sys.version_info < (3, 10), reason="requires python3.10+" +) diff --git a/typer/utils.py b/typer/utils.py index 44816e242..2ba7bace4 100644 --- a/typer/utils.py +++ b/typer/utils.py @@ -1,4 +1,5 @@ import inspect +import sys from copy import copy from typing import Any, Callable, Dict, List, Tuple, Type, cast, get_type_hints @@ -106,7 +107,11 @@ def _split_annotation_from_typer_annotations( def get_params_from_function(func: Callable[..., Any]) -> Dict[str, ParamMeta]: - signature = inspect.signature(func) + if sys.version_info >= (3, 10): + signature = inspect.signature(func, eval_str=True) + else: + signature = inspect.signature(func) + type_hints = get_type_hints(func) params = {} for param in signature.parameters.values():