Skip to content

Pydantic Types#

pyticktick.models.pydantic #

Generic custom pydantic types, not specific to any API version.

Classes:

Name Description
Color

Represents a color in pydantic-based TickTick models.

HttpUrl

Represents an HTTP URL in pydantic-based TickTick models.

Color #

Bases: Color

Represents a color in pydantic-based TickTick models.

Methods:

Name Description
__get_pydantic_core_schema__

Change the serialization logic of the color field to always be a string.

__str__

Return the color as a hex string.

__get_pydantic_core_schema__ classmethod #

__get_pydantic_core_schema__(
    source: type[Any], handler: Callable[[Any], CoreSchema]
) -> core_schema.CoreSchema

Change the serialization logic of the color field to always be a string.

This method mirrors the logic of the standard __get_pydantic_core_schema__ method in pydantic_extra_types.color.Color, but changes the when_used parameter to be "always" instead of the default of "json-unless-none".

Source code in src/pyticktick/models/pydantic.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@classmethod
def __get_pydantic_core_schema__(  # noqa: DOC101, DOC103, DOC203
    cls,
    source: type[Any],
    handler: Callable[[Any], CoreSchema],
) -> core_schema.CoreSchema:
    """Change the serialization logic of the color field to always be a string.

    This method mirrors the logic of the standard [`__get_pydantic_core_schema__` method](https://github.com/pydantic/pydantic-extra-types/blob/1da2a1caeb7502e40037e2e3e2961726c2c5c002/pydantic_extra_types/color.py#L213-L219)
    in [`pydantic_extra_types.color.Color`](https://docs.pydantic.dev/latest/api/pydantic_extra_types_color/),
    but changes the `when_used` parameter to be `"always"` instead of the default of
    `"json-unless-none"`.
    """  # noqa: DOC201
    return core_schema.with_info_plain_validator_function(
        cls._validate,
        serialization=core_schema.to_string_ser_schema(when_used="always"),
    )

__str__ #

__str__() -> str

Return the color as a hex string.

TickTick uses the long hex format for colors, so this method allows for easy serialization of the color field into the correct format. Equivalent to calling as_hex(format="long"). It will be lowercase and always have 7 characters (and #): #rrggbb.

Example

color = Color((0, 255, 255))
assert color.as_named() == "cyan"
assert str(color) == "#00ffff"

Returns:

Name Type Description
str str

The color as a hex string.

Source code in src/pyticktick/models/pydantic.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def __str__(self) -> str:
    """Return the color as a hex string.

    TickTick uses the long hex format for colors, so this method allows for easy
    serialization of the color field into the correct format. Equivalent to calling
    [`as_hex(format="long")`](https://docs.pydantic.dev/latest/api/pydantic_extra_types_color/#pydantic_extra_types.color.Color.as_hex).
    It will be lowercase and always have 7 characters (and `#`): `#rrggbb`.

    !!! Example
        ```python
        color = Color((0, 255, 255))
        assert color.as_named() == "cyan"
        assert str(color) == "#00ffff"
        ```

    Returns:
        str: The color as a hex string.
    """
    return self.as_hex(format="long")

HttpUrl #

Bases: HttpUrl

Represents an HTTP URL in pydantic-based TickTick models.

Methods:

Name Description
join

Join the current URL with additional path segments.

join #

join(*args: str) -> HttpUrl

Join the current URL with additional path segments.

This method is a convenience method to join additional path segments to the current URL. It takes its inspiration from the urllib.parse.urljoin and pathlib.PurePath.joinpath methods in Python.

Example

from pyticktick.models.v2 import HttpUrl

url = HttpUrl("https://example.com")
assert url.join("foo") == HttpUrl("https://example.com/foo")
assert url.join("foo", "bar") == HttpUrl("https://example.com/foo/bar")

Parameters:

Name Type Description Default
*args str

The path segments to join with the current URL.

()

Returns:

Name Type Description
HttpUrl HttpUrl

A new URL with the path segments joined to the current URL.

Source code in src/pyticktick/models/pydantic.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def join(self, *args: str) -> HttpUrl:
    """Join the current URL with additional path segments.

    This method is a convenience method to join additional path segments to the
    current URL. It takes its inspiration from the [`urllib.parse.urljoin`](https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urljoin)
    and [`pathlib.PurePath.joinpath`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.joinpath)
    methods in Python.

    !!! Example
        ```python
        from pyticktick.models.v2 import HttpUrl

        url = HttpUrl("https://example.com")
        assert url.join("foo") == HttpUrl("https://example.com/foo")
        assert url.join("foo", "bar") == HttpUrl("https://example.com/foo/bar")
        ```

    Args:
        *args (str): The path segments to join with the current URL.

    Returns:
        HttpUrl: A new URL with the path segments joined to the current URL.
    """
    if len(args) == 0:
        return self

    url = str(self).rstrip("/")
    for arg in args:
        url = f"{url.rstrip('/')}/{arg.lstrip('/')}"
    return self.__class__(url)