test_types.py
This file
Path
tests/test_types.py
165 lines · 4.7 KiB · Python
Last changed by
🐛 Ensure that the default of a list argument is used correctly (#1821)Sofie Van Landeghembefore adoption06fce7d
History of this fileTake it
The walk stopped after 200 commits: lines older than 9009016 point at it.
⬆ Bump pygithub from 2.8.1 to 2.9.0 (#1650)dependabot[bot]before adoption9009016
lines 1–2why, one step back1from enum import Enum2➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 3why, one step back3import pytest⬆ Bump pygithub from 2.8.1 to 2.9.0 (#1650)dependabot[bot]before adoption9009016
line 4why, one step back4import typer➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 5why, one step back5from typer import _click⬆ Bump pygithub from 2.8.1 to 2.9.0 (#1650)dependabot[bot]before adoption9009016
lines 6–16why, one step back6from typer.testing import CliRunner78app = typer.Typer(context_settings={"token_normalize_func": str.lower})91011class User(str, Enum):12 rick = "Rick"13 morty = "Morty"141516@app.command()➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 17why, one step back17def hello_option(name: User = User.rick) -> None:⬆ Bump pygithub from 2.8.1 to 2.9.0 (#1650)dependabot[bot]before adoption9009016
lines 18–20why, one step back18 print(f"Hello {name.value}!")1920➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 21–33why, one step back21@app.command()22def hello_argument(name: User) -> None:23 print(f"Hello {name.value}!")242526@app.command()27def hello_no_choices(28 name: User = typer.Option(..., "--name", show_choices=False),29):30 print(f"Hello {name.value}!")313233@app.command()🐛 Ensure that the default of a list argument is used correctly (#1821)Sofie Van Landeghembefore adoption06fce7d
lines 34–40why, one step back34def hello_all_options(name: list[str] = typer.Option(["World"])) -> None:35 for n in name:36 print(f"Hello {n}!")373839@app.command()40def hello_all_args(names: list[str] = typer.Argument(["World"])) -> None:➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 41–45why, one step back41 for name in names:42 print(f"Hello {name}!")434445@app.command()🐛 Ensure that the default of a list argument is used correctly (#1821)Sofie Van Landeghembefore adoption06fce7d
lines 46–53why, one step back46def hello_all_envvar(47 names: list[str] = typer.Argument(["World"], envvar="NAMES"),48) -> None:49 for name in names:50 print(f"Hello {name}!")515253@app.command()➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 54–58why, one step back54def split_variadic_and_pair(items: list[str], pair: tuple[str, str]) -> None:55 print(f"items={items}")56 print(f"pair={pair}")5758⬆ Bump pygithub from 2.8.1 to 2.9.0 (#1650)dependabot[bot]before adoption9009016
lines 59–62why, one step back59runner = CliRunner()606162def test_enum_choice() -> None:➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 63–65why, one step back63 result = runner.invoke(64 app, ["hello-option", "--name", "morty"], catch_exceptions=False65 )⬆ Bump pygithub from 2.8.1 to 2.9.0 (#1650)dependabot[bot]before adoption9009016
lines 66–68why, one step back66 assert result.exit_code == 067 assert "Hello Morty!" in result.output68➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 69why, one step back69 result = runner.invoke(app, ["hello-option", "--name", "Rick"])⬆ Bump pygithub from 2.8.1 to 2.9.0 (#1650)dependabot[bot]before adoption9009016
lines 70–72why, one step back70 assert result.exit_code == 071 assert "Hello Rick!" in result.output72➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 73why, one step back73 result = runner.invoke(app, ["hello-option", "--name", "RICK"])⬆ Bump pygithub from 2.8.1 to 2.9.0 (#1650)dependabot[bot]before adoption9009016
lines 74–75why, one step back74 assert result.exit_code == 075 assert "Hello Rick!" in result.output➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 76–116why, one step back7677 result = runner.invoke(app, ["hello-no-choices", "--name", "RICK"])78 assert result.exit_code == 079 assert "Hello Rick!" in result.output8081 result = runner.invoke(app, ["hello-argument", "RICK"])82 assert result.exit_code == 083 assert "Hello Rick!" in result.output848586def test_enum_choice_repr() -> None:87 root_command = typer.main.get_command(app)88 command = root_command.commands["hello-option"]89 name_param = next(param for param in command.params if param.name == "name")90 assert repr(name_param.type).startswith("Choice([")919293def test_enum_choice_help() -> None:94 result = runner.invoke(app, ["hello-argument", "--help"])95 assert result.exit_code == 096 assert "{rick|morty}" in result.output9798 result = runner.invoke(app, ["hello-option", "--help"])99 assert result.exit_code == 0100 assert "[rick|morty]" in result.output101102 result = runner.invoke(app, ["hello-no-choices", "--help"])103 assert result.exit_code == 0104 assert "--name" in result.output105 assert "rick|morty" not in result.output106107108def test_enum_choice_missing_message() -> None:109 result = runner.invoke(app, ["hello-argument"])110 assert result.exit_code != 0111 assert "Missing argument" in result.output112 assert "Choose from:" in result.output113 assert "rick" in result.output114 assert "morty" in result.output115116🐛 Ensure that the default of a list argument is used correctly (#1821)Sofie Van Landeghembefore adoption06fce7d
lines 117–146why, one step back117def test_list() -> None:118 result = runner.invoke(119 app, ["hello-all-options", "--name", "Rick", "--name", "Morty"]120 )121 assert result.exit_code == 0122 assert "Hello World!" not in result.output123 assert "Hello Rick!" in result.output124 assert "Hello Morty!" in result.output125126 result = runner.invoke(app, ["hello-all-args", "Rick", "Morty"])127 assert result.exit_code == 0128 assert "Hello World!" not in result.output129 assert "Hello Rick!" in result.output130 assert "Hello Morty!" in result.output131132133def test_list_empty() -> None:134 result = runner.invoke(app, ["hello-all-args"])135 assert result.exit_code == 0136 assert "Hello World!" in result.output137138 result = runner.invoke(app, ["hello-all-options"])139 assert result.exit_code == 0140 assert "Hello World!" in result.output141142 result = runner.invoke(app, ["hello-all-envvar"])143 assert result.exit_code == 0144 assert "Hello World!" in result.output145146➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 147–149why, one step back147def test_split_envvar_value(monkeypatch) -> None:148 # This will use split_envvar_value to produce two strings from the envvar149 monkeypatch.setenv("NAMES", "Rick Morty")🐛 Ensure that the default of a list argument is used correctly (#1821)Sofie Van Landeghembefore adoption06fce7d
line 150why, one step back150 result = runner.invoke(app, ["hello-all-envvar"])➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 151–165why, one step back151 assert result.exit_code == 0152 assert "Hello Rick!" in result.output153 assert "Hello Morty!" in result.output154155156def test_list_pair() -> None:157 result = runner.invoke(app, ["split-variadic-and-pair", "a", "b", "c", "x", "y"])158 assert result.exit_code == 0159 assert "items=['a', 'b', 'c']" in result.output160 assert "pair=('x', 'y')" in result.output161162163def test_float_range_open_bounds_with_clamp_not_allowed():164 with pytest.raises(TypeError, match="Clamping is not supported for open bounds."):165 _click.types.FloatRange(min=0.0, min_open=True, clamp=True)