| 1 |
"""Tests for structured types with various parameter kinds (POSITIONAL_ONLY, VAR_POSITIONAL, etc).""" |
| 2 |
|
| 3 |
import sys |
| 4 |
from dataclasses import dataclass, field |
| 5 |
from typing import Annotated |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from cyclopts import Parameter |
| 10 |
|
| 11 |
|
| 12 |
class GenericClassWithMixedKinds: |
| 13 |
"""A generic class that uses __init__ with mixed parameter kinds.""" |
| 14 |
|
| 15 |
def __init__(self, pos_only: int, /, normal: str, *args: int, kw_only: float, **kwargs: str): |
| 16 |
self.pos_only = pos_only |
| 17 |
self.normal = normal |
| 18 |
self.args = args |
| 19 |
self.kw_only = kw_only |
| 20 |
self.kwargs = kwargs |
| 21 |
|
| 22 |
def __eq__(self, other): |
| 23 |
if not isinstance(other, GenericClassWithMixedKinds): |
| 24 |
return NotImplemented |
| 25 |
return ( |
| 26 |
self.pos_only == other.pos_only |
| 27 |
and self.normal == other.normal |
| 28 |
and self.args == other.args |
| 29 |
and self.kw_only == other.kw_only |
| 30 |
and self.kwargs == other.kwargs |
| 31 |
) |
| 32 |
|
| 33 |
def __repr__(self): |
| 34 |
return ( |
| 35 |
f"GenericClassWithMixedKinds(pos_only={self.pos_only}, normal={self.normal}, " |
| 36 |
f"args={self.args}, kw_only={self.kw_only}, kwargs={self.kwargs})" |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
class GenericClassPositionalOnly: |
| 41 |
"""A class with only positional-only parameters.""" |
| 42 |
|
| 43 |
def __init__(self, a: int, b: str, /): |
| 44 |
self.a = a |
| 45 |
self.b = b |
| 46 |
|
| 47 |
def __eq__(self, other): |
| 48 |
if not isinstance(other, GenericClassPositionalOnly): |
| 49 |
return NotImplemented |
| 50 |
return self.a == other.a and self.b == other.b |
| 51 |
|
| 52 |
def __repr__(self): |
| 53 |
return f"GenericClassPositionalOnly(a={self.a}, b={self.b})" |
| 54 |
|
| 55 |
|
| 56 |
class GenericClassKeywordOnly: |
| 57 |
"""A class with only keyword-only parameters.""" |
| 58 |
|
| 59 |
def __init__(self, *, x: int, y: str): |
| 60 |
self.x = x |
| 61 |
self.y = y |
| 62 |
|
| 63 |
def __eq__(self, other): |
| 64 |
if not isinstance(other, GenericClassKeywordOnly): |
| 65 |
return NotImplemented |
| 66 |
return self.x == other.x and self.y == other.y |
| 67 |
|
| 68 |
def __repr__(self): |
| 69 |
return f"GenericClassKeywordOnly(x={self.x}, y={self.y})" |
| 70 |
|
| 71 |
|
| 72 |
class GenericClassPositionalOnlyAndKeywordOnly: |
| 73 |
"""A class with both positional-only and keyword-only parameters.""" |
| 74 |
|
| 75 |
def __init__(self, a: int, /, *, z: str): |
| 76 |
self.a = a |
| 77 |
self.z = z |
| 78 |
|
| 79 |
def __eq__(self, other): |
| 80 |
if not isinstance(other, GenericClassPositionalOnlyAndKeywordOnly): |
| 81 |
return NotImplemented |
| 82 |
return self.a == other.a and self.z == other.z |
| 83 |
|
| 84 |
def __repr__(self): |
| 85 |
return f"GenericClassPositionalOnlyAndKeywordOnly(a={self.a}, z={self.z})" |
| 86 |
|
| 87 |
|
| 88 |
def test_generic_class_positional_only(app, assert_parse_args): |
| 89 |
"""Test that POSITIONAL_ONLY parameters are passed as positional arguments.""" |
| 90 |
|
| 91 |
@app.default |
| 92 |
def cmd(obj: Annotated[GenericClassPositionalOnly, Parameter(name="*")]): |
| 93 |
return obj |
| 94 |
|
| 95 |
assert_parse_args(cmd, "42 hello", obj=GenericClassPositionalOnly(42, "hello")) |
| 96 |
|
| 97 |
|
| 98 |
def test_generic_class_keyword_only(app, assert_parse_args): |
| 99 |
"""Test that KEYWORD_ONLY parameters are passed as keyword arguments.""" |
| 100 |
|
| 101 |
@app.default |
| 102 |
def cmd(obj: Annotated[GenericClassKeywordOnly, Parameter(name="*")]): |
| 103 |
return obj |
| 104 |
|
| 105 |
|
| 106 |
assert_parse_args(cmd, "--x 10 --y world", obj=GenericClassKeywordOnly(x=10, y="world")) |
| 107 |
|
| 108 |
|
| 109 |
def test_generic_class_keyword_only_positionally(app, assert_parse_args): |
| 110 |
"""Test keyword-only class with accepts_keys=False (forces positional parsing).""" |
| 111 |
|
| 112 |
@app.default |
| 113 |
def cmd(obj: Annotated[GenericClassKeywordOnly, Parameter(name="obj", accepts_keys=False)]): |
| 114 |
return obj |
| 115 |
|
| 116 |
|
| 117 |
assert_parse_args(cmd, "--obj 10 world", obj=GenericClassKeywordOnly(x=10, y="world")) |
| 118 |
|
| 119 |
|
| 120 |
def test_generic_class_positional_only_and_keyword_only(app, assert_parse_args): |
| 121 |
"""Test mixed POSITIONAL_ONLY and KEYWORD_ONLY parameters.""" |
| 122 |
|
| 123 |
@app.default |
| 124 |
def cmd(obj: Annotated[GenericClassPositionalOnlyAndKeywordOnly, Parameter(name="*")]): |
| 125 |
return obj |
| 126 |
|
| 127 |
|
| 128 |
assert_parse_args(cmd, "5 --z test", obj=GenericClassPositionalOnlyAndKeywordOnly(5, z="test")) |
| 129 |
|
| 130 |
|
| 131 |
assert_parse_args(cmd, "--z test 5", obj=GenericClassPositionalOnlyAndKeywordOnly(5, z="test")) |
| 132 |
|
| 133 |
|
| 134 |
@dataclass |
| 135 |
class DataclassWithKwOnly: |
| 136 |
"""Dataclass with kw_only fields.""" |
| 137 |
|
| 138 |
a: int |
| 139 |
b: str = field(kw_only=True) |
| 140 |
c: float = field(default=3.14, kw_only=True) |
| 141 |
|
| 142 |
|
| 143 |
def test_dataclass_kw_only_mixed(app, assert_parse_args): |
| 144 |
"""Test dataclass with mix of normal and kw_only fields.""" |
| 145 |
|
| 146 |
@app.default |
| 147 |
def cmd(config: Annotated[DataclassWithKwOnly, Parameter(name="*")]): |
| 148 |
return config |
| 149 |
|
| 150 |
|
| 151 |
assert_parse_args(cmd, "100 --b hello --c 2.5", config=DataclassWithKwOnly(a=100, b="hello", c=2.5)) |
| 152 |
|
| 153 |
|
| 154 |
assert_parse_args(cmd, "--b hello 100 --c 2.5", config=DataclassWithKwOnly(a=100, b="hello", c=2.5)) |
| 155 |
|
| 156 |
|
| 157 |
assert_parse_args(cmd, "100 --b world", config=DataclassWithKwOnly(a=100, b="world")) |
| 158 |
|
| 159 |
|
| 160 |
@dataclass(kw_only=True) |
| 161 |
class DataclassFullyKwOnly: |
| 162 |
"""Fully keyword-only dataclass.""" |
| 163 |
|
| 164 |
name: str |
| 165 |
age: int |
| 166 |
|
| 167 |
|
| 168 |
def test_dataclass_fully_kw_only(app, assert_parse_args): |
| 169 |
"""Test dataclass that is entirely kw_only.""" |
| 170 |
|
| 171 |
@app.default |
| 172 |
def cmd(person: Annotated[DataclassFullyKwOnly, Parameter(name="*")]): |
| 173 |
return person |
| 174 |
|
| 175 |
|
| 176 |
assert_parse_args(cmd, "--name Alice --age 30", person=DataclassFullyKwOnly(name="Alice", age=30)) |
| 177 |
|
| 178 |
|
| 179 |
def test_dataclass_fully_kw_only_with_accepts_keys_false(app, assert_parse_args): |
| 180 |
"""Test that kw_only dataclass works with accepts_keys=False. |
| 181 |
|
| 182 |
This is the exact scenario from issue #648. |
| 183 |
""" |
| 184 |
|
| 185 |
@dataclass(kw_only=True) |
| 186 |
class Config: |
| 187 |
name: str |
| 188 |
|
| 189 |
@app.default |
| 190 |
def cmd(config: Annotated[Config, Parameter(name="config", accepts_keys=False)]): |
| 191 |
return config |
| 192 |
|
| 193 |
assert_parse_args(cmd, "--config Bob", config=Config(name="Bob")) |
| 194 |
|
| 195 |
|
| 196 |
@pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires Python 3.10+ for positional-only in dataclass") |
| 197 |
def test_dataclass_positional_only_and_keyword_only(): |
| 198 |
"""Test dataclass with both positional-only and keyword-only fields. |
| 199 |
|
| 200 |
Note: Python dataclasses don't directly support positional-only parameters. |
| 201 |
This test is included for completeness but may not be applicable. |
| 202 |
""" |
| 203 |
|
| 204 |
|
| 205 |
pass |
| 206 |
|
| 207 |
|
| 208 |
def test_generic_class_all_keyword_or_positional(app, assert_parse_args): |
| 209 |
"""Test that POSITIONAL_OR_KEYWORD works correctly.""" |
| 210 |
|
| 211 |
class SimpleClass: |
| 212 |
def __init__(self, x: int, y: str): |
| 213 |
self.x = x |
| 214 |
self.y = y |
| 215 |
|
| 216 |
def __eq__(self, other): |
| 217 |
if not isinstance(other, SimpleClass): |
| 218 |
return NotImplemented |
| 219 |
return self.x == other.x and self.y == other.y |
| 220 |
|
| 221 |
@app.default |
| 222 |
def cmd(obj: Annotated[SimpleClass, Parameter(name="*")]): |
| 223 |
return obj |
| 224 |
|
| 225 |
|
| 226 |
assert_parse_args(cmd, "42 hello", obj=SimpleClass(42, "hello")) |
| 227 |
assert_parse_args(cmd, "--x 42 --y hello", obj=SimpleClass(42, "hello")) |
| 228 |
|
| 229 |
|
| 230 |
@dataclass |
| 231 |
class DataclassNested: |
| 232 |
"""Nested dataclass for testing.""" |
| 233 |
|
| 234 |
inner: DataclassFullyKwOnly |
| 235 |
outer: str |
| 236 |
|
| 237 |
|
| 238 |
def test_nested_dataclass_with_kw_only(app, assert_parse_args): |
| 239 |
"""Test nested dataclass where inner is kw_only.""" |
| 240 |
|
| 241 |
@app.default |
| 242 |
def cmd(data: Annotated[DataclassNested, Parameter(name="*")]): |
| 243 |
return data |
| 244 |
|
| 245 |
|
| 246 |
assert_parse_args( |
| 247 |
cmd, |
| 248 |
"--inner.name Alice --inner.age 25 --outer test", |
| 249 |
data=DataclassNested(inner=DataclassFullyKwOnly(name="Alice", age=25), outer="test"), |
| 250 |
) |