458a492ed9
* ✨ Add support for PEP-593 `Annotated` for specifying options and arguments Implements #184 * Revert things I did to get a 3.6 virtualenv 😅 A lot of the dev dependency packages weren't installable on 3.6, so I had to remove them from the pyproject.toml. This commit adds them back * Fix mypy/lint errors * Skip coverage on test lines that shouldn't execute * Missed a spot * ♻️ Tweak examples and tests with Annotated, add extra examples and tests * 🔥 Remove Pydantic-specific logic from _typing.py * 📝 Update docs to use new Annotated examples * 📝 Add docs introducing Annotated and previous versions * 🔧 Add commented out MkDocs config for highlighting docs examples * ✅ Fix tests for Click 7 --------- Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
import typer
|
|
from typer.testing import CliRunner
|
|
from typing_extensions import Annotated
|
|
|
|
runner = CliRunner()
|
|
|
|
|
|
def test_annotated_argument_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()
|
|
|
|
def make_string():
|
|
return "I made it"
|
|
|
|
@app.command()
|
|
def cmd(val: Annotated[str, typer.Argument(default_factory=make_string)]):
|
|
print(val)
|
|
|
|
result = runner.invoke(app)
|
|
assert result.exit_code == 0, result.output
|
|
assert "I made it" in result.output
|
|
|
|
result = runner.invoke(app, ["overridden"])
|
|
assert result.exit_code == 0, result.output
|
|
assert "overridden" in result.output
|
|
|
|
|
|
def test_annotated_option_with_argname_doesnt_mutate_multiple_calls():
|
|
app = typer.Typer()
|
|
|
|
@app.command()
|
|
def cmd(force: Annotated[bool, typer.Option("--force")] = False):
|
|
if force:
|
|
print("Forcing operation")
|
|
else:
|
|
print("Not forcing")
|
|
|
|
result = runner.invoke(app)
|
|
assert result.exit_code == 0, result.output
|
|
assert "Not forcing" in result.output
|
|
|
|
result = runner.invoke(app, ["--force"])
|
|
assert result.exit_code == 0, result.output
|
|
assert "Forcing operation" in result.output
|