| 1 |
from dataclasses import dataclass |
| 2 |
from enum import Enum, auto |
| 3 |
from textwrap import dedent |
| 4 |
from typing import Annotated |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from cyclopts import App, Parameter, default_name_transform |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.parametrize( |
| 12 |
"before,after", |
| 13 |
[ |
| 14 |
("FOO", "foo"), |
| 15 |
("_FOO", "foo"), |
| 16 |
("_FOO_", "foo"), |
| 17 |
("_F_O_O_", "f-o-o"), |
| 18 |
], |
| 19 |
) |
| 20 |
def test_default_name_transform(before, after): |
| 21 |
assert default_name_transform(before) == after |
| 22 |
|
| 23 |
|
| 24 |
def test_app_name_transform_default(app): |
| 25 |
@app.command |
| 26 |
def _F_O_O_(): |
| 27 |
pass |
| 28 |
|
| 29 |
assert "f-o-o" in app |
| 30 |
|
| 31 |
|
| 32 |
def test_app_name_transform_custom(app): |
| 33 |
def name_transform(s: str) -> str: |
| 34 |
return "my-custom-name-transform" |
| 35 |
|
| 36 |
app.name_transform = name_transform |
| 37 |
|
| 38 |
@app.command |
| 39 |
def foo(): |
| 40 |
pass |
| 41 |
|
| 42 |
assert "my-custom-name-transform" in app |
| 43 |
|
| 44 |
|
| 45 |
def test_subapp_name_transform_custom(app): |
| 46 |
"""A subapp with an explicitly set ``name_transform`` should NOT inherit from parent.""" |
| 47 |
|
| 48 |
def name_transform_1(s: str) -> str: |
| 49 |
return "my-custom-name-transform-1" |
| 50 |
|
| 51 |
def name_transform_2(s: str) -> str: |
| 52 |
return "my-custom-name-transform-2" |
| 53 |
|
| 54 |
app.name_transform = name_transform_1 |
| 55 |
|
| 56 |
app.command(subapp := App(name="bar", name_transform=name_transform_2)) |
| 57 |
|
| 58 |
@subapp.command |
| 59 |
def foo(): |
| 60 |
pass |
| 61 |
|
| 62 |
assert "my-custom-name-transform-2" in subapp |
| 63 |
|
| 64 |
|
| 65 |
def test_subapp_name_transform_custom_inherited(app): |
| 66 |
"""A subapp without an explicitly set ``name_transform`` should inherit it from the first parent.""" |
| 67 |
|
| 68 |
def name_transform(s: str) -> str: |
| 69 |
return "my-custom-name-transform" |
| 70 |
|
| 71 |
app.name_transform = name_transform |
| 72 |
|
| 73 |
app.command(subapp := App(name="bar")) |
| 74 |
|
| 75 |
@subapp.command |
| 76 |
def foo(): |
| 77 |
pass |
| 78 |
|
| 79 |
assert "my-custom-name-transform" in subapp |
| 80 |
|
| 81 |
|
| 82 |
def test_parameter_name_transform_default(app, assert_parse_args): |
| 83 |
@app.default |
| 84 |
def foo(*, b_a_r: int): |
| 85 |
pass |
| 86 |
|
| 87 |
assert_parse_args(foo, "--b-a-r 5", b_a_r=5) |
| 88 |
|
| 89 |
|
| 90 |
def test_parameter_name_transform_custom(app, assert_parse_args): |
| 91 |
app.default_parameter = Parameter(name_transform=lambda s: s) |
| 92 |
|
| 93 |
@app.default |
| 94 |
def foo(*, b_a_r: int): |
| 95 |
pass |
| 96 |
|
| 97 |
assert_parse_args(foo, "--b_a_r 5", b_a_r=5) |
| 98 |
|
| 99 |
|
| 100 |
@pytest.mark.parametrize("transform", [None, lambda s: s]) |
| 101 |
def test_parameter_name_transform_kwargs(app, assert_parse_args, transform): |
| 102 |
"""Both custom and non-custom transforms should result in the same kwargs.""" |
| 103 |
app.default_parameter = Parameter(name_transform=transform) |
| 104 |
|
| 105 |
@app.default |
| 106 |
def foo(**kwargs: int): |
| 107 |
pass |
| 108 |
|
| 109 |
assert_parse_args(foo, "--hy-phen=1 --under_score=2", **{"hy-phen": 1, "under_score": 2}) |
| 110 |
|
| 111 |
|
| 112 |
def test_parameter_name_transform_custom_name_override(app, assert_parse_args): |
| 113 |
app.default_parameter = Parameter(name_transform=lambda s: s) |
| 114 |
|
| 115 |
@app.default |
| 116 |
def foo(*, b_a_r: Annotated[int, Parameter(name="--buzz")]): |
| 117 |
pass |
| 118 |
|
| 119 |
assert_parse_args(foo, "--buzz 5", b_a_r=5) |
| 120 |
|
| 121 |
|
| 122 |
def test_parameter_name_transform_custom_enum(app, assert_parse_args): |
| 123 |
"""name_transform should also be applied to enum options.""" |
| 124 |
app.default_parameter = Parameter(name_transform=lambda s: s) |
| 125 |
|
| 126 |
class SoftwareEnvironment(Enum): |
| 127 |
DEV = auto() |
| 128 |
STAGING = auto() |
| 129 |
PROD = auto() |
| 130 |
_PROD_OLD = auto() |
| 131 |
|
| 132 |
@app.default |
| 133 |
def foo(*, b_a_r: SoftwareEnvironment = SoftwareEnvironment.STAGING): |
| 134 |
pass |
| 135 |
|
| 136 |
assert_parse_args(foo, "--b_a_r PROD", b_a_r=SoftwareEnvironment.PROD) |
| 137 |
|
| 138 |
|
| 139 |
def test_parameter_name_transform_help(app, console): |
| 140 |
app.default_parameter = Parameter(name_transform=lambda s: s) |
| 141 |
|
| 142 |
@app.default |
| 143 |
def foo(*, b_a_r: int): |
| 144 |
pass |
| 145 |
|
| 146 |
with console.capture() as capture: |
| 147 |
app.help_print([], console=console) |
| 148 |
|
| 149 |
actual = capture.get() |
| 150 |
expected = dedent( |
| 151 |
"""\ |
| 152 |
Usage: test_name_transform --b_a_r INT |
| 153 |
|
| 154 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 155 |
│ --help (-h) Display this message and exit. │ |
| 156 |
│ --version Display application version. │ |
| 157 |
╰────────────────────────────────────────────────────────────────────╯ |
| 158 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 159 |
│ * --b_a_r [required] │ |
| 160 |
╰────────────────────────────────────────────────────────────────────╯ |
| 161 |
""" |
| 162 |
) |
| 163 |
assert actual == expected |
| 164 |
|
| 165 |
|
| 166 |
def test_parameter_name_transform_help_enum(app, console): |
| 167 |
"""name_transform should also be applied to enum options on help page.""" |
| 168 |
app.default_parameter = Parameter(name_transform=lambda s: s) |
| 169 |
|
| 170 |
class CompSciProblem(Enum): |
| 171 |
FIZZ = "bleep bloop blop" |
| 172 |
BUZZ = "blop bleep bloop" |
| 173 |
|
| 174 |
@app.command |
| 175 |
def cmd( |
| 176 |
foo: Annotated[CompSciProblem, Parameter(help="Docstring for foo.")] = CompSciProblem.FIZZ, |
| 177 |
bar: Annotated[CompSciProblem, Parameter(help="Docstring for bar.")] = CompSciProblem.BUZZ, |
| 178 |
): |
| 179 |
pass |
| 180 |
|
| 181 |
with console.capture() as capture: |
| 182 |
app.help_print(["cmd"], console=console) |
| 183 |
|
| 184 |
actual = capture.get() |
| 185 |
expected = dedent( |
| 186 |
"""\ |
| 187 |
Usage: test_name_transform cmd [ARGS] |
| 188 |
|
| 189 |
╭─ Parameters ───────────────────────────────────────────────────────╮ |
| 190 |
│ FOO --foo Docstring for foo. [choices: FIZZ, BUZZ] [default: │ |
| 191 |
│ FIZZ] │ |
| 192 |
│ BAR --bar Docstring for bar. [choices: FIZZ, BUZZ] [default: │ |
| 193 |
│ BUZZ] │ |
| 194 |
╰────────────────────────────────────────────────────────────────────╯ |
| 195 |
""" |
| 196 |
) |
| 197 |
assert actual == expected |
| 198 |
|
| 199 |
|
| 200 |
def test_parameter_name_transform_dataclass(app, assert_parse_args): |
| 201 |
app.default_parameter = Parameter(name_transform=lambda s: s.upper()) |
| 202 |
|
| 203 |
@dataclass |
| 204 |
class Color: |
| 205 |
red: int |
| 206 |
green: int |
| 207 |
blue: int |
| 208 |
|
| 209 |
@dataclass |
| 210 |
class User: |
| 211 |
name: str |
| 212 |
favorite_color: Color |
| 213 |
|
| 214 |
@app.default |
| 215 |
def default(user: Annotated[User, Parameter(name="--user")]): |
| 216 |
pass |
| 217 |
|
| 218 |
assert_parse_args( |
| 219 |
default, |
| 220 |
"Bob --user.FAVORITE_COLOR.RED=100 --user.FAVORITE_COLOR.GREEN=200 --user.FAVORITE_COLOR.BLUE=255", |
| 221 |
User("Bob", Color(100, 200, 255)), |
| 222 |
) |