-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_qblike_2.py
More file actions
251 lines (186 loc) · 5.63 KB
/
test_qblike_2.py
File metadata and controls
251 lines (186 loc) · 5.63 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import textwrap
from typing import assert_type, Literal, Unpack
from typemap.type_eval import eval_call, eval_typing
import typemap_extensions as typing
from typemap.type_eval import format_helper
# Begin PEP section: Prisma-style ORMs
"""First, to support the annotations we saw above, we have a collection
of dummy classes with generic types.
"""
class Pointer[T]:
pass
class Property[T](Pointer[T]):
pass
class Link[T](Pointer[T]):
pass
class SingleLink[T](Link[T]):
pass
class MultiLink[T](Link[T]):
pass
"""
The ``select`` method is where we start seeing new things.
The ``**kwargs: Unpack[K]`` is part of this proposal, and allows
*inferring* a TypedDict from keyword args.
``Attrs[K]`` extracts ``Member`` types corresponding to every
type-annotated attribute of ``K``, while calling ``NewProtocol`` with
``Member`` arguments constructs a new structural type.
``c.name`` fetches the name of the ``Member`` bound to the variable ``c``
as a literal type--all of these mechanisms lean very heavily on literal types.
``GetMemberType`` gets the type of an attribute from a class.
"""
def select[ModelT, K: typing.BaseTypedDict](
typ: type[ModelT],
/,
**kwargs: Unpack[K],
) -> list[
typing.NewProtocol[
*[
typing.Member[
c.name,
ConvertField[typing.GetMemberType[ModelT, c.name]],
]
for c in typing.Iter[typing.Attrs[K]]
]
]
]:
raise NotImplementedError
"""``ConvertField`` is our first type helper, and it is a conditional type
alias, which decides between two types based on a (limited)
subtype-ish check.
In ``ConvertField``, we wish to drop the ``Property`` or ``Link``
annotation and produce the underlying type, as well as, for links,
producing a new target type containing only properties and wrapping
``MultiLink`` in a list.
"""
type ConvertField[T] = (
AdjustLink[PropsOnly[PointerArg[T]], T]
if typing.IsAssignable[T, Link]
else PointerArg[T]
)
"""``PointerArg`` gets the type argument to ``Pointer`` or a subclass.
``GetArg[T, Base, I]`` is one of the core primitives; it fetches the
index ``I`` type argument to ``Base`` from a type ``T``, if ``T``
inherits from ``Base``.
(The subtleties of this will be discussed later; in this case, it just
grabs the argument to a ``Pointer``).
"""
type PointerArg[T] = typing.GetArg[T, Pointer, Literal[0]]
"""
``AdjustLink`` sticks a ``list`` around ``MultiLink``, using features
we've discussed already.
"""
type AdjustLink[Tgt, LinkTy] = (
list[Tgt] if typing.IsAssignable[LinkTy, MultiLink] else Tgt
)
"""And the final helper, ``PropsOnly[T]``, generates a new type that
contains all the ``Property`` attributes of ``T``.
"""
type PropsOnly[T] = typing.NewProtocol[
*[
typing.Member[p.name, PointerArg[p.type]]
for p in typing.Iter[typing.Attrs[T]]
if typing.IsAssignable[p.type, Property]
]
]
"""
The full test is `in our test suite <#qb-test_>`_.
"""
# End PEP section
# Basic filtering
class Comment:
id: Property[int]
name: Property[str]
poster: Link[User]
class Post:
id: Property[int]
title: Property[str]
content: Property[str]
comments: MultiLink[Comment]
author: Link[User]
class User:
id: Property[int]
name: Property[str]
email: Property[str]
posts: MultiLink[Post]
def _check_select_user() -> None:
r = select(User, id=True, name=True)
assert_type(
r,
list[
typing.NewProtocol[
typing.Member[Literal["id"], int],
typing.Member[Literal["name"], str],
]
],
)
def _check_select_post_with_links() -> None:
r = select(Post, title=True, comments=True, author=True)
assert_type(
r,
list[
typing.NewProtocol[
typing.Member[Literal["title"], str],
typing.Member[Literal["comments"], list[PropsOnly[Comment]]],
typing.Member[Literal["author"], PropsOnly[User]],
]
],
)
def test_qblike2_1():
ret = eval_call(
select,
User,
id=True,
name=True,
)
assert ret.__origin__ is list
ret = ret.__args__[0]
fmt = format_helper.format_class(ret)
assert fmt == textwrap.dedent("""\
class select[...]:
id: int
name: str
""")
def test_qblike2_2():
ret = eval_call(
select,
User,
name=True,
email=True,
posts=True,
)
assert ret.__origin__ is list
ret = ret.__args__[0]
fmt = format_helper.format_class(ret)
assert fmt == textwrap.dedent("""\
class select[...]:
name: str
email: str
posts: list[tests.test_qblike_2.PropsOnly[tests.test_qblike_2.Post]]
""")
res = eval_typing(typing.GetMemberType[ret, Literal["posts"]])
tgt = res.__args__[0]
# XXX: this should probably be pre-evaluated already?
fmt = format_helper.format_class(tgt)
assert fmt == textwrap.dedent("""\
class PropsOnly[tests.test_qblike_2.Post]:
id: int
title: str
content: str
""")
def test_qblike2_3():
ret = eval_call(
select,
Post,
title=True,
comments=True,
author=True,
)
assert ret.__origin__ is list
ret = ret.__args__[0]
fmt = format_helper.format_class(ret)
assert fmt == textwrap.dedent("""\
class select[...]:
title: str
comments: list[tests.test_qblike_2.PropsOnly[tests.test_qblike_2.Comment]]
author: tests.test_qblike_2.PropsOnly[tests.test_qblike_2.User]
""")