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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
import collections.abc
import sys
from dataclasses import dataclass, fields
from enum import Enum
from typing import (
Any,
Collection,
Dict,
Generic,
List,
Literal,
Mapping,
NamedTuple,
NewType,
Optional,
Tuple,
TypedDict,
TypeVar,
Union,
)
from unittest.mock import Mock
import pytest
from apischema.types import NoneType
from apischema.typing import Annotated
from apischema.visitor import Unsupported, Visitor
ARG = object()
@pytest.fixture
def visitor() -> Mock:
return Mock()
class NamedTupleExample(NamedTuple):
a: int
b: str = ""
class EnumExample(Enum):
A = "a"
B = "b"
NewTypeExample = NewType("NewTypeExample", int)
def func():
pass
@dataclass
class DataclassExample:
a: int
b: str
class TypedDictExample(TypedDict, total=True):
key1: str
key2: List[int]
class MyInt(int):
pass
pep_585: list = []
if sys.version_info >= (3, 9):
pep_585 = [
(list[int], Visitor.collection, [list, int]),
(tuple[str, ...], Visitor.collection, [tuple, str]),
(
collections.abc.Collection[int],
Visitor.collection,
[collections.abc.Collection, int],
),
(
collections.abc.Mapping[str, int],
Visitor.mapping,
[collections.abc.Mapping, str, int],
),
(dict[str, int], Visitor.mapping, [dict, str, int]),
]
py310: list = []
if sys.version_info >= (3, 10):
py310 = [(int | str, Visitor.union, [(int, str)])]
py311: list = []
if sys.version_info >= (3, 11):
from typing import LiteralString
py311 = [(LiteralString, Visitor.primitive, [str])]
@pytest.mark.parametrize(
"cls, method, args",
[
(List[int], Visitor.collection, [list, int]),
(Tuple[str, ...], Visitor.collection, [tuple, str]),
(Collection[int], Visitor.collection, [collections.abc.Collection, int]),
(Mapping[str, int], Visitor.mapping, [collections.abc.Mapping, str, int]),
(Dict[str, int], Visitor.mapping, [dict, str, int]),
*pep_585,
*py310,
*py311,
(Annotated[int, 42, "42"], Visitor.annotated, [int, (42, "42")]),
(Any, Visitor.any, []),
(
DataclassExample,
Visitor.dataclass,
[
DataclassExample,
{"a": int, "b": str},
(fields(DataclassExample)[0], fields(DataclassExample)[1]),
(),
],
),
(EnumExample, Visitor.enum, [EnumExample]),
(Literal[1, 2], Visitor.literal, [(1, 2)]),
(
NamedTupleExample,
Visitor.named_tuple,
[NamedTupleExample, {"a": int, "b": str}, {"b": ""}],
),
(NewTypeExample, Visitor.new_type, [NewTypeExample, int]),
(int, Visitor.primitive, [int]),
(str, Visitor.primitive, [str]),
(MyInt, Visitor.subprimitive, [MyInt, int]),
(Tuple[str, int], Visitor.tuple, [(str, int)]),
(
TypedDictExample,
Visitor.typed_dict,
(
TypedDictExample,
{"key1": str, "key2": List[int]},
{"key1", "key2"} if sys.version_info >= (3, 9) else (),
),
),
(Optional[int], Visitor.union, [(int, NoneType)]),
(Union[int, str], Visitor.union, [(int, str)]),
],
)
def test_visitor(visitor, cls, method, args):
Visitor.visit(visitor, cls)
getattr(visitor, method.__name__).assert_called_once_with(*args)
T = TypeVar("T")
@dataclass
class GenericExample(Generic[T]):
attr: T
def test_default_implementations(visitor):
assert Visitor.annotated(visitor, int, (42,))
visitor.visit.assert_called_once_with(int)
visitor.reset_mock()
assert Visitor.new_type(visitor, ..., int)
visitor.visit.assert_called_once_with(int)
visitor.reset_mock()
with pytest.raises(Unsupported) as err:
Visitor.unsupported(..., Generic) # type: ignore
assert err.value.type == Generic
with pytest.raises(Unsupported) as err:
Visitor.unsupported(..., Generic[T]) # type: ignore
assert err.value.type == Generic[T]
with pytest.raises(NotImplementedError):
Visitor.named_tuple(..., ..., ..., ...) # type: ignore
|