Skip to content
Myr

test_annotated.py

This file

Path

tests/test_annotated.py

78 lines · 2.0 KiB · Python

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 8f98e0d point at it.

👷 Deploy docs to Cloudflare (#644)Sebastián Ramírezbefore adoption8f98e0d

lines 1–4why, one step back
1import typer
2from typer.testing import CliRunner3from typing_extensions import Annotated4

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

lines 5–6why, one step back
5from .utils import needs_py310
6

👷 Deploy docs to Cloudflare (#644)Sebastián Ramírezbefore adoption8f98e0d

lines 7–14why, one step back
7runner = CliRunner()
8910def test_annotated_argument_with_default():11 app = typer.Typer()1213 @app.command()14 def cmd(val: Annotated[int, typer.Argument()] = 0):

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

lines 15–31why, one step back
15 print(f"hello {val}")
1617 result = runner.invoke(app)18 assert result.exit_code == 0, result.output19 assert "hello 0" in result.output2021 result = runner.invoke(app, ["42"])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):

👷 Deploy docs to Cloudflare (#644)Sebastián Ramírezbefore adoption8f98e0d

lines 32–78why, one step back
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():47 return "I made it"4849 @app.command()50 def cmd(val: Annotated[str, typer.Argument(default_factory=make_string)]):51 print(val)5253 result = runner.invoke(app)54 assert result.exit_code == 0, result.output55 assert "I made it" in result.output5657 result = runner.invoke(app, ["overridden"])58 assert result.exit_code == 0, result.output59 assert "overridden" in result.output606162def test_annotated_option_with_argname_doesnt_mutate_multiple_calls():63 app = typer.Typer()6465 @app.command()66 def cmd(force: Annotated[bool, typer.Option("--force")] = False):67 if force:68 print("Forcing operation")69 else:70 print("Not forcing")7172 result = runner.invoke(app)73 assert result.exit_code == 0, result.output74 assert "Not forcing" in result.output7576 result = runner.invoke(app, ["--force"])77 assert result.exit_code == 0, result.output78 assert "Forcing operation" in result.output