File: test_typing.py

package info (click to toggle)
python-apischema 0.18.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,636 kB
  • sloc: python: 15,281; makefile: 3; sh: 2
file content (51 lines) | stat: -rw-r--r-- 1,175 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
40
41
42
43
44
45
46
47
48
49
50
51
from typing import Generic, TypeVar

import pytest

from apischema.typing import Annotated, generic_mro, resolve_type_hints

T = TypeVar("T")
U = TypeVar("U")


class A(Generic[T, U]):
    t: T
    u: U


class B(A[int, T]):
    v: T


class C(B[str]):
    pass


class D(C):
    d: Annotated[int, ""]


test_cases = [
    (A, [A], {"t": T, "u": U}),
    (A[int, str], [A[int, str]], {"t": int, "u": str}),
    (A[int, T], [A[int, T]], {"t": int, "u": T}),  # type: ignore
    (B, [B, A[int, T]], {"t": int, "u": T, "v": T}),  # type: ignore
    (B[U], [B[U], A[int, U]], {"t": int, "u": U, "v": U}),  # type: ignore
    (B[str], [B[str], A[int, str]], {"t": int, "u": str, "v": str}),
    (C, [C, B[str], A[int, str]], {"t": int, "u": str, "v": str}),
    (
        D,
        [D, C, B[str], A[int, str]],
        {"t": int, "u": str, "v": str, "d": Annotated[int, ""]},
    ),
]


@pytest.mark.parametrize("tp, result, _", test_cases)
def test_generic_mro(tp, result, _):
    assert generic_mro(tp) == (*result, Generic, object)


@pytest.mark.parametrize("tp, _, result", test_cases)
def test_resolve_type_hints(tp, _, result):
    assert resolve_type_hints(tp) == result