This repository was archived by the owner on Feb 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_annotation.py
92 lines (57 loc) · 2.2 KB
/
test_annotation.py
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
#!/usr/bin/env python3
from typing import Generic
from typing import List
from typing import NewType
from typing import Tuple
from typing import Type
from typing import TypeVar
from typedjson.annotation import args_of
from typedjson.annotation import origin_of
from typedjson.annotation import parameters_of
from typedjson.annotation import supertype_of
from dataclasses import dataclass
A = NewType("A", str)
@dataclass(frozen=True)
class NameJson:
first: str
last: str
T1 = TypeVar("T1")
T2 = TypeVar("T2")
@dataclass(frozen=True)
class GenericJson(Generic[T1, T2]):
t1: T1
t2: T2
def test_can_obtain_args_of_generics() -> None:
expectation = (int, str)
assert args_of(GenericJson[int, str]) == expectation
def test_can_obtain_parameters_of_generics() -> None:
expectation = (T1, T2)
assert parameters_of(GenericJson[int, str]) == expectation
def test_can_obtain_origin_of_generics() -> None:
expectation = GenericJson
assert origin_of(GenericJson[int, str]) == expectation
def test_can_obtain_origin_of_tuple() -> None:
assert origin_of(Tuple[int, ...]) is tuple
def test_can_obtain_origin_of_list() -> None:
assert origin_of(List[int]) is list
def test_can_obtain_supertype_of_newtype() -> None:
assert supertype_of(A) == str
def test_cannot_obtain_args_of_raw_generics() -> None:
expectation: Tuple[Type, ...] = tuple()
assert args_of(GenericJson) == expectation
def test_cannot_obtain_parameters_of_raw_generics() -> None:
expectation: Tuple[Type, ...] = tuple()
assert parameters_of(GenericJson) == expectation
def test_cannot_obtain_origin_of_raw_generics() -> None:
assert origin_of(GenericJson) is None
def test_cannot_obtain_args_of_non_generics() -> None:
expectation: Tuple[Type, ...] = tuple()
assert args_of(NameJson) == expectation
def test_cannot_obtain_parameters_of_non_generics() -> None:
expectation: Tuple[Type, ...] = tuple()
assert parameters_of(NameJson) == expectation
def test_cannot_obtain_origin_of_non_generics() -> None:
assert origin_of(NameJson) is None
def test_cannot_obtain_supertype_of_non_newtype() -> None:
assert supertype_of(NameJson) is None
assert supertype_of(str) is None