| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from dataclasses import dataclass |
| 4 |
from typing import Annotated, NamedTuple, TypedDict |
| 5 |
|
| 6 |
import attrs |
| 7 |
from pydantic import BaseModel |
| 8 |
|
| 9 |
from cyclopts import Parameter |
| 10 |
|
| 11 |
|
| 12 |
def test_future_annotations_basic(app, assert_parse_args): |
| 13 |
@app.default |
| 14 |
def default(value: str): |
| 15 |
pass |
| 16 |
|
| 17 |
assert_parse_args(default, "foo", "foo") |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
@dataclass |
| 23 |
class DataclassMovie: |
| 24 |
title: str |
| 25 |
year: int |
| 26 |
|
| 27 |
|
| 28 |
def test_future_annotations_dataclass(app, assert_parse_args): |
| 29 |
""" |
| 30 |
https://github.com/BrianPugh/cyclopts/issues/352 |
| 31 |
""" |
| 32 |
|
| 33 |
@app.command |
| 34 |
def add(movie: DataclassMovie): |
| 35 |
print(f"Adding movie: {movie}") |
| 36 |
|
| 37 |
assert_parse_args(add, "add BladeRunner 1982", DataclassMovie("BladeRunner", 1982)) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class GenericMovie: |
| 42 |
def __init__(self, title: str, year: int): |
| 43 |
self.title = title |
| 44 |
self.year = year |
| 45 |
|
| 46 |
def __eq__(self, other): |
| 47 |
if not isinstance(other, type(self)): |
| 48 |
return False |
| 49 |
return self.title == other.title and self.year == other.year |
| 50 |
|
| 51 |
|
| 52 |
def test_future_annotations_generic_class(app, assert_parse_args): |
| 53 |
@app.command |
| 54 |
def add(movie: Annotated[GenericMovie, Parameter(accepts_keys=True)]): |
| 55 |
print(f"Adding movie: {movie}") |
| 56 |
|
| 57 |
assert_parse_args(add, "add BladeRunner 1982", GenericMovie("BladeRunner", 1982)) |
| 58 |
|
| 59 |
|
| 60 |
@attrs.define |
| 61 |
class AttrsMovie: |
| 62 |
title: str |
| 63 |
year: int |
| 64 |
|
| 65 |
|
| 66 |
def test_future_annotations_attrs_movie(app, assert_parse_args): |
| 67 |
@app.command |
| 68 |
def add(movie: Annotated[AttrsMovie, Parameter(accepts_keys=True)]): |
| 69 |
print(f"Adding movie: {movie}") |
| 70 |
|
| 71 |
assert_parse_args(add, "add BladeRunner 1982", AttrsMovie("BladeRunner", 1982)) |
| 72 |
|
| 73 |
|
| 74 |
class TypedDictMovie(TypedDict): |
| 75 |
title: str |
| 76 |
year: int |
| 77 |
|
| 78 |
|
| 79 |
def test_future_annotations_typed_dict_movie(app, assert_parse_args): |
| 80 |
@app.command |
| 81 |
def add(movie: Annotated[TypedDictMovie, Parameter(accepts_keys=True)]): |
| 82 |
print(f"Adding movie: {movie}") |
| 83 |
|
| 84 |
assert_parse_args( |
| 85 |
add, "add --movie.title=BladeRunner --movie.year=1982", TypedDictMovie(title="BladeRunner", year=1982) |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
class NamedTupleMovie(NamedTuple): |
| 90 |
title: str |
| 91 |
year: int |
| 92 |
|
| 93 |
|
| 94 |
def test_future_annotations_named_tuple_movie(app, assert_parse_args): |
| 95 |
@app.command |
| 96 |
def add(movie: Annotated[NamedTupleMovie, Parameter(accepts_keys=True)]): |
| 97 |
print(f"Adding movie: {movie}") |
| 98 |
|
| 99 |
assert_parse_args(add, "add BladeRunner 1982", NamedTupleMovie(title="BladeRunner", year=1982)) |
| 100 |
|
| 101 |
|
| 102 |
class PydanticMovie(BaseModel): |
| 103 |
title: str |
| 104 |
year: int |
| 105 |
|
| 106 |
|
| 107 |
def test_future_annotations_pydantic_movie(app, assert_parse_args): |
| 108 |
@app.command |
| 109 |
def add(movie: Annotated[PydanticMovie, Parameter(accepts_keys=True)]): |
| 110 |
print(f"Adding movie: {movie}") |
| 111 |
|
| 112 |
assert_parse_args(add, "add BladeRunner 1982", PydanticMovie(title="BladeRunner", year=1982)) |