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