| 1 |
import collections.abc |
| 2 |
from collections.abc import MutableSequence as AbcMutableSequence |
| 3 |
from collections.abc import MutableSet as AbcMutableSet |
| 4 |
from collections.abc import Sequence |
| 5 |
from collections.abc import Set as AbcSet |
| 6 |
from pathlib import Path |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from cyclopts.exceptions import MissingArgumentError |
| 11 |
|
| 12 |
|
| 13 |
def test_pos_list(app, assert_parse_args): |
| 14 |
@app.command |
| 15 |
def foo(a: list[int]): |
| 16 |
pass |
| 17 |
|
| 18 |
assert_parse_args(foo, "foo 1 2 3", [1, 2, 3]) |
| 19 |
|
| 20 |
|
| 21 |
def test_keyword_list(app, assert_parse_args): |
| 22 |
@app.command |
| 23 |
def foo(a: list[int]): |
| 24 |
pass |
| 25 |
|
| 26 |
assert_parse_args(foo, "foo --a=1 --a=2 --a 3", [1, 2, 3]) |
| 27 |
|
| 28 |
|
| 29 |
def test_keyword_list_mutable_default(app, assert_parse_args): |
| 30 |
@app.command |
| 31 |
def foo(a: list[int] = []): |
| 32 |
pass |
| 33 |
|
| 34 |
assert_parse_args(foo, "foo --a=1 --a=2 --a 3", [1, 2, 3]) |
| 35 |
assert_parse_args(foo, "foo") |
| 36 |
|
| 37 |
|
| 38 |
def test_keyword_list_pos(app, assert_parse_args): |
| 39 |
@app.command |
| 40 |
def foo(a: list[int]): |
| 41 |
pass |
| 42 |
|
| 43 |
assert_parse_args(foo, "foo 1 2 3", [1, 2, 3]) |
| 44 |
|
| 45 |
|
| 46 |
def test_keyword_optional_list_none_default(app, assert_parse_args): |
| 47 |
@app.command |
| 48 |
def foo(a: list[int] | None = None): |
| 49 |
pass |
| 50 |
|
| 51 |
assert_parse_args(foo, "foo") |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize( |
| 55 |
"cmd_expected", |
| 56 |
[ |
| 57 |
("", None), |
| 58 |
("--verbose", [True]), |
| 59 |
("--verbose --verbose", [True, True]), |
| 60 |
("--verbose --verbose --no-verbose", [True, True, False]), |
| 61 |
("--verbose --verbose=False", [True, False]), |
| 62 |
("--verbose --no-verbose=False", [True, True]), |
| 63 |
("--verbose --verbose=True", [True, True]), |
| 64 |
], |
| 65 |
) |
| 66 |
def test_keyword_list_of_bool(app, assert_parse_args, cmd_expected): |
| 67 |
cmd, expected = cmd_expected |
| 68 |
|
| 69 |
@app.default |
| 70 |
def foo(*, verbose: list[bool] | None = None): |
| 71 |
pass |
| 72 |
|
| 73 |
if expected is None: |
| 74 |
assert_parse_args(foo, cmd) |
| 75 |
else: |
| 76 |
assert_parse_args(foo, cmd, verbose=expected) |
| 77 |
|
| 78 |
|
| 79 |
@pytest.mark.parametrize( |
| 80 |
"cmd_expected", |
| 81 |
[ |
| 82 |
("", None), |
| 83 |
("--verbose", (True,)), |
| 84 |
("--verbose --verbose", (True, True)), |
| 85 |
("--verbose --verbose --no-verbose", (True, True, False)), |
| 86 |
("--verbose --verbose=False", (True, False)), |
| 87 |
("--verbose --no-verbose=False", (True, True)), |
| 88 |
("--verbose --verbose=True", (True, True)), |
| 89 |
], |
| 90 |
) |
| 91 |
def test_keyword_tuple_of_bool(app, assert_parse_args, cmd_expected): |
| 92 |
cmd, expected = cmd_expected |
| 93 |
|
| 94 |
@app.default |
| 95 |
def foo(*, verbose: tuple[bool, ...] | None = None): |
| 96 |
pass |
| 97 |
|
| 98 |
if expected is None: |
| 99 |
assert_parse_args(foo, cmd) |
| 100 |
else: |
| 101 |
assert_parse_args(foo, cmd, verbose=expected) |
| 102 |
|
| 103 |
|
| 104 |
@pytest.mark.parametrize( |
| 105 |
"cmd_expected", |
| 106 |
[ |
| 107 |
("", None), |
| 108 |
("--verbose", [True]), |
| 109 |
("--verbose --verbose", [True, True]), |
| 110 |
("--verbose --verbose --no-verbose", [True, True, False]), |
| 111 |
("--verbose --verbose=False", [True, False]), |
| 112 |
("--verbose --no-verbose=False", [True, True]), |
| 113 |
("--verbose --verbose=True", [True, True]), |
| 114 |
], |
| 115 |
) |
| 116 |
@pytest.mark.parametrize("hint", [Sequence[bool], collections.abc.Sequence[bool]]) |
| 117 |
def test_keyword_sequence_of_bool(app, assert_parse_args, cmd_expected, hint): |
| 118 |
cmd, expected = cmd_expected |
| 119 |
|
| 120 |
@app.default |
| 121 |
def foo(*, verbose: hint | None = None): |
| 122 |
pass |
| 123 |
|
| 124 |
if expected is None: |
| 125 |
assert_parse_args(foo, cmd) |
| 126 |
else: |
| 127 |
assert_parse_args(foo, cmd, verbose=expected) |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.parametrize( |
| 131 |
"cmd", |
| 132 |
[ |
| 133 |
"foo --item", |
| 134 |
], |
| 135 |
) |
| 136 |
def test_list_tuple_missing_arguments_no_arguments(app, cmd): |
| 137 |
"""Missing values.""" |
| 138 |
|
| 139 |
@app.command |
| 140 |
def foo(item: list[tuple[int, str]]): |
| 141 |
pass |
| 142 |
|
| 143 |
with pytest.raises(MissingArgumentError): |
| 144 |
app(cmd, exit_on_error=False) |
| 145 |
|
| 146 |
|
| 147 |
@pytest.mark.parametrize( |
| 148 |
"cmd", |
| 149 |
[ |
| 150 |
"foo --item 1", |
| 151 |
"foo --item a --stuff g", |
| 152 |
], |
| 153 |
) |
| 154 |
def test_list_tuple_missing_arguments_non_divisible(app, cmd): |
| 155 |
"""Missing values.""" |
| 156 |
|
| 157 |
@app.command |
| 158 |
def foo(item: list[tuple[int, str]], stuff: str = ""): |
| 159 |
pass |
| 160 |
|
| 161 |
with pytest.raises(MissingArgumentError): |
| 162 |
app(cmd, exit_on_error=False) |
| 163 |
|
| 164 |
|
| 165 |
def test_pos_sequence(app, assert_parse_args): |
| 166 |
@app.command |
| 167 |
def foo(a: Sequence[int]): |
| 168 |
pass |
| 169 |
|
| 170 |
assert_parse_args(foo, "foo 1 2 3", [1, 2, 3]) |
| 171 |
|
| 172 |
|
| 173 |
@pytest.mark.parametrize( |
| 174 |
"cmd_str", |
| 175 |
[ |
| 176 |
"fizz buzz bar", |
| 177 |
"-- fizz buzz bar", |
| 178 |
"fizz -- buzz bar", |
| 179 |
"fizz buzz -- bar", |
| 180 |
"fizz buzz bar --", |
| 181 |
], |
| 182 |
) |
| 183 |
def test_list_positional_all_but_last(app, cmd_str, assert_parse_args): |
| 184 |
@app.default |
| 185 |
def foo(inputs: list[Path], output: Path, /): |
| 186 |
pass |
| 187 |
|
| 188 |
assert_parse_args(foo, cmd_str, [Path("fizz"), Path("buzz")], Path("bar")) |
| 189 |
|
| 190 |
|
| 191 |
@pytest.mark.parametrize( |
| 192 |
"hint,expected", |
| 193 |
[ |
| 194 |
(AbcSet[str], {"1", "2", "3"}), |
| 195 |
(AbcMutableSet[str], {"1", "2", "3"}), |
| 196 |
(AbcMutableSequence[str], ["1", "2", "3"]), |
| 197 |
(collections.abc.Set, {"1", "2", "3"}), |
| 198 |
(collections.abc.MutableSet, {"1", "2", "3"}), |
| 199 |
(collections.abc.MutableSequence, ["1", "2", "3"]), |
| 200 |
], |
| 201 |
) |
| 202 |
def test_abstract_collection_types(app, assert_parse_args, hint, expected): |
| 203 |
"""Test that collections.abc abstract types work (issue #702). |
| 204 |
|
| 205 |
Tests both parameterized types (e.g., Set[str]) and bare types (e.g., Set). |
| 206 |
Bare abstract types should default to [str] like bare concrete types. |
| 207 |
""" |
| 208 |
|
| 209 |
@app.default |
| 210 |
def main(test: hint): |
| 211 |
return test |
| 212 |
|
| 213 |
assert_parse_args(main, "1 2 3", expected) |