File: test_bind_custom_type.py

package info (click to toggle)
python-cyclopts 3.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,288 kB
  • sloc: python: 11,445; makefile: 24
file content (33 lines) | stat: -rw-r--r-- 728 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from typing import Annotated

from cyclopts import Parameter


class OneToken:
    def __init__(self, value: int):
        self.value = value

    def __eq__(self, other):
        return self.value == other.value


def test_custom_type_one_token_implicit_convert(app):
    @app.default
    def default(value: OneToken):
        return value

    res = app("5")
    assert res == OneToken(5)


def test_custom_type_one_token_explicit_convert(app):
    def converter(type_, tokens):
        assert len(tokens) == 1
        return type_(int(tokens[0].value) + 10)

    @app.default
    def default(value: Annotated[OneToken, Parameter(converter=converter)]):
        return value

    res = app("5")
    assert res == OneToken(15)