| 1 |
from dataclasses import dataclass |
| 2 |
from textwrap import dedent |
| 3 |
from typing import Annotated |
| 4 |
|
| 5 |
import pytest |
| 6 |
from attrs import define |
| 7 |
|
| 8 |
from cyclopts import Parameter |
| 9 |
from cyclopts.exceptions import UnknownOptionError |
| 10 |
|
| 11 |
|
| 12 |
@pytest.mark.parametrize("decorator", [dataclass, define]) |
| 13 |
def test_parameter_decorator_dataclass(app, assert_parse_args, decorator): |
| 14 |
@Parameter(name="*") |
| 15 |
@decorator |
| 16 |
class User: |
| 17 |
name: str |
| 18 |
age: int |
| 19 |
|
| 20 |
@app.command |
| 21 |
def create(*, user: User | None = None): |
| 22 |
pass |
| 23 |
|
| 24 |
assert_parse_args(create, "create") |
| 25 |
assert_parse_args(create, "create --name=Bob --age=100", user=User("Bob", 100)) |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.parametrize("decorator", [dataclass, define]) |
| 29 |
def test_parameter_decorator_dataclass_nested_1(app, decorator, console): |
| 30 |
""" |
| 31 |
https://github.com/BrianPugh/cyclopts/issues/320 |
| 32 |
""" |
| 33 |
|
| 34 |
@decorator |
| 35 |
class S3Path: |
| 36 |
bucket: Annotated[str, Parameter()] |
| 37 |
key: Annotated[str, Parameter()] |
| 38 |
|
| 39 |
@Parameter(name="*") |
| 40 |
@decorator |
| 41 |
class S3CliParams: |
| 42 |
path: Annotated[S3Path, Parameter(name="*")] |
| 43 |
region: Annotated[str, Parameter(name="region")] |
| 44 |
|
| 45 |
@app.command |
| 46 |
def action(*, s3_path: S3CliParams): |
| 47 |
pass |
| 48 |
|
| 49 |
with console.capture() as capture: |
| 50 |
app("action --help", console=console) |
| 51 |
|
| 52 |
actual = capture.get() |
| 53 |
expected = dedent( |
| 54 |
"""\ |
| 55 |
Usage: test_parameter_decorator action --bucket STR --key STR --region |
| 56 |
STR |
| 57 |
|
| 58 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 59 |
│ * --bucket [required] │ |
| 60 |
│ * --key [required] │ |
| 61 |
│ * --region [required] │ |
| 62 |
╰────────────────────────────────────────────────────────────────────╯ |
| 63 |
""" |
| 64 |
) |
| 65 |
assert actual == expected |
| 66 |
|
| 67 |
|
| 68 |
def test_parameter_decorator_dataclass_inheritance(app, assert_parse_args): |
| 69 |
@Parameter(name="person") |
| 70 |
@Parameter(name="u", negative_bool=[]) |
| 71 |
@dataclass |
| 72 |
class User: |
| 73 |
name: str |
| 74 |
age: int |
| 75 |
privileged: bool = False |
| 76 |
|
| 77 |
@Parameter(name="a", negative_bool=None) |
| 78 |
@dataclass |
| 79 |
class Admin(User): |
| 80 |
privileged: bool = True |
| 81 |
|
| 82 |
@app.command |
| 83 |
def create(*, user: User | None = None, admin: Admin | None = None): |
| 84 |
pass |
| 85 |
|
| 86 |
assert_parse_args(create, "create --person.name=Bob --person.age=100", user=User("Bob", 100)) |
| 87 |
with pytest.raises(UnknownOptionError): |
| 88 |
app("create --person.no-privileged", exit_on_error=False) |
| 89 |
|
| 90 |
assert_parse_args(create, "create --a.name=Bob --a.age=100", admin=Admin("Bob", 100)) |
| 91 |
assert_parse_args( |
| 92 |
create, "create --a.name=Bob --a.age=100 --a.no-privileged", admin=Admin("Bob", 100, privileged=False) |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
def test_parameter_class_decorator_with_annotated(app, assert_parse_args): |
| 97 |
"""Test that @Parameter on a class works when the type is wrapped in Annotated. |
| 98 |
|
| 99 |
This is a regression test for a bug where __cyclopts__ was checked before |
| 100 |
unwrapping Annotated, so class-level Parameter settings were ignored. |
| 101 |
""" |
| 102 |
|
| 103 |
@Parameter(allow_leading_hyphen=True) |
| 104 |
class MyType: |
| 105 |
def __init__(self, value: str): |
| 106 |
self.value = value |
| 107 |
|
| 108 |
def __eq__(self, other): |
| 109 |
return self.value == other.value |
| 110 |
|
| 111 |
@app.default |
| 112 |
def foo(arg: Annotated[MyType, Parameter(help="Custom help text.")]): |
| 113 |
pass |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
assert_parse_args(foo, "-", arg=MyType("-")) |
| 118 |
assert_parse_args(foo, "--foo", arg=MyType("--foo")) |
| 119 |
|
| 120 |
|
| 121 |
def test_parameter_class_decorator_with_annotated_optional_and_default(app, assert_parse_args): |
| 122 |
"""Test that @Parameter on a class works with Annotated[Type | None, Parameter(...)] = None. |
| 123 |
|
| 124 |
Regression test for https://github.com/BrianPugh/cyclopts/issues/740 |
| 125 |
|
| 126 |
The bug: When a type hint is Annotated[Type | None, Parameter(...)], the get_parameters |
| 127 |
function would: |
| 128 |
1. Call resolve_optional() on the Annotated type (which does nothing because Annotated is not a Union) |
| 129 |
2. Unwrap Annotated to get Type | None |
| 130 |
3. Try to get __cyclopts__ from Type | None (a Union), which fails |
| 131 |
|
| 132 |
The __cyclopts__ attribute from Type's @Parameter decorator was never extracted, |
| 133 |
so settings like allow_leading_hyphen=True were lost. |
| 134 |
""" |
| 135 |
|
| 136 |
@Parameter(allow_leading_hyphen=True) |
| 137 |
class MyType: |
| 138 |
def __init__(self, value: str): |
| 139 |
self.value = value |
| 140 |
|
| 141 |
def __eq__(self, other): |
| 142 |
return self.value == other.value |
| 143 |
|
| 144 |
@app.default |
| 145 |
def foo( |
| 146 |
required_arg: MyType, |
| 147 |
optional_arg: Annotated[MyType | None, Parameter(name=["--opt", "-o"])] = None, |
| 148 |
): |
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
assert_parse_args(foo, ["-", "-"], MyType("-"), MyType("-")) |
| 154 |
assert_parse_args(foo, ["-", "--opt", "-"], MyType("-"), MyType("-")) |
| 155 |
assert_parse_args(foo, ["-", "-o", "-"], MyType("-"), MyType("-")) |