File: test_wrapper.py

package info (click to toggle)
python-apischema 0.18.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,608 kB
  • sloc: python: 15,266; sh: 7; makefile: 7
file content (95 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
90
91
92
93
94
95
# flake8: noqa
# type: ignore
import inspect
import json
import sys
import timeit
import typing
from typing import *
from unittest.mock import MagicMock

import pytest

from apischema import settings
from apischema.typing import Annotated, get_args, get_origin, is_type

typing.get_origin, typing.get_args = get_origin, get_args
typing.Annotated = Annotated
if "include_extras" not in inspect.signature(typing.get_type_hints).parameters:
    gth = typing.get_type_hints

    def get_type_hints(*args, include_extras=False, **kwargs):
        return gth(*args, **kwargs)

    typing.get_type_hints = get_type_hints
inspect.isclass = is_type
if sys.version_info < (3, 9):

    class CollectionABC:
        def __getattribute__(self, name):
            return globals()[name] if name in globals() else MagicMock()

    sys.modules["collections.abc"] = CollectionABC()
    del CollectionABC


class Wrapper:
    def __init__(self, cls):
        self.cls = cls
        self.implem = cls.__origin__

    def __getitem__(self, item):
        return self.cls[item]

    def __call__(self, *args, **kwargs):
        return self.implem(*args, **kwargs)

    def __instancecheck__(self, instance):
        return isinstance(instance, self.implem)

    def __subclasscheck__(self, subclass):
        return issubclass(subclass, self.implem)


for cls in (Dict, List, Set, FrozenSet, Tuple, Type):  # type: ignore # noqa
    wrapper = Wrapper(cls)
    globals()[wrapper.implem.__name__] = wrapper

Set = AbstractSet

del Wrapper

__timeit = timeit.timeit
timeit.timeit = lambda stmt, number=None, **kwargs: __timeit(stmt, number=1, **kwargs)

sys.modules["orjson"] = json

settings_classes = (
    settings,
    settings.errors,
    settings.base_schema,
    settings.deserialization,
    settings.serialization,
)
settings_dicts = {cls: dict(cls.__dict__) for cls in settings_classes}


## test body


def set_settings(dicts: Mapping[type, Mapping[str, Any]]):
    for cls, dict_ in dicts.items():
        for key, value in dict_.items():
            if not key.startswith("_"):
                setattr(cls, key, value)


test_dicts = {cls: dict(cls.__dict__) for cls in settings_classes}
set_settings(settings_dicts)


@pytest.fixture(autouse=True)
def test_settings(monkeypatch):
    set_settings(test_dicts)
    yield
    set_settings(settings_dicts)