| 1 |
"""Tests for Python 3.12+ TypeAliasType with Parameter metadata. |
| 2 |
|
| 3 |
Regression tests for https://github.com/BrianPugh/cyclopts/issues/669 |
| 4 |
""" |
| 5 |
|
| 6 |
from typing import Annotated, Optional |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from cyclopts import Parameter |
| 11 |
from cyclopts.annotations import resolve, resolve_annotated, resolve_optional |
| 12 |
|
| 13 |
|
| 14 |
type IntWithAlias = Annotated[int, Parameter(alias="-f")] |
| 15 |
type OptionalIntWithAlias = Optional[Annotated[int, Parameter(alias="-f")]] |
| 16 |
type BoolAlias = bool |
| 17 |
type AnnotatedIntWithMetadata = Annotated[int, "metadata"] |
| 18 |
type OptionalInt = Optional[int] |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.parametrize( |
| 22 |
"cmd_str,expected", |
| 23 |
[ |
| 24 |
("--foo 10", 10), |
| 25 |
("-f 10", 10), |
| 26 |
], |
| 27 |
) |
| 28 |
def test_type_alias_with_parameter_alias(app, assert_parse_args, cmd_str, expected): |
| 29 |
"""Test that type statement (TypeAliasType) works with Parameter alias.""" |
| 30 |
|
| 31 |
@app.default |
| 32 |
def main(foo: IntWithAlias): |
| 33 |
pass |
| 34 |
|
| 35 |
assert_parse_args(main, cmd_str, foo=expected) |
| 36 |
|
| 37 |
|
| 38 |
@pytest.mark.parametrize( |
| 39 |
"cmd_str,expected", |
| 40 |
[ |
| 41 |
("--foo 10", 10), |
| 42 |
("-f 10", 10), |
| 43 |
], |
| 44 |
) |
| 45 |
def test_type_alias_optional_with_parameter_alias(app, assert_parse_args, cmd_str, expected): |
| 46 |
"""Test that Optional TypeAliasType works with Parameter alias.""" |
| 47 |
|
| 48 |
@app.default |
| 49 |
def main(foo: OptionalIntWithAlias = None): |
| 50 |
pass |
| 51 |
|
| 52 |
assert_parse_args(main, cmd_str, foo=expected) |
| 53 |
|
| 54 |
|
| 55 |
def test_parameter_get_negatives_with_type_alias(): |
| 56 |
"""Test that Parameter.get_negatives works with TypeAliasType.""" |
| 57 |
p = Parameter(name=("--foo", "--bar")) |
| 58 |
assert ("--no-foo", "--no-bar") == p.get_negatives(BoolAlias) |
| 59 |
|
| 60 |
|
| 61 |
def test_parameter_from_annotation_type_alias(): |
| 62 |
"""Test that Parameter.from_annotation works with TypeAliasType.""" |
| 63 |
result_type, result_param = Parameter.from_annotation(IntWithAlias, Parameter()) |
| 64 |
assert result_type is int |
| 65 |
assert result_param.alias == ("-f",) |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.parametrize( |
| 69 |
"type_alias,resolve_func,expected", |
| 70 |
[ |
| 71 |
(AnnotatedIntWithMetadata, resolve, int), |
| 72 |
(OptionalInt, resolve, int), |
| 73 |
(AnnotatedIntWithMetadata, resolve_annotated, int), |
| 74 |
(OptionalInt, resolve_optional, int), |
| 75 |
], |
| 76 |
ids=[ |
| 77 |
"resolve(Annotated[int])", |
| 78 |
"resolve(Optional[int])", |
| 79 |
"resolve_annotated(Annotated[int])", |
| 80 |
"resolve_optional(Optional[int])", |
| 81 |
], |
| 82 |
) |
| 83 |
def test_resolve_functions_with_type_alias(type_alias, resolve_func, expected): |
| 84 |
"""Test that resolve functions handle TypeAliasType automatically.""" |
| 85 |
res = resolve_func(type_alias) |
| 86 |
assert res is expected |
| 87 |
|
| 88 |
|
| 89 |
def test_type_alias_in_help(capsys): |
| 90 |
"""Test that TypeAliasType appears correctly in help text.""" |
| 91 |
from cyclopts import App |
| 92 |
|
| 93 |
app = App() |
| 94 |
|
| 95 |
@app.default |
| 96 |
def main(foo: IntWithAlias): |
| 97 |
"""Test command. |
| 98 |
|
| 99 |
Parameters |
| 100 |
---------- |
| 101 |
foo : int |
| 102 |
A parameter with alias. |
| 103 |
""" |
| 104 |
pass |
| 105 |
|
| 106 |
try: |
| 107 |
app(["--help"]) |
| 108 |
except SystemExit: |
| 109 |
pass |
| 110 |
|
| 111 |
captured = capsys.readouterr() |
| 112 |
help_text = captured.out |
| 113 |
assert "--foo" in help_text |
| 114 |
assert "-f" in help_text |