| 1 |
import sys |
| 2 |
from textwrap import dedent |
| 3 |
from typing import Annotated, TypedDict |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from cyclopts import MissingArgumentError, Parameter |
| 8 |
from cyclopts.exceptions import UnknownOptionError |
| 9 |
|
| 10 |
if sys.version_info < (3, 11): |
| 11 |
from typing_extensions import NotRequired, Required |
| 12 |
else: |
| 13 |
from typing import NotRequired, Required |
| 14 |
|
| 15 |
|
| 16 |
class MyDict(TypedDict): |
| 17 |
my_int: int |
| 18 |
my_str: str |
| 19 |
my_list: list |
| 20 |
my_list_int: list[int] |
| 21 |
|
| 22 |
|
| 23 |
def test_bind_typed_dict(app, assert_parse_args): |
| 24 |
@app.command |
| 25 |
def foo(d: MyDict): |
| 26 |
pass |
| 27 |
|
| 28 |
assert_parse_args( |
| 29 |
foo, |
| 30 |
"foo --d.my-int=5 --d.my-str=bar --d.my-list=a --d.my-list=b --d.my-list-int=1 --d.my-list-int=2", |
| 31 |
d={ |
| 32 |
"my_int": 5, |
| 33 |
"my_str": "bar", |
| 34 |
"my_list": ["a", "b"], |
| 35 |
"my_list_int": [1, 2], |
| 36 |
}, |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
def test_bind_typed_dict_missing_arg_basic(app, console): |
| 41 |
@app.command |
| 42 |
def foo(d: MyDict): |
| 43 |
pass |
| 44 |
|
| 45 |
with console.capture() as capture, pytest.raises(MissingArgumentError): |
| 46 |
app( |
| 47 |
"foo --d.my-int=5 --d.my-str=bar", |
| 48 |
error_console=console, |
| 49 |
exit_on_error=False, |
| 50 |
) |
| 51 |
|
| 52 |
actual = capture.get() |
| 53 |
|
| 54 |
expected = dedent( |
| 55 |
"""\ |
| 56 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 57 |
│ Command "foo" parameter --d.my-list requires an argument. │ |
| 58 |
╰────────────────────────────────────────────────────────────────────╯ |
| 59 |
""" |
| 60 |
) |
| 61 |
|
| 62 |
assert actual == expected |
| 63 |
|
| 64 |
|
| 65 |
def test_bind_typed_dict_missing_arg_flatten(app, console): |
| 66 |
@app.command |
| 67 |
def foo(d: Annotated[MyDict, Parameter(name="*")]): |
| 68 |
pass |
| 69 |
|
| 70 |
with console.capture() as capture, pytest.raises(MissingArgumentError): |
| 71 |
app( |
| 72 |
"foo", |
| 73 |
error_console=console, |
| 74 |
exit_on_error=False, |
| 75 |
) |
| 76 |
|
| 77 |
actual = capture.get() |
| 78 |
|
| 79 |
expected = dedent( |
| 80 |
"""\ |
| 81 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 82 |
│ Command "foo" parameter --my-int requires an argument. │ |
| 83 |
╰────────────────────────────────────────────────────────────────────╯ |
| 84 |
""" |
| 85 |
) |
| 86 |
|
| 87 |
assert actual == expected |
| 88 |
|
| 89 |
|
| 90 |
def test_bind_typed_dict_missing_arg_renamed_no_hyphen(app, console): |
| 91 |
class MyDict(TypedDict): |
| 92 |
my_int: int |
| 93 |
my_str: str |
| 94 |
my_list: Annotated[list, Parameter(name="your-list")] |
| 95 |
my_list_int: list[int] |
| 96 |
|
| 97 |
@app.command |
| 98 |
def foo(d: MyDict): |
| 99 |
pass |
| 100 |
|
| 101 |
with console.capture() as capture, pytest.raises(MissingArgumentError): |
| 102 |
app( |
| 103 |
"foo --d.my-int=5 --d.my-str=bar", |
| 104 |
error_console=console, |
| 105 |
exit_on_error=False, |
| 106 |
) |
| 107 |
|
| 108 |
actual = capture.get() |
| 109 |
|
| 110 |
expected = dedent( |
| 111 |
"""\ |
| 112 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 113 |
│ Command "foo" parameter --d.your-list requires an argument. │ |
| 114 |
╰────────────────────────────────────────────────────────────────────╯ |
| 115 |
""" |
| 116 |
) |
| 117 |
|
| 118 |
assert actual == expected |
| 119 |
|
| 120 |
|
| 121 |
def test_bind_typed_dict_missing_arg_renamed_hyphen(app, console): |
| 122 |
class MyDict(TypedDict): |
| 123 |
my_int: int |
| 124 |
my_str: str |
| 125 |
my_list: Annotated[list, Parameter(name="--your-list")] |
| 126 |
my_list_int: list[int] |
| 127 |
|
| 128 |
@app.command |
| 129 |
def foo(d: MyDict): |
| 130 |
pass |
| 131 |
|
| 132 |
with console.capture() as capture, pytest.raises(MissingArgumentError): |
| 133 |
app( |
| 134 |
"foo --d.my-int=5 --d.my-str=bar", |
| 135 |
error_console=console, |
| 136 |
exit_on_error=False, |
| 137 |
) |
| 138 |
|
| 139 |
actual = capture.get() |
| 140 |
|
| 141 |
expected = dedent( |
| 142 |
"""\ |
| 143 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 144 |
│ Command "foo" parameter --your-list requires an argument. │ |
| 145 |
╰────────────────────────────────────────────────────────────────────╯ |
| 146 |
""" |
| 147 |
) |
| 148 |
|
| 149 |
assert actual == expected |
| 150 |
|
| 151 |
|
| 152 |
def test_bind_typed_dict_missing_arg_nested(app, console): |
| 153 |
class User(TypedDict): |
| 154 |
name: str |
| 155 |
age: int |
| 156 |
|
| 157 |
class MyDict(TypedDict): |
| 158 |
my_int: int |
| 159 |
my_str: str |
| 160 |
my_user: User |
| 161 |
|
| 162 |
@app.command |
| 163 |
def foo(d: MyDict): |
| 164 |
pass |
| 165 |
|
| 166 |
with console.capture() as capture, pytest.raises(MissingArgumentError): |
| 167 |
app( |
| 168 |
"foo --d.my-int=5 --d.my-str=bar --d.my-user.age=30", |
| 169 |
error_console=console, |
| 170 |
exit_on_error=False, |
| 171 |
) |
| 172 |
|
| 173 |
actual = capture.get() |
| 174 |
|
| 175 |
expected = dedent( |
| 176 |
"""\ |
| 177 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 178 |
│ Command "foo" parameter --d.my-user.name requires an argument. │ |
| 179 |
╰────────────────────────────────────────────────────────────────────╯ |
| 180 |
""" |
| 181 |
) |
| 182 |
|
| 183 |
assert actual == expected |
| 184 |
|
| 185 |
|
| 186 |
def test_bind_typed_dict_total_false(app, assert_parse_args): |
| 187 |
class MyDict(TypedDict, total=False): |
| 188 |
my_int: int |
| 189 |
my_str: str |
| 190 |
|
| 191 |
@app.command |
| 192 |
def foo(d: MyDict): |
| 193 |
pass |
| 194 |
|
| 195 |
assert_parse_args(foo, "foo --d.my-str=bar", d={"my_str": "bar"}) |
| 196 |
|
| 197 |
|
| 198 |
def test_bind_typed_dict_not_required(app, assert_parse_args): |
| 199 |
class MyDict(TypedDict): |
| 200 |
my_int: int |
| 201 |
my_str: NotRequired[str] |
| 202 |
|
| 203 |
@app.command |
| 204 |
def foo(d: MyDict): |
| 205 |
pass |
| 206 |
|
| 207 |
assert_parse_args(foo, "foo --d.my-int=5", d={"my_int": 5}) |
| 208 |
|
| 209 |
|
| 210 |
def test_bind_typed_dict_required(app, assert_parse_args): |
| 211 |
class MyDict(TypedDict, total=False): |
| 212 |
my_int: Required[int] |
| 213 |
my_str: str |
| 214 |
|
| 215 |
@app.command |
| 216 |
def foo(d: MyDict): |
| 217 |
pass |
| 218 |
|
| 219 |
assert_parse_args(foo, "foo --d.my-int=5", d={"my_int": 5}) |
| 220 |
|
| 221 |
|
| 222 |
def test_bind_typed_dict_extra_field(app, console): |
| 223 |
@app.command |
| 224 |
def foo(d: MyDict): |
| 225 |
pass |
| 226 |
|
| 227 |
with console.capture() as capture, pytest.raises(UnknownOptionError): |
| 228 |
app.parse_args( |
| 229 |
"foo --d.my-int=5 --d.my-str=bar --d.my-list=a --d.my-list=b --d.my-list-int=1 --d.my-list-int=2 --d.extra-key=10", |
| 230 |
error_console=console, |
| 231 |
exit_on_error=False, |
| 232 |
) |
| 233 |
|
| 234 |
actual = capture.get() |
| 235 |
|
| 236 |
expected = dedent( |
| 237 |
"""\ |
| 238 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 239 |
│ Unknown option: --d.extra-key. │ |
| 240 |
╰────────────────────────────────────────────────────────────────────╯ |
| 241 |
""" |
| 242 |
) |
| 243 |
|
| 244 |
assert actual == expected |