Debian Codegraph @

tests/test_bind_tuple.py

raw symbols SCIP
1 import pytest
2
3 from cyclopts.exceptions import MissingArgumentError
4
5
6 @pytest.mark.parametrize(
7 "cmd_str",
8 [
9 "1 2 80 160 255",
10 "--coordinates 1 2 --color 80 160 255",
11 "--color 80 160 255 --coordinates 1 2",
12 "--color 80 160 255 --coordinates=1 2",
13 ],
14 )
15 def test_bind_tuple_basic(app, cmd_str, assert_parse_args):
16 @app.default
17 def foo(coordinates: tuple[int, int], color: tuple[int, int, int]):
18 pass
19
20 assert_parse_args(foo, cmd_str, (1, 2), (80, 160, 255))
21
22
23 @pytest.mark.parametrize(
24 "cmd_str",
25 [
26 "1 2 alice 100 200",
27 "--coordinates 1 2 --data alice 100 200",
28 "--data alice 100 200 --coordinates 1 2",
29 ],
30 )
31 def test_bind_tuple_nested(app, cmd_str, assert_parse_args):
32 @app.default
33 def foo(coordinates: tuple[int, int], data: tuple[tuple[str, int], int]):
34 pass
35
36 assert_parse_args(foo, cmd_str, (1, 2), (("alice", 100), 200))
37
38
39 @pytest.mark.parametrize(
40 "cmd_str",
41 [
42 "1 2 alice 100 bob 200",
43 "--coordinates 1 2 --data alice 100 --data bob 200",
44 "--data alice 100 --coordinates 1 2 --data bob 200",
45 ],
46 )
47 def test_bind_tuple_ellipsis(app, cmd_str, assert_parse_args):
48 @app.default
49 def foo(coordinates: tuple[int, int], data: tuple[tuple[str, int], ...]):
50 pass
51
52 assert_parse_args(foo, cmd_str, (1, 2), (("alice", 100), ("bob", 200)))
53
54
55 @pytest.mark.parametrize(
56 "cmd_str",
57 [
58 "1 2 3",
59 "--values 1 --values 2 --values 3",
60 ],
61 )
62 def test_bind_tuple_no_inner_types(app, cmd_str, assert_parse_args):
63 @app.default
64 def foo(values: tuple):
65 pass
66
67 # Interpreted as a string because:
68 # 1. Tuple -> Tuple[Any, ...]
69 # 2. Any is treated the same as no annotation.
70 # 3. Even if a default value was supplied, we couldn't unambiguously infer a type.
71 # 4. This falls back to string.
72 assert_parse_args(foo, cmd_str, ("1", "2", "3"))
73
74
75 @pytest.mark.parametrize(
76 "cmd_str",
77 [
78 "1",
79 "--coordinates 1",
80 ],
81 )
82 def test_bind_tuple_insufficient_tokens(app, cmd_str):
83 @app.default
84 def foo(coordinates: tuple[int, int]):
85 pass
86
87 with pytest.raises(MissingArgumentError):
88 app.parse_args(cmd_str, print_error=False, exit_on_error=False)
89
90
91 @pytest.mark.parametrize(
92 "cmd_str",
93 [
94 "--coordinates 1 2 --color 80 160 255 --coordinates 3 4",
95 "--coordinates 1 2 --coordinates 3 4 --color 80 160 255",
96 "1 2 3 4 --color 80 160 255",
97 ],
98 )
99 def test_bind_list_of_tuple(app, cmd_str, assert_parse_args):
100 @app.default
101 def foo(coordinates: list[tuple[int, int]], color: tuple[int, int, int]):
102 pass
103
104 assert_parse_args(foo, cmd_str, [(1, 2), (3, 4)], (80, 160, 255))