Skip to content

Commit

Permalink
Fix tutorial docs, source, and tests to match current style
Browse files Browse the repository at this point in the history
  • Loading branch information
nealian authored Aug 16, 2024
1 parent 0ac57c3 commit f797a09
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@ The same rules apply for the number of values for each use and their types; the

For this, we use the standard Python `typing.List` and declare its internal type to be a `typing.Tuple`:

//// tab | Python 3.7+

```Python hl_lines="1 7"
{!> ../docs_src/multiple_values/multiple_options_with_multiple_values/tutorial001_an.py!}
```

////

//// tab | Python 3.7+ non-Annotated

/// tip

Prefer to use the `Annotated` version if possible

///

```Python hl_lines="1 6"
{!../docs_src/multiple_values/multiple_options_with_multiple_values/tutorial001.py!}
{!> ../docs_src/multiple_values/multiple_options_with_multiple_values/tutorial001.py!}
```

////

Just as before, the types internal to the `Tuple` define the type of each value in the tuple.

## Check it
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List, Tuple

import typer
from typing_extensions import Annotated


def main(borrow: Annotated[List[Tuple[float, str]], typer.Option()] = []):
if not borrow:
print("Congratulations, you're debt-free!")
raise typer.Exit(0)
total = 0.0
for amount, person in borrow:
print(f"Borrowed {amount:.2f} from {person}")
total += amount
print()
print(f"Total borrowed: {total:.2f}")


if __name__ == "__main__":
typer.run(main)
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def test_invalid_borrow():
def test_script():
result = subprocess.run(
["coverage", "run", mod.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
capture_output=True,
encoding="utf-8",
)
assert "Usage" in result.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import subprocess

import typer
from typer.testing import CliRunner

from docs_src.multiple_values.multiple_options_with_multiple_values import (
tutorial001_an as mod,
)

runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)


def test_main():
result = runner.invoke(app)
assert result.exit_code == 0
assert "Congratulations, you're debt-free!" in result.output


def test_borrow_1():
result = runner.invoke(app, ["--borrow", "2.5", "Mark"])
assert result.exit_code == 0
assert "Borrowed 2.50 from Mark" in result.output
assert "Total borrowed: 2.50" in result.output


def test_borrow_many():
result = runner.invoke(
app,
[
"--borrow",
"2.5",
"Mark",
"--borrow",
"5.25",
"Sean",
"--borrow",
"1.75",
"Wade",
],
)
assert result.exit_code == 0
assert "Borrowed 2.50 from Mark" in result.output
assert "Borrowed 5.25 from Sean" in result.output
assert "Borrowed 1.75 from Wade" in result.output
assert "Total borrowed: 9.50" in result.output


def test_invalid_borrow():
result = runner.invoke(app, ["--borrow", "2.5"])
assert result.exit_code != 0
# TODO: when deprecating Click 7, remove second option

assert (
"Option '--borrow' requires 2 arguments" in result.output
or "--borrow option requires 2 arguments" in result.output
)


def test_script():
result = subprocess.run(
["coverage", "run", mod.__file__, "--help"],
capture_output=True,
encoding="utf-8",
)
assert "Usage" in result.stdout

0 comments on commit f797a09

Please sign in to comment.