| 1 |
from textwrap import dedent |
| 2 |
from typing import Annotated |
| 3 |
|
| 4 |
import pytest |
| 5 |
from attrs import define, field |
| 6 |
|
| 7 |
from cyclopts import Parameter |
| 8 |
from cyclopts.exceptions import MissingArgumentError, UnknownOptionError |
| 9 |
|
| 10 |
|
| 11 |
@define |
| 12 |
class Outfit: |
| 13 |
body: str |
| 14 |
head: str |
| 15 |
|
| 16 |
|
| 17 |
@define |
| 18 |
class User: |
| 19 |
id: int |
| 20 |
name: str = "John Doe" |
| 21 |
tastes: dict[str, int] = field(factory=dict) |
| 22 |
outfit: Outfit | None = None |
| 23 |
admin: Annotated[bool, Parameter(negative="not-admin")] = False |
| 24 |
vip: Annotated[bool, Parameter(negative="--not-vip")] = False |
| 25 |
staff: Annotated[bool, Parameter(parse=False)] = False |
| 26 |
|
| 27 |
|
| 28 |
def test_bind_attrs(app, assert_parse_args, console): |
| 29 |
@app.command |
| 30 |
def foo(user: User): |
| 31 |
pass |
| 32 |
|
| 33 |
assert_parse_args( |
| 34 |
foo, |
| 35 |
"foo --user.id=123 --user.tastes.wine=9 --user.tastes.cheese=7 --user.tastes.cabbage=1 --user.outfit.body=t-shirt --user.outfit.head=baseball-cap --user.admin", |
| 36 |
User( |
| 37 |
id=123, |
| 38 |
tastes={"wine": 9, "cheese": 7, "cabbage": 1}, |
| 39 |
outfit=Outfit(body="t-shirt", head="baseball-cap"), |
| 40 |
admin=True, |
| 41 |
), |
| 42 |
) |
| 43 |
|
| 44 |
with console.capture() as capture: |
| 45 |
app("foo --help", console=console) |
| 46 |
|
| 47 |
actual = capture.get() |
| 48 |
|
| 49 |
expected = dedent( |
| 50 |
"""\ |
| 51 |
Usage: test_bind_attrs foo USER.ID [ARGS] |
| 52 |
|
| 53 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 54 |
│ * USER.ID --user.id [required] │ |
| 55 |
│ USER.NAME --user.name [default: John Doe] │ |
| 56 |
│ --user.tastes │ |
| 57 |
│ --user.outfit.body │ |
| 58 |
│ --user.outfit.head │ |
| 59 |
│ --user.admin [default: False] │ |
| 60 |
│ --user.not-admin │ |
| 61 |
│ --user.vip --not-vip [default: False] │ |
| 62 |
╰────────────────────────────────────────────────────────────────────╯ |
| 63 |
""" |
| 64 |
) |
| 65 |
assert actual == expected |
| 66 |
|
| 67 |
|
| 68 |
def test_bind_attrs_flatten(app, assert_parse_args, console): |
| 69 |
@app.command |
| 70 |
def foo(user: Annotated[User, Parameter(name="*")]): |
| 71 |
pass |
| 72 |
|
| 73 |
assert_parse_args( |
| 74 |
foo, |
| 75 |
"foo --id=123 --tastes.wine=9 --tastes.cheese=7 --tastes.cabbage=1 --outfit.body=t-shirt --outfit.head=baseball-cap --admin", |
| 76 |
User( |
| 77 |
id=123, |
| 78 |
tastes={"wine": 9, "cheese": 7, "cabbage": 1}, |
| 79 |
outfit=Outfit(body="t-shirt", head="baseball-cap"), |
| 80 |
admin=True, |
| 81 |
), |
| 82 |
) |
| 83 |
|
| 84 |
with console.capture() as capture: |
| 85 |
app("foo --help", console=console) |
| 86 |
|
| 87 |
actual = capture.get() |
| 88 |
|
| 89 |
expected = dedent( |
| 90 |
"""\ |
| 91 |
Usage: test_bind_attrs foo ID [ARGS] |
| 92 |
|
| 93 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 94 |
│ * ID --id [required] │ |
| 95 |
│ NAME --name [default: John Doe] │ |
| 96 |
│ --tastes │ |
| 97 |
│ --outfit.body │ |
| 98 |
│ --outfit.head │ |
| 99 |
│ --admin --not-admin [default: False] │ |
| 100 |
│ --vip --not-vip [default: False] │ |
| 101 |
╰────────────────────────────────────────────────────────────────────╯ |
| 102 |
""" |
| 103 |
) |
| 104 |
assert actual == expected |
| 105 |
|
| 106 |
|
| 107 |
def test_bind_attrs_accepts_keys_false(app, assert_parse_args, console): |
| 108 |
@define |
| 109 |
class SimpleClass: |
| 110 |
value: int |
| 111 |
name: str |
| 112 |
|
| 113 |
@app.command |
| 114 |
def foo(example: Annotated[SimpleClass, Parameter(accepts_keys=False)]): |
| 115 |
pass |
| 116 |
|
| 117 |
assert_parse_args(foo, "foo 5 foo", SimpleClass(5, "foo")) |
| 118 |
assert_parse_args(foo, "foo --example=5 foo", SimpleClass(5, "foo")) |
| 119 |
|
| 120 |
with console.capture() as capture: |
| 121 |
app("foo --help", console=console) |
| 122 |
|
| 123 |
actual = capture.get() |
| 124 |
|
| 125 |
expected = dedent( |
| 126 |
"""\ |
| 127 |
Usage: test_bind_attrs foo EXAMPLE |
| 128 |
|
| 129 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 130 |
│ * EXAMPLE --example [required] │ |
| 131 |
╰────────────────────────────────────────────────────────────────────╯ |
| 132 |
""" |
| 133 |
) |
| 134 |
assert actual == expected |
| 135 |
|
| 136 |
|
| 137 |
def test_bind_attrs_kw_only(app, assert_parse_args): |
| 138 |
@define |
| 139 |
class Engine: |
| 140 |
cylinders: int |
| 141 |
volume: float |
| 142 |
power: Annotated[float, Parameter(name="--power")] = field(kw_only=True) |
| 143 |
|
| 144 |
@app.default |
| 145 |
def default(engine: Engine): |
| 146 |
pass |
| 147 |
|
| 148 |
assert_parse_args(default, "4 100 --power=200", Engine(4, 100, power=200)) |
| 149 |
assert_parse_args(default, "--power=200 4 100", Engine(4, 100, power=200)) |
| 150 |
assert_parse_args(default, "4 --power=200 100", Engine(4, 100, power=200)) |
| 151 |
with pytest.raises(MissingArgumentError): |
| 152 |
app.parse_args("4 100 200", exit_on_error=False) |
| 153 |
|
| 154 |
|
| 155 |
def test_bind_attrs_unknown_option(app, assert_parse_args): |
| 156 |
@define |
| 157 |
class Engine: |
| 158 |
cylinders: int |
| 159 |
volume: float |
| 160 |
|
| 161 |
@app.default |
| 162 |
def default(engine: Engine): |
| 163 |
pass |
| 164 |
|
| 165 |
with pytest.raises(UnknownOptionError): |
| 166 |
app("--engine.cylinders 4 --this-parameter-does-not-exist 100", exit_on_error=False) |
| 167 |
|
| 168 |
|
| 169 |
def test_bind_attrs_alias(app, assert_parse_args): |
| 170 |
@define |
| 171 |
class Engine: |
| 172 |
cylinders: int |
| 173 |
volume: float = field(alias="cc") |
| 174 |
|
| 175 |
@app.default |
| 176 |
def default(engine: Engine): |
| 177 |
pass |
| 178 |
|
| 179 |
assert_parse_args(default, "--engine.cylinders 4 --engine.cc 100", Engine(cylinders=4, cc=100.0)) |
| 180 |
|
| 181 |
with pytest.raises(UnknownOptionError): |
| 182 |
app("--engine.cylinders 4 --engine.volume 100", exit_on_error=False) |
| 183 |
|
| 184 |
|
| 185 |
def test_attrs_field_metadata_help(app, console): |
| 186 |
"""Test that attrs field metadata={"help": "..."} is used for help text.""" |
| 187 |
|
| 188 |
@define |
| 189 |
class Config: |
| 190 |
name: str = field(default="default", metadata={"help": "Help from metadata."}) |
| 191 |
|
| 192 |
age: Annotated[int, Parameter(help="Parameter help takes precedence.")] = field( |
| 193 |
default=25, metadata={"help": "This metadata help is ignored."} |
| 194 |
) |
| 195 |
|
| 196 |
count: int = field(default=10) |
| 197 |
"""Docstring for count.""" |
| 198 |
|
| 199 |
size: int = field(default=5, metadata={"help": "Metadata help overrides docstring."}) |
| 200 |
"""This docstring is ignored.""" |
| 201 |
|
| 202 |
@app.default |
| 203 |
def main(config: Config): |
| 204 |
pass |
| 205 |
|
| 206 |
with console.capture() as capture: |
| 207 |
app("--help", console=console) |
| 208 |
|
| 209 |
actual = capture.get() |
| 210 |
|
| 211 |
assert "Help from metadata." in actual |
| 212 |
|
| 213 |
assert "Parameter help takes precedence." in actual |
| 214 |
assert "This metadata help is ignored." not in actual |
| 215 |
|
| 216 |
assert "Docstring for count." in actual |
| 217 |
|
| 218 |
assert "Metadata help overrides docstring." in actual |
| 219 |
assert "This docstring is ignored." not in actual |
| 220 |
|
| 221 |
|
| 222 |
def test_attrs_inheritance_simple(app, console): |
| 223 |
"""Test that docstrings from base attrs class are inherited by derived class.""" |
| 224 |
|
| 225 |
@define |
| 226 |
class BaseClass: |
| 227 |
"""Base class.""" |
| 228 |
|
| 229 |
some_arg: int = 42 |
| 230 |
"""BaseClass.some_arg docstring.""" |
| 231 |
|
| 232 |
@define |
| 233 |
class DerivedClass(BaseClass): |
| 234 |
"""Derived class.""" |
| 235 |
|
| 236 |
some_other_arg: str = "some_other_arg default value" |
| 237 |
"""DerivedClass.some_other_arg docstring.""" |
| 238 |
|
| 239 |
@app.default |
| 240 |
def main(params: DerivedClass): |
| 241 |
pass |
| 242 |
|
| 243 |
with console.capture() as capture: |
| 244 |
app("--help", console=console) |
| 245 |
|
| 246 |
actual = capture.get() |
| 247 |
|
| 248 |
|
| 249 |
assert "BaseClass.some_arg docstring." in actual |
| 250 |
assert "DerivedClass.some_other_arg docstring." in actual |