-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_schemalike.py
More file actions
80 lines (62 loc) · 1.77 KB
/
test_schemalike.py
File metadata and controls
80 lines (62 loc) · 1.77 KB
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
import textwrap
from typing import Callable, Literal
from typemap.type_eval import eval_typing
from typemap_extensions import (
NewProtocol,
Iter,
Attrs,
Member,
NamedParam,
Param,
Params,
Concat,
)
from . import format_helper
class Schema:
pass
class Type:
pass
class Expression:
pass
# hmmmm... recursion with this sort of thing will be funny...
# how will we handle the decorators or __init_subclass__ or what have you
class Property:
name: str
required: bool
multi: bool
typ: Type
expr: Expression | None
type Schemaify[T] = NewProtocol[
*[p for p in Iter[Attrs[T]]],
*[
Member[
Concat[Literal["get_"], p.name],
Callable[
Params[
Param[Literal["self"], Schemaify[T]],
NamedParam[Literal["schema"], Schema, Literal["keyword"]],
],
p.type,
],
Literal["ClassVar"],
]
for p in Iter[Attrs[T]]
],
]
def test_schema_like_1():
tgt = eval_typing(Schemaify[Property])
fmt = format_helper.format_class(tgt)
getter_params = "self: Self, *, schema: tests.test_schemalike.Schema"
assert fmt == textwrap.dedent(f"""\
class Schemaify[tests.test_schemalike.Property]:
name: str
required: bool
multi: bool
typ: tests.test_schemalike.Type
expr: tests.test_schemalike.Expression | None
def get_name({getter_params}) -> str: ...
def get_required({getter_params}) -> bool: ...
def get_multi({getter_params}) -> bool: ...
def get_typ({getter_params}) -> tests.test_schemalike.Type: ...
def get_expr({getter_params}) -> tests.test_schemalike.Expression | None: ...
""")