Debian Codegraph @

tests/py312/test_type_alias_type.py

raw symbols SCIP
1 from enum import Enum
2 from typing import Annotated, Literal, TypeAlias
3
4 import pytest
5
6 from cyclopts import Parameter
7 from cyclopts.argument import get_choices_from_hint
8
9 FontSize: TypeAlias = Literal[10, 12, 16]
10 type BoxSize = Literal[10, 12, 16]
11
12
13 def test_py312_type_alias_type(app, assert_parse_args):
14 """Support for python3.12 :obj:`TypeAliasType`.
15
16 https://github.com/BrianPugh/cyclopts/issues/190
17 """
18
19 @app.default
20 def main(
21 font_size: FontSize,
22 box_size: BoxSize,
23 box_size_2: Annotated[BoxSize, "foo"],
24 ):
25 pass
26
27 assert_parse_args(main, "10 12 16", 10, 12, 16)
28
29
30 class CompSciProblem(Enum):
31 fizz = "bleep bloop blop"
32 buzz = "blop bleep bloop"
33
34
35 type AnnotatedEnum = Annotated[CompSciProblem, Parameter(name="foo")]
36 type AnnotatedOptionalEnum = Annotated[CompSciProblem | None, Parameter(name="foo")]
37
38 type FontSingleFormat = Literal["otf", "woff2", "ttf", "bdf", "pcf"]
39 type FontCollectionFormat = Literal["otc", "ttc"]
40 FontPixelFormat: TypeAlias = Literal["bmp"]
41
42
43 @pytest.mark.parametrize(
44 "type_, expected",
45 [
46 (AnnotatedEnum, ["fizz", "buzz"]),
47 (AnnotatedOptionalEnum, ["fizz", "buzz"]),
48 (FontPixelFormat, ["bmp"]),
49 (FontSingleFormat, ["otf", "woff2", "ttf", "bdf", "pcf"]),
50 (FontSingleFormat | FontPixelFormat, ["otf", "woff2", "ttf", "bdf", "pcf", "bmp"]),
51 (FontSingleFormat | FontCollectionFormat, ["otf", "woff2", "ttf", "bdf", "pcf", "otc", "ttc"]),
52 (FontSingleFormat | FontCollectionFormat | None, ["otf", "woff2", "ttf", "bdf", "pcf", "otc", "ttc"]),
53 (list[FontSingleFormat | FontCollectionFormat] | None, ["otf", "woff2", "ttf", "bdf", "pcf", "otc", "ttc"]),
54 ],
55 )
56 def test_py312_type_alias_type_help_get_choices(type_, expected):
57 assert expected == get_choices_from_hint(type_, lambda x: x)
58
59
60 type Numbers = tuple[int, str]
61
62
63 def test_py312_type_alias_type_tuple_token_count(app, assert_parse_args):
64 """Ensure that TypeAliasType can handle multi-token types.
65
66 https://github.com/BrianPugh/cyclopts/issues/413
67 """
68
69 @app.default
70 def main(foo: Numbers):
71 pass
72
73 assert_parse_args(main, "1 one", (1, "one"))