Skip to content

Commit

Permalink
🐛 Fix evaluating stringified annotations in Python 3.10 (also `from _…
Browse files Browse the repository at this point in the history
…_future__ import annotations`) (#721)

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>
  • Loading branch information
3 people committed Mar 23, 2024
1 parent d75faa6 commit d1a177c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
19 changes: 19 additions & 0 deletions tests/test_annotated.py
Expand Up @@ -2,6 +2,8 @@
from typer.testing import CliRunner
from typing_extensions import Annotated

from .utils import needs_py310

runner = CliRunner()


Expand All @@ -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()

Expand Down
7 changes: 7 additions & 0 deletions 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+"
)
7 changes: 6 additions & 1 deletion 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

Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit d1a177c

Please sign in to comment.