File: properties.py

package info (click to toggle)
python-apischema 0.18.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,624 kB
  • sloc: python: 15,266; sh: 7; makefile: 7
file content (39 lines) | stat: -rw-r--r-- 1,244 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
34
35
36
37
38
39
from collections.abc import Mapping
from dataclasses import dataclass, field
from typing import Annotated

from apischema import deserialize, properties, schema
from apischema.json_schema import deserialization_schema


@dataclass
class Config:
    active: bool = True
    server_options: Mapping[str, bool] = field(
        default_factory=dict, metadata=properties(pattern=r"^server_")
    )
    client_options: Mapping[
        Annotated[str, schema(pattern=r"^client_")], bool  # noqa: F722
    ] = field(default_factory=dict, metadata=properties(...))
    options: Mapping[str, bool] = field(default_factory=dict, metadata=properties)


assert deserialize(
    Config,
    {"use_lightsaber": True, "server_auto_restart": False, "client_timeout": False},
) == Config(
    True,
    {"server_auto_restart": False},
    {"client_timeout": False},
    {"use_lightsaber": True},
)
assert deserialization_schema(Config) == {
    "$schema": "http://json-schema.org/draft/2020-12/schema#",
    "type": "object",
    "properties": {"active": {"type": "boolean", "default": True}},
    "additionalProperties": {"type": "boolean"},
    "patternProperties": {
        "^server_": {"type": "boolean"},
        "^client_": {"type": "boolean"},
    },
}