Skip to content
Myr

🐛 Fix evaluating stringified annotations in Python 3.10 (also `from __future__ import annotations`) (#721)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kazantcev Andrey · committed by GitHub · parent d75faa6 · Browse files · 3 files+32 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
  • Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

Diff stats

3 files+32 added−1 removed

  • tests:+26 added−0 removed
  • other:+6 added−1 removed

3 files+32 added−1 removed

modified:tests/test_annotated.py+19 added
1import typer2from typer.testing import CliRunner3from typing_extensions import Annotated45from .utils import needs_py31067runner = CliRunner()8910def test_annotated_argument_with_default():
11 unmodified lines
22 assert result.exit_code == 0, result.output23 assert "hello 42" in result.output242526@needs_py31027def test_annotated_argument_in_string_type_with_default():28 app = typer.Typer()2930 @app.command()31 def cmd(val: "Annotated[int, typer.Argument()]" = 0):32 print(f"hello {val}")3334 result = runner.invoke(app)35 assert result.exit_code == 0, result.output36 assert "hello 0" in result.output3738 result = runner.invoke(app, ["42"])39 assert result.exit_code == 0, result.output40 assert "hello 42" in result.output414243def test_annotated_argument_with_default_factory():44 app = typer.Typer()4546 def make_string():
32 unmodified lines
added:tests/utils.py+7 added
1import sys23import pytest45needs_py310 = pytest.mark.skipif(6 sys.version_info < (3, 10), reason="requires python3.10+"7)
modified:typer/utils.py+6 added-1 removed
1import inspect2import sys3from copy import copy4from typing import Any, Callable, Dict, List, Tuple, Type, cast, get_type_hints56from typing_extensions import Annotated
99 unmodified lines
106 ]107108109def get_params_from_function(func: Callable[..., Any]) -> Dict[str, ParamMeta]:110 if sys.version_info >= (3, 10):111 signature = inspect.signature(func, eval_str=True)112 else:109 signature = inspect.signature(func)113 signature = inspect.signature(func)114115 type_hints = get_type_hints(func)116 params = {}117 for param in signature.parameters.values():118 annotation, typer_annotations = _split_annotation_from_typer_annotations(
74 unmodified lines