Skip to content

Commit aaf48cb

Browse files
authored
Use UpdateClass in qblike_3. (#96)
1 parent 9addcad commit aaf48cb

File tree

1 file changed

+49
-29
lines changed

1 file changed

+49
-29
lines changed

tests/test_qblike_3.py

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@
1818
Bool,
1919
Length,
2020
GetArg,
21+
GetInit,
2122
GetMemberType,
2223
GetName,
24+
GetQuals,
2325
GetSpecialAttr,
2426
GetType,
25-
GetInit,
2627
InitField,
2728
IsAssignable,
2829
Iter,
2930
IsEquivalent,
3031
Member,
32+
Members,
3133
NewProtocol,
3234
Slice,
35+
UpdateClass,
3336
)
3437

3538
from . import format_helper
@@ -134,16 +137,39 @@ class DbString:
134137

135138

136139
class Table[name: str]:
140+
def __init_subclass__[T](
141+
cls: type[T],
142+
) -> UpdateClass[
143+
*[
144+
Member[
145+
GetName[m],
146+
_Field[
147+
GetArg[GetType[m], Field, Literal[0]],
148+
T,
149+
GetName[m],
150+
],
151+
GetQuals[m],
152+
GetInit[m],
153+
]
154+
for m in Iter[Members[T]]
155+
if IsAssignable[GetType[m], Field]
156+
],
157+
*[m for m in Iter[Members[T]] if not IsAssignable[GetType[m], Field]],
158+
]:
159+
super().__init_subclass__()
160+
161+
162+
class Field[PyType]:
137163
pass
138164

139165

140-
class Field[Table, Name, PyType]:
166+
class _Field[PyType, Table, Name]:
141167
def __lt__(self, other: Any) -> Filter[Table]: ...
142168

143169

144-
type FieldTable[T] = GetArg[T, Field, Literal[0]]
145-
type FieldName[T] = GetArg[T, Field, Literal[1]]
146-
type FieldPyType[T] = GetArg[T, Field, Literal[2]]
170+
type FieldPyType[T] = GetArg[T, _Field, Literal[0]]
171+
type FieldTable[T] = GetArg[T, _Field, Literal[1]]
172+
type FieldName[T] = GetArg[T, _Field, Literal[2]]
147173

148174

149175
class ColumnArgs(TypedDict, total=False):
@@ -236,7 +262,7 @@ class DbLinkSource[Args: DbLinkSourceArgs](InitField[Args]):
236262
*[
237263
GetName[m]
238264
for m in Iter[Attrs[T]]
239-
if IsAssignable[GetType[m], Field]
265+
if IsAssignable[GetType[m], _Field]
240266
],
241267
],
242268
]
@@ -249,7 +275,7 @@ class DbLinkSource[Args: DbLinkSourceArgs](InitField[Args]):
249275
*[
250276
GetName[m]
251277
for m in Iter[Attrs[T]]
252-
if IsAssignable[GetType[m], Field]
278+
if IsAssignable[GetType[m], _Field]
253279
and any(
254280
IsAssignable[FieldName[GetType[m]], f] for f in Iter[FieldNames]
255281
)
@@ -268,7 +294,7 @@ class DbLinkSource[Args: DbLinkSourceArgs](InitField[Args]):
268294
else [MakeQueryEntryAllFields[New]]
269295
),
270296
]
271-
type AddField[Entries, New: Field] = tuple[
297+
type AddField[Entries, New: _Field] = tuple[
272298
*[ # Existing entries
273299
(
274300
e # Non-matching entry
@@ -286,7 +312,7 @@ class DbLinkSource[Args: DbLinkSourceArgs](InitField[Args]):
286312
if not Bool[EntriesHasTable[Entries, FieldTable[New]]]
287313
),
288314
]
289-
type AddEntries[Entries, News: tuple[Table | Field, ...]] = (
315+
type AddEntries[Entries, News: tuple[Table | _Field, ...]] = (
290316
Entries
291317
if IsAssignable[Length[News], Literal[0]]
292318
else AddEntries[
@@ -351,50 +377,44 @@ def execute[Es: tuple[type[Table], ...]](
351377

352378

353379
class User(Table[Literal["users"]]):
354-
id: Field[User, Literal["id"], int] = column(
380+
id: Field[int] = column(
355381
db_type=DbInteger(), primary_key=True, autoincrement=True
356382
)
357-
name: Field[User, Literal["name"], str] = column(
358-
db_type=DbString(length=150), nullable=False
359-
)
360-
email: Field[User, Literal["email"], str] = column(
383+
name: Field[str] = column(db_type=DbString(length=150), nullable=False)
384+
email: Field[str] = column(
361385
db_type=DbString(length=100), unique=True, nullable=False
362386
)
363-
age: Field[User, Literal["age"], int | None] = column(db_type=DbInteger())
364-
active: Field[User, Literal["active"], bool] = column(
387+
age: Field[int | None] = column(db_type=DbInteger())
388+
active: Field[bool] = column(
365389
db_type=DbBoolean(), default=True, nullable=False
366390
)
367-
posts: Field[User, Literal["posts"], list[Post]] = column(
391+
posts: Field[list[Post]] = column(
368392
db_type=DbLinkSource(source="Post", cardinality=Cardinality.MANY)
369393
)
370394

371395

372396
class Post(Table[Literal["posts"]]):
373-
id: Field[Post, Literal["id"], int] = column(
397+
id: Field[int] = column(
374398
db_type=DbInteger(), primary_key=True, autoincrement=True
375399
)
376-
content: Field[Post, Literal["content"], str] = column(
377-
db_type=DbString(length=1000), nullable=False
378-
)
379-
author: Field[Post, Literal["author"], User] = column(
400+
content: Field[str] = column(db_type=DbString(length=1000), nullable=False)
401+
author: Field[User] = column(
380402
db_type=DbLinkTarget(target=User), nullable=False
381403
)
382-
comments: Field[Post, Literal["comments"], list[Comment]] = column(
404+
comments: Field[list[Comment]] = column(
383405
db_type=DbLinkSource(source="Comment", cardinality=Cardinality.MANY)
384406
)
385407

386408

387409
class Comment(Table[Literal["comments"]]):
388-
id: Field[Comment, Literal["id"], int] = column(
410+
id: Field[int] = column(
389411
db_type=DbInteger(), primary_key=True, autoincrement=True
390412
)
391-
content: Field[Comment, Literal["content"], str] = column(
392-
db_type=DbString(length=1000), nullable=False
393-
)
394-
author: Field[Comment, Literal["author"], User] = column(
413+
content: Field[str] = column(db_type=DbString(length=1000), nullable=False)
414+
author: Field[User] = column(
395415
db_type=DbLinkTarget(target=User), nullable=False
396416
)
397-
post: Field[Comment, Literal["post"], Post] = column(
417+
post: Field[Post] = column(
398418
db_type=DbLinkTarget(target=Post), nullable=False
399419
)
400420

0 commit comments

Comments
 (0)