Debian Codegraph @

tests/test_bind_namedtuple.py

raw symbols SCIP
1 from collections import namedtuple
2 from typing import NamedTuple
3
4
5 def test_bind_typing_named_tuple(app, assert_parse_args):
6 class Employee(NamedTuple):
7 name: str
8 id: int = 3
9
10 @app.command
11 def foo(user: Employee):
12 pass
13
14 assert_parse_args(
15 foo,
16 'foo --user.name="John Smith" --user.id=100',
17 Employee(name="John Smith", id=100),
18 )
19
20 assert_parse_args(
21 foo,
22 'foo "John Smith" 100',
23 Employee(name="John Smith", id=100),
24 )
25
26
27 def test_bind_typing_named_tuple_var_positional(app, assert_parse_args):
28 class Employee(NamedTuple):
29 name: str
30 id: int
31
32 @app.command
33 def foo(*users: Employee):
34 pass
35
36 assert_parse_args(
37 foo,
38 'foo "John Smith" 100 "Mary Jones" 200',
39 Employee(name="John Smith", id=100),
40 Employee(name="Mary Jones", id=200),
41 )
42
43
44 def test_bind_collections_named_tuple(app, assert_parse_args):
45 # All fields will be strings since cyclopts doesn't know the types.
46 Employee = namedtuple("Employee", ["name", "id"])
47
48 @app.command
49 def foo(user: Employee):
50 pass
51
52 assert_parse_args(
53 foo,
54 'foo --user.name="John Smith" --user.id=100',
55 Employee(name="John Smith", id="100"),
56 )