| 1 |
from collections import namedtuple |
| 2 |
from typing import Annotated, Dict, Optional, TypedDict, Union |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from cyclopts.annotations import is_typeddict |
| 7 |
from cyclopts.argument import ( |
| 8 |
Argument, |
| 9 |
ArgumentCollection, |
| 10 |
_resolve_groups_from_callable, |
| 11 |
resolve_parameter_name, |
| 12 |
) |
| 13 |
from cyclopts.group import Group |
| 14 |
from cyclopts.parameter import Parameter |
| 15 |
from cyclopts.token import Token |
| 16 |
from cyclopts.utils import UNSET |
| 17 |
|
| 18 |
Case = namedtuple("TestCase", ["args", "expected"]) |
| 19 |
|
| 20 |
|
| 21 |
def test_argument_collection_no_annotation_no_default(): |
| 22 |
def foo(a, b): |
| 23 |
pass |
| 24 |
|
| 25 |
collection = ArgumentCollection._from_callable(foo) |
| 26 |
|
| 27 |
assert len(collection) == 2 |
| 28 |
|
| 29 |
assert collection[0].field_info.name == "a" |
| 30 |
assert collection[0].hint is str |
| 31 |
assert collection[0].keys == () |
| 32 |
assert collection[0]._accepts_keywords is False |
| 33 |
|
| 34 |
assert collection[1].field_info.name == "b" |
| 35 |
assert collection[1].hint is str |
| 36 |
assert collection[1].keys == () |
| 37 |
assert collection[1]._accepts_keywords is False |
| 38 |
|
| 39 |
|
| 40 |
def test_argument_collection_no_annotation_default(): |
| 41 |
def foo(a="foo", b=100): |
| 42 |
pass |
| 43 |
|
| 44 |
collection = ArgumentCollection._from_callable(foo) |
| 45 |
|
| 46 |
assert len(collection) == 2 |
| 47 |
|
| 48 |
assert collection[0].field_info.name == "a" |
| 49 |
assert collection[0].hint is str |
| 50 |
assert collection[0].keys == () |
| 51 |
assert collection[0]._accepts_keywords is False |
| 52 |
|
| 53 |
assert collection[1].field_info.name == "b" |
| 54 |
assert collection[1].hint is int |
| 55 |
assert collection[1].keys == () |
| 56 |
assert collection[1]._accepts_keywords is False |
| 57 |
|
| 58 |
|
| 59 |
def test_argument_collection_basic_annotation(): |
| 60 |
def foo(a: str, b: int): |
| 61 |
pass |
| 62 |
|
| 63 |
collection = ArgumentCollection._from_callable(foo) |
| 64 |
|
| 65 |
assert len(collection) == 2 |
| 66 |
|
| 67 |
assert collection[0].field_info.name == "a" |
| 68 |
assert collection[0].hint is str |
| 69 |
assert collection[0].keys == () |
| 70 |
assert collection[0]._accepts_keywords is False |
| 71 |
|
| 72 |
assert collection[1].field_info.name == "b" |
| 73 |
assert collection[1].hint is int |
| 74 |
assert collection[1].keys == () |
| 75 |
assert collection[1]._accepts_keywords is False |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.parametrize("type_", [dict, Dict]) |
| 79 |
def test_argument_collection_bare_dict(type_): |
| 80 |
def foo(a: type_, b: int): |
| 81 |
pass |
| 82 |
|
| 83 |
collection = ArgumentCollection._from_callable(foo) |
| 84 |
|
| 85 |
assert len(collection) == 2 |
| 86 |
|
| 87 |
assert collection[0].field_info.name == "a" |
| 88 |
assert collection[0].parameter.name == ("--a",) |
| 89 |
assert collection[0].hint is type_ |
| 90 |
assert collection[0].keys == () |
| 91 |
assert collection[0]._accepts_keywords is True |
| 92 |
assert collection[0]._accepts_arbitrary_keywords is True |
| 93 |
|
| 94 |
assert collection[1].field_info.name == "b" |
| 95 |
assert collection[1].parameter.name == ("--b",) |
| 96 |
assert collection[1].hint is int |
| 97 |
assert collection[1].keys == () |
| 98 |
assert collection[1]._accepts_keywords is False |
| 99 |
|
| 100 |
|
| 101 |
def test_argument_collection_typing_dict(): |
| 102 |
def foo(a: dict[str, int], b: int): |
| 103 |
pass |
| 104 |
|
| 105 |
collection = ArgumentCollection._from_callable(foo) |
| 106 |
|
| 107 |
assert len(collection) == 2 |
| 108 |
|
| 109 |
assert collection[0].field_info.name == "a" |
| 110 |
assert collection[0].hint == dict[str, int] |
| 111 |
assert collection[0].keys == () |
| 112 |
assert collection[0]._accepts_keywords is True |
| 113 |
assert collection[0]._accepts_arbitrary_keywords is True |
| 114 |
|
| 115 |
assert collection[1].field_info.name == "b" |
| 116 |
assert collection[1].hint is int |
| 117 |
assert collection[1].keys == () |
| 118 |
assert collection[1]._accepts_keywords is False |
| 119 |
|
| 120 |
|
| 121 |
def test_argument_collection_typeddict(): |
| 122 |
class ExampleTypedDict(TypedDict): |
| 123 |
foo: str |
| 124 |
bar: int |
| 125 |
|
| 126 |
def foo(a: ExampleTypedDict, b: int): |
| 127 |
pass |
| 128 |
|
| 129 |
collection = ArgumentCollection._from_callable(foo) |
| 130 |
|
| 131 |
assert len(collection) == 4 |
| 132 |
|
| 133 |
assert collection[0].field_info.name == "a" |
| 134 |
assert collection[0].parameter.name == ("--a",) |
| 135 |
assert collection[0].hint is ExampleTypedDict |
| 136 |
assert collection[0].keys == () |
| 137 |
assert collection[0]._accepts_keywords is True |
| 138 |
assert collection[0].children |
| 139 |
|
| 140 |
assert collection[1].field_info.name == "foo" |
| 141 |
assert collection[1].parameter.name == ("--a.foo",) |
| 142 |
assert collection[1].hint is str |
| 143 |
assert collection[1].keys == ("foo",) |
| 144 |
assert collection[1]._accepts_keywords is False |
| 145 |
assert not collection[1].children |
| 146 |
|
| 147 |
assert collection[2].field_info.name == "bar" |
| 148 |
assert collection[2].parameter.name == ("--a.bar",) |
| 149 |
assert collection[2].hint is int |
| 150 |
assert collection[2].keys == ("bar",) |
| 151 |
assert collection[2]._accepts_keywords is False |
| 152 |
assert not collection[2].children |
| 153 |
|
| 154 |
assert collection[3].field_info.name == "b" |
| 155 |
assert collection[3].parameter.name == ("--b",) |
| 156 |
assert collection[3].hint is int |
| 157 |
assert collection[3].keys == () |
| 158 |
assert collection[3]._accepts_keywords is False |
| 159 |
assert not collection[3].children |
| 160 |
|
| 161 |
|
| 162 |
def test_argument_collection_typeddict_nested(): |
| 163 |
class Inner(TypedDict): |
| 164 |
fizz: float |
| 165 |
buzz: Annotated[complex, Parameter(name="bazz")] |
| 166 |
|
| 167 |
class ExampleTypedDict(TypedDict): |
| 168 |
foo: Inner |
| 169 |
bar: int |
| 170 |
|
| 171 |
def foo(a: ExampleTypedDict, b: int): |
| 172 |
pass |
| 173 |
|
| 174 |
collection = ArgumentCollection._from_callable(foo) |
| 175 |
|
| 176 |
assert len(collection) == 6 |
| 177 |
|
| 178 |
assert collection[0].field_info.name == "a" |
| 179 |
assert collection[0].parameter.name == ("--a",) |
| 180 |
assert collection[0].hint is ExampleTypedDict |
| 181 |
assert collection[0].keys == () |
| 182 |
assert collection[0]._accepts_keywords is True |
| 183 |
assert collection[0].children |
| 184 |
|
| 185 |
assert collection[1].field_info.name == "foo" |
| 186 |
assert collection[1].parameter.name == ("--a.foo",) |
| 187 |
assert collection[1].hint is Inner |
| 188 |
assert collection[1].keys == ("foo",) |
| 189 |
assert collection[1]._accepts_keywords is True |
| 190 |
assert collection[1].children |
| 191 |
|
| 192 |
assert collection[2].field_info.name == "fizz" |
| 193 |
assert collection[2].parameter.name == ("--a.foo.fizz",) |
| 194 |
assert collection[2].hint is float |
| 195 |
assert collection[2].keys == ("foo", "fizz") |
| 196 |
assert collection[2]._accepts_keywords is False |
| 197 |
assert not collection[2].children |
| 198 |
|
| 199 |
assert collection[3].field_info.name == "buzz" |
| 200 |
assert collection[3].parameter.name == ("--a.foo.bazz",) |
| 201 |
assert collection[3].hint is complex |
| 202 |
assert collection[3].keys == ("foo", "buzz") |
| 203 |
assert collection[3]._accepts_keywords is False |
| 204 |
assert not collection[3].children |
| 205 |
|
| 206 |
assert collection[4].field_info.name == "bar" |
| 207 |
assert collection[4].parameter.name == ("--a.bar",) |
| 208 |
assert collection[4].hint is int |
| 209 |
assert collection[4].keys == ("bar",) |
| 210 |
assert collection[4]._accepts_keywords is False |
| 211 |
assert not collection[4].children |
| 212 |
|
| 213 |
assert collection[5].field_info.name == "b" |
| 214 |
assert collection[5].parameter.name == ("--b",) |
| 215 |
assert collection[5].hint is int |
| 216 |
assert collection[5].keys == () |
| 217 |
assert collection[5]._accepts_keywords is False |
| 218 |
assert not collection[5].children |
| 219 |
|
| 220 |
|
| 221 |
def test_argument_collection_typeddict_annotated_keys_name_change(): |
| 222 |
class ExampleTypedDict(TypedDict): |
| 223 |
foo: Annotated[str, Parameter(name="fizz")] |
| 224 |
bar: Annotated[int, Parameter(name="buzz")] |
| 225 |
|
| 226 |
def foo(a: ExampleTypedDict, b: int): |
| 227 |
pass |
| 228 |
|
| 229 |
collection = ArgumentCollection._from_callable(foo) |
| 230 |
|
| 231 |
assert len(collection) == 4 |
| 232 |
|
| 233 |
assert collection[0].field_info.name == "a" |
| 234 |
assert collection[0].parameter.name == ("--a",) |
| 235 |
assert collection[0].hint is ExampleTypedDict |
| 236 |
assert collection[0].keys == () |
| 237 |
assert collection[0]._accepts_keywords is True |
| 238 |
assert collection[0].children |
| 239 |
|
| 240 |
assert collection[1].field_info.name == "foo" |
| 241 |
assert collection[1].parameter.name == ("--a.fizz",) |
| 242 |
assert collection[1].hint is str |
| 243 |
assert collection[1].keys == ("foo",) |
| 244 |
assert collection[1]._accepts_keywords is False |
| 245 |
assert not collection[1].children |
| 246 |
|
| 247 |
assert collection[2].field_info.name == "bar" |
| 248 |
assert collection[2].parameter.name == ("--a.buzz",) |
| 249 |
assert collection[2].hint is int |
| 250 |
assert collection[2].keys == ("bar",) |
| 251 |
assert collection[2]._accepts_keywords is False |
| 252 |
assert not collection[2].children |
| 253 |
|
| 254 |
assert collection[3].field_info.name == "b" |
| 255 |
assert collection[3].parameter.name == ("--b",) |
| 256 |
assert collection[3].hint is int |
| 257 |
assert collection[3].keys == () |
| 258 |
assert collection[3]._accepts_keywords is False |
| 259 |
assert not collection[3].children |
| 260 |
|
| 261 |
|
| 262 |
def test_argument_collection_typeddict_annotated_keys_name_override(): |
| 263 |
class ExampleTypedDict(TypedDict): |
| 264 |
foo: Annotated[str, Parameter(name="--fizz")] |
| 265 |
bar: Annotated[int, Parameter(name="--buzz")] |
| 266 |
|
| 267 |
def foo(a: ExampleTypedDict, b: int): |
| 268 |
pass |
| 269 |
|
| 270 |
collection = ArgumentCollection._from_callable(foo) |
| 271 |
|
| 272 |
assert len(collection) == 4 |
| 273 |
|
| 274 |
assert collection[0].field_info.name == "a" |
| 275 |
assert collection[0].parameter.name == ("--a",) |
| 276 |
assert collection[0].hint is ExampleTypedDict |
| 277 |
assert collection[0].keys == () |
| 278 |
assert collection[0]._accepts_keywords is True |
| 279 |
assert collection[0].children |
| 280 |
|
| 281 |
assert collection[1].field_info.name == "foo" |
| 282 |
assert collection[1].parameter.name == ("--fizz",) |
| 283 |
assert collection[1].hint is str |
| 284 |
assert collection[1].keys == ("foo",) |
| 285 |
assert collection[1]._accepts_keywords is False |
| 286 |
assert not collection[1].children |
| 287 |
|
| 288 |
assert collection[2].field_info.name == "bar" |
| 289 |
assert collection[2].parameter.name == ("--buzz",) |
| 290 |
assert collection[2].hint is int |
| 291 |
assert collection[2].keys == ("bar",) |
| 292 |
assert collection[2]._accepts_keywords is False |
| 293 |
assert not collection[2].children |
| 294 |
|
| 295 |
assert collection[3].field_info.name == "b" |
| 296 |
assert collection[3].parameter.name == ("--b",) |
| 297 |
assert collection[3].hint is int |
| 298 |
assert collection[3].keys == () |
| 299 |
assert collection[3]._accepts_keywords is False |
| 300 |
assert not collection[3].children |
| 301 |
|
| 302 |
|
| 303 |
def test_argument_collection_typeddict_flatten_root(): |
| 304 |
class ExampleTypedDict(TypedDict): |
| 305 |
foo: str |
| 306 |
bar: int |
| 307 |
|
| 308 |
def foo(a: Annotated[ExampleTypedDict, Parameter(name="*")], b: int): |
| 309 |
pass |
| 310 |
|
| 311 |
collection = ArgumentCollection._from_callable(foo) |
| 312 |
|
| 313 |
assert collection[0].field_info.name == "a" |
| 314 |
assert collection[0].parameter.name == ("*",) |
| 315 |
assert collection[0].hint is ExampleTypedDict |
| 316 |
assert collection[0].keys == () |
| 317 |
assert collection[0]._accepts_keywords is True |
| 318 |
assert collection[0].children |
| 319 |
|
| 320 |
assert collection[1].field_info.name == "foo" |
| 321 |
assert collection[1].parameter.name == ("--foo",) |
| 322 |
assert collection[1].hint is str |
| 323 |
assert collection[1].keys == ("foo",) |
| 324 |
assert collection[1]._accepts_keywords is False |
| 325 |
assert not collection[1].children |
| 326 |
|
| 327 |
assert collection[2].field_info.name == "bar" |
| 328 |
assert collection[2].parameter.name == ("--bar",) |
| 329 |
assert collection[2].hint is int |
| 330 |
assert collection[2].keys == ("bar",) |
| 331 |
assert collection[2]._accepts_keywords is False |
| 332 |
assert not collection[2].children |
| 333 |
|
| 334 |
assert collection[3].field_info.name == "b" |
| 335 |
assert collection[3].parameter.name == ("--b",) |
| 336 |
assert collection[3].hint is int |
| 337 |
assert collection[3].keys == () |
| 338 |
assert collection[3]._accepts_keywords is False |
| 339 |
assert not collection[3].children |
| 340 |
|
| 341 |
|
| 342 |
def test_argument_collection_var_positional(): |
| 343 |
def foo(a: int, *b: float): |
| 344 |
pass |
| 345 |
|
| 346 |
collection = ArgumentCollection._from_callable(foo) |
| 347 |
|
| 348 |
assert len(collection) == 2 |
| 349 |
|
| 350 |
assert collection[0].parameter.name == ("--a",) |
| 351 |
assert collection[0].hint is int |
| 352 |
assert collection[0].keys == () |
| 353 |
assert collection[0]._accepts_keywords is False |
| 354 |
|
| 355 |
assert collection[1].field_info.name == "b" |
| 356 |
assert collection[1].parameter.name == ("B",) |
| 357 |
assert collection[1].hint == tuple[float, ...] |
| 358 |
assert collection[1].keys == () |
| 359 |
assert collection[1]._accepts_keywords is False |
| 360 |
|
| 361 |
|
| 362 |
def test_argument_collection_var_keyword(): |
| 363 |
def foo(a: int, **b: float): |
| 364 |
pass |
| 365 |
|
| 366 |
collection = ArgumentCollection._from_callable(foo) |
| 367 |
|
| 368 |
assert len(collection) == 2 |
| 369 |
|
| 370 |
assert collection[0].field_info.name == "a" |
| 371 |
assert collection[0].parameter.name == ("--a",) |
| 372 |
assert collection[0].hint is int |
| 373 |
assert collection[0].keys == () |
| 374 |
assert collection[0]._accepts_keywords is False |
| 375 |
|
| 376 |
assert collection[1].field_info.name == "b" |
| 377 |
assert collection[1].parameter.name == ("--[KEYWORD]",) |
| 378 |
assert collection[1].hint == dict[str, float] |
| 379 |
assert collection[1].keys == () |
| 380 |
assert collection[1]._accepts_keywords is True |
| 381 |
|
| 382 |
|
| 383 |
def test_argument_collection_var_keyword_named(): |
| 384 |
def foo(a: int, **b: Annotated[float, Parameter(name=("--foo", "--bar"))]): |
| 385 |
pass |
| 386 |
|
| 387 |
collection = ArgumentCollection._from_callable(foo) |
| 388 |
|
| 389 |
assert len(collection) == 2 |
| 390 |
|
| 391 |
assert collection[0].field_info.name == "a" |
| 392 |
assert collection[0].parameter.name == ("--a",) |
| 393 |
assert collection[0].hint is int |
| 394 |
assert collection[0].keys == () |
| 395 |
assert collection[0]._accepts_keywords is False |
| 396 |
|
| 397 |
assert collection[1].field_info.name == "b" |
| 398 |
assert collection[1].parameter.name == ("--foo", "--bar") |
| 399 |
assert collection[1].hint == dict[str, float] |
| 400 |
assert collection[1].keys == () |
| 401 |
assert collection[1]._accepts_keywords is True |
| 402 |
|
| 403 |
|
| 404 |
def test_argument_collection_var_keyword_match(): |
| 405 |
def foo(a: int, **b: float): |
| 406 |
pass |
| 407 |
|
| 408 |
collection = ArgumentCollection._from_callable(foo) |
| 409 |
|
| 410 |
argument, keys, _ = collection.match("--fizz") |
| 411 |
assert keys == ("fizz",) |
| 412 |
assert argument.field_info.name == "b" |
| 413 |
|
| 414 |
|
| 415 |
@pytest.mark.parametrize( |
| 416 |
"args, expected", |
| 417 |
[ |
| 418 |
Case(args=(), expected=()), |
| 419 |
Case(args=(("foo",),), expected=("--foo",)), |
| 420 |
Case(args=(("--foo",),), expected=("--foo",)), |
| 421 |
Case(args=(("--foo", "--bar"),), expected=("--foo", "--bar")), |
| 422 |
Case(args=(("--foo",), ("--bar",)), expected=("--bar",)), |
| 423 |
Case(args=(("--foo",), ("baz",)), expected=("--foo.baz",)), |
| 424 |
Case(args=(("--foo",), ("--bar", "baz")), expected=("--bar", "--foo.baz")), |
| 425 |
Case(args=(("--foo", "--bar"), ("baz",)), expected=("--foo.baz", "--bar.baz")), |
| 426 |
Case(args=(("*",), ("bar",)), expected=("--bar",)), |
| 427 |
Case(args=(("--foo", "*"), ("bar",)), expected=("--foo.bar", "--bar")), |
| 428 |
Case(args=(("--foo",), ("*",), ("bar",)), expected=("--foo.bar",)), |
| 429 |
Case(args=(("foo",), ("--bar",)), expected=("--bar",)), |
| 430 |
Case(args=(("foo",), ("bar",)), expected=("--foo.bar",)), |
| 431 |
], |
| 432 |
) |
| 433 |
def test_resolve_parameter_name(args, expected): |
| 434 |
assert resolve_parameter_name(*args) == expected |
| 435 |
|
| 436 |
|
| 437 |
def test_resolve_groups_from_callable(): |
| 438 |
class User(TypedDict): |
| 439 |
name: Annotated[str, Parameter(group="Inside Typed Dict")] |
| 440 |
age: Annotated[int, Parameter(group="Inside Typed Dict")] |
| 441 |
height: float |
| 442 |
|
| 443 |
def build( |
| 444 |
config1: str, |
| 445 |
config2: Annotated[str, Parameter()], |
| 446 |
flag1: Annotated[bool, Parameter(group="Flags")] = False, |
| 447 |
flag2: Annotated[bool, Parameter(group=("Flags", "Other Flags"))] = False, |
| 448 |
user: User | None = None, |
| 449 |
): |
| 450 |
pass |
| 451 |
|
| 452 |
actual = _resolve_groups_from_callable(build) |
| 453 |
assert actual == [ |
| 454 |
Group.create_default_parameters(), |
| 455 |
Group("Flags"), |
| 456 |
Group("Other Flags"), |
| 457 |
Group("Inside Typed Dict"), |
| 458 |
] |
| 459 |
|
| 460 |
|
| 461 |
def test_argument_convert(): |
| 462 |
argument = Argument( |
| 463 |
hint=list[int], |
| 464 |
tokens=[ |
| 465 |
Token(value="42", source="test"), |
| 466 |
Token(value="70", source="test"), |
| 467 |
], |
| 468 |
) |
| 469 |
assert argument.convert() == [42, 70] |
| 470 |
|
| 471 |
|
| 472 |
def test_argument_convert_dict(): |
| 473 |
def foo(bar: dict[str, int]): |
| 474 |
pass |
| 475 |
|
| 476 |
collection = ArgumentCollection._from_callable(foo) |
| 477 |
|
| 478 |
assert len(collection) == 1 |
| 479 |
argument = collection[0] |
| 480 |
|
| 481 |
|
| 482 |
assert argument.match("--bar.buzz") == (("buzz",), UNSET) |
| 483 |
|
| 484 |
argument.append(Token(value="7", source="test", keys=("fizz",))) |
| 485 |
argument.append(Token(value="12", source="test", keys=("buzz",))) |
| 486 |
|
| 487 |
assert argument.convert() == {"fizz": 7, "buzz": 12} |
| 488 |
|
| 489 |
|
| 490 |
def test_argument_convert_var_keyword(): |
| 491 |
def foo(**kwargs: int): |
| 492 |
pass |
| 493 |
|
| 494 |
collection = ArgumentCollection._from_callable(foo) |
| 495 |
|
| 496 |
assert len(collection) == 1 |
| 497 |
argument = collection[0] |
| 498 |
|
| 499 |
|
| 500 |
assert argument.match("--fizz") == (("fizz",), UNSET) |
| 501 |
|
| 502 |
argument.append(Token(value="7", source="test", keys=("fizz",))) |
| 503 |
argument.append(Token(value="12", source="test", keys=("buzz",))) |
| 504 |
|
| 505 |
assert argument.convert() == {"fizz": 7, "buzz": 12} |
| 506 |
|
| 507 |
|
| 508 |
def test_argument_convert_cparam_provided(): |
| 509 |
def my_converter(type_, tokens): |
| 510 |
return f"my_converter_{tokens[0].value}" |
| 511 |
|
| 512 |
argument = Argument( |
| 513 |
hint=str, |
| 514 |
tokens=[Token(value="my_value", source="test")], |
| 515 |
parameter=Parameter( |
| 516 |
converter=my_converter, |
| 517 |
), |
| 518 |
) |
| 519 |
assert argument.convert() == "my_converter_my_value" |
| 520 |
|
| 521 |
|
| 522 |
class ExampleTypedDict(TypedDict): |
| 523 |
foo: str |
| 524 |
bar: int |
| 525 |
|
| 526 |
|
| 527 |
@pytest.mark.parametrize( |
| 528 |
"hint", |
| 529 |
[ |
| 530 |
ExampleTypedDict, |
| 531 |
Optional[ExampleTypedDict], |
| 532 |
Annotated[ExampleTypedDict, "foo"], |
| 533 |
|
| 534 |
Union[ExampleTypedDict, str, int], |
| 535 |
], |
| 536 |
) |
| 537 |
def test_is_typed_dict_true(hint): |
| 538 |
assert is_typeddict(hint) |
| 539 |
|
| 540 |
|
| 541 |
@pytest.mark.parametrize( |
| 542 |
"hint", |
| 543 |
[ |
| 544 |
list, |
| 545 |
dict, |
| 546 |
dict, |
| 547 |
dict[str, int], |
| 548 |
], |
| 549 |
) |
| 550 |
def test_is_typed_dict_false(hint): |
| 551 |
assert not is_typeddict(hint) |
| 552 |
|
| 553 |
|
| 554 |
def test_assemble_argument_collection_no_default_command(): |
| 555 |
from cyclopts import App |
| 556 |
|
| 557 |
app = App() |
| 558 |
|
| 559 |
@app.command |
| 560 |
def foo(loops: int): |
| 561 |
pass |
| 562 |
|
| 563 |
with pytest.raises(ValueError, match="Cannot assemble argument collection: no default command is registered"): |
| 564 |
app.assemble_argument_collection() |
| 565 |
|
| 566 |
|
| 567 |
def test_argument_collection_getitem_by_string(): |
| 568 |
def foo(alpha: int, beta: str): |
| 569 |
pass |
| 570 |
|
| 571 |
collection = ArgumentCollection._from_callable(foo) |
| 572 |
|
| 573 |
assert collection["--alpha"].field_info.name == "alpha" |
| 574 |
assert collection["--alpha"].hint is int |
| 575 |
assert collection["--beta"].field_info.name == "beta" |
| 576 |
assert collection["--beta"].hint is str |
| 577 |
|
| 578 |
|
| 579 |
@pytest.mark.parametrize( |
| 580 |
"slice_obj, expected_names", |
| 581 |
[ |
| 582 |
(slice(1, 3), ["beta", "gamma"]), |
| 583 |
(slice(5, 10), []), |
| 584 |
(slice(-2, None), ["beta", "gamma"]), |
| 585 |
], |
| 586 |
) |
| 587 |
def test_argument_collection_getitem_by_slice(slice_obj, expected_names): |
| 588 |
def foo(alpha: int, beta: str, gamma: float): |
| 589 |
pass |
| 590 |
|
| 591 |
collection = ArgumentCollection._from_callable(foo) |
| 592 |
subset = collection[slice_obj] |
| 593 |
assert isinstance(subset, list) |
| 594 |
assert [arg.field_info.name for arg in subset] == expected_names |
| 595 |
|
| 596 |
|
| 597 |
def test_argument_collection_getitem_by_int(): |
| 598 |
def foo(alpha: int, beta: str, gamma: float): |
| 599 |
pass |
| 600 |
|
| 601 |
collection = ArgumentCollection._from_callable(foo) |
| 602 |
|
| 603 |
assert collection[0].field_info.name == "alpha" |
| 604 |
assert collection[1].field_info.name == "beta" |
| 605 |
assert collection[2].field_info.name == "gamma" |
| 606 |
|
| 607 |
|
| 608 |
@pytest.mark.parametrize( |
| 609 |
"term, default, expected", |
| 610 |
[ |
| 611 |
("--nonexistent", "my_default", "my_default"), |
| 612 |
(10, "my_default", "my_default"), |
| 613 |
], |
| 614 |
) |
| 615 |
def test_argument_collection_get_with_default(term, default, expected): |
| 616 |
def foo(alpha: int, beta: str): |
| 617 |
pass |
| 618 |
|
| 619 |
collection = ArgumentCollection._from_callable(foo) |
| 620 |
assert collection.get(term, default=default) == expected |
| 621 |
|
| 622 |
|
| 623 |
@pytest.mark.parametrize( |
| 624 |
"term, exception_type, match_pattern", |
| 625 |
[ |
| 626 |
("--nonexistent", KeyError, "No such Argument: --nonexistent"), |
| 627 |
(10, IndexError, "Argument index 10 out of range"), |
| 628 |
], |
| 629 |
) |
| 630 |
def test_argument_collection_get_not_found(term, exception_type, match_pattern): |
| 631 |
def foo(alpha: int, beta: str): |
| 632 |
pass |
| 633 |
|
| 634 |
collection = ArgumentCollection._from_callable(foo) |
| 635 |
with pytest.raises(exception_type, match=match_pattern): |
| 636 |
collection.get(term) |
| 637 |
|
| 638 |
|
| 639 |
def test_argument_collection_getitem_not_found(): |
| 640 |
def foo(alpha: int, beta: str): |
| 641 |
pass |
| 642 |
|
| 643 |
collection = ArgumentCollection._from_callable(foo) |
| 644 |
|
| 645 |
with pytest.raises(KeyError, match="No such Argument: --nonexistent"): |
| 646 |
_ = collection["--nonexistent"] |
| 647 |
|
| 648 |
with pytest.raises(IndexError): |
| 649 |
_ = collection[10] |
| 650 |
|
| 651 |
|
| 652 |
def test_argument_collection_contains_with_string(): |
| 653 |
"""Test that 'in' operator works with string argument names.""" |
| 654 |
|
| 655 |
def foo(alpha: int, beta: str): |
| 656 |
pass |
| 657 |
|
| 658 |
collection = ArgumentCollection._from_callable(foo) |
| 659 |
|
| 660 |
|
| 661 |
assert "--alpha" in collection |
| 662 |
assert "--beta" in collection |
| 663 |
|
| 664 |
|
| 665 |
assert "--nonexistent" not in collection |
| 666 |
assert "--gamma" not in collection |
| 667 |
|
| 668 |
|
| 669 |
def test_argument_collection_contains_with_alias(): |
| 670 |
"""Test that 'in' operator works with argument aliases.""" |
| 671 |
|
| 672 |
def foo( |
| 673 |
alpha: Annotated[int, Parameter(name="--foo", alias="-f")], |
| 674 |
beta: Annotated[str, Parameter(name="--bar", alias=("-b", "--baz"))], |
| 675 |
): |
| 676 |
pass |
| 677 |
|
| 678 |
collection = ArgumentCollection._from_callable(foo) |
| 679 |
|
| 680 |
|
| 681 |
assert "--foo" in collection |
| 682 |
assert "--bar" in collection |
| 683 |
|
| 684 |
|
| 685 |
assert "-f" in collection |
| 686 |
assert "-b" in collection |
| 687 |
assert "--baz" in collection |
| 688 |
|
| 689 |
|
| 690 |
assert "--alpha" not in collection |
| 691 |
assert "--beta" not in collection |
| 692 |
|
| 693 |
|
| 694 |
def test_argument_collection_contains_with_object(): |
| 695 |
"""Test that 'in' operator works with Argument objects (backward compatibility).""" |
| 696 |
|
| 697 |
def foo(alpha: int, beta: str): |
| 698 |
pass |
| 699 |
|
| 700 |
collection = ArgumentCollection._from_callable(foo) |
| 701 |
|
| 702 |
|
| 703 |
assert collection[0] in collection |
| 704 |
assert collection[1] in collection |
| 705 |
|
| 706 |
|
| 707 |
new_arg = Argument(parameter=Parameter(name="--gamma"), hint=float) |
| 708 |
assert new_arg not in collection |
| 709 |
|
| 710 |
|
| 711 |
def test_argument_collection_contains_with_filtered(): |
| 712 |
"""Test that 'in' operator works with filtered collections (common validator pattern).""" |
| 713 |
collection = ArgumentCollection( |
| 714 |
[ |
| 715 |
Argument( |
| 716 |
tokens=[Token(keyword="--foo", value="100", source="test")], |
| 717 |
parameter=Parameter(name="--foo"), |
| 718 |
value=100, |
| 719 |
), |
| 720 |
Argument( |
| 721 |
parameter=Parameter(name="--bar"), |
| 722 |
), |
| 723 |
Argument( |
| 724 |
tokens=[Token(keyword="--baz", value="text", source="test")], |
| 725 |
parameter=Parameter(name="--baz"), |
| 726 |
value="text", |
| 727 |
), |
| 728 |
] |
| 729 |
) |
| 730 |
|
| 731 |
|
| 732 |
assert "--foo" in collection |
| 733 |
assert "--bar" in collection |
| 734 |
assert "--baz" in collection |
| 735 |
|
| 736 |
|
| 737 |
populated = collection.filter_by(value_set=True) |
| 738 |
assert "--foo" in populated |
| 739 |
assert "--bar" not in populated |
| 740 |
assert "--baz" in populated |
| 741 |
|
| 742 |
|
| 743 |
def test_argument_collection_contains_with_keys(): |
| 744 |
"""Test that 'in' operator works with nested argument keys (dataclass fields).""" |
| 745 |
from dataclasses import dataclass |
| 746 |
|
| 747 |
@dataclass |
| 748 |
class Config: |
| 749 |
host: str |
| 750 |
port: int |
| 751 |
|
| 752 |
def foo(config: Config): |
| 753 |
pass |
| 754 |
|
| 755 |
collection = ArgumentCollection._from_callable(foo) |
| 756 |
|
| 757 |
|
| 758 |
assert "--config.host" in collection |
| 759 |
assert "--config.port" in collection |
| 760 |
|
| 761 |
|
| 762 |
assert "--config" in collection |
| 763 |
|
| 764 |
|
| 765 |
assert "--config.username" not in collection |