2929import itertools
3030
3131from ducktools .lazyimporter import LazyImporter , ModuleImport
32-
3332from ducktools .classbuilder import (
3433 SlotMakerMeta ,
3534 builder ,
3635 make_unified_gatherer ,
3736)
38-
3937from ducktools .classbuilder .prefab import (
4038 PREFAB_FIELDS ,
4139 Attribute ,
4745)
4846
4947
48+ TYPE_CHECKING = False
49+ if TYPE_CHECKING :
50+ from typing import dataclass_transform
51+ else :
52+ def dataclass_transform (
53+ * ,
54+ eq_default = True ,
55+ order_default = False ,
56+ kw_only_default = False ,
57+ frozen_default = False ,
58+ field_specifiers = (),
59+ ** kwargs
60+ ):
61+ def decorator (cls_or_fn ):
62+ cls_or_fn .__dataclass_transform__ = {
63+ "eq_default" : eq_default ,
64+ "order_default" : order_default ,
65+ "kw_only_default" : kw_only_default ,
66+ "frozen_default" : frozen_default ,
67+ "field_specifiers" : field_specifiers ,
68+ "kwargs" : kwargs ,
69+ }
70+ return cls_or_fn
71+ return decorator
72+
73+
5074# Unlike the other modules this has its own lazy importer
5175# As it might be spun off as a separate package
5276_laz = LazyImporter (
5377 [
54- ModuleImport ("sqlite3" , asname = "sql" )
78+ ModuleImport ("sqlite3" , asname = "sql" ),
5579 ]
5680)
5781
@@ -113,7 +137,7 @@ def get_sql_fields(cls: "SQLMeta", local=False) -> dict[str, SQLAttribute]:
113137 k : SQLAttribute .from_field (v ) if type (v ) in parents else v
114138 for k , v in attribs .items ()
115139 }
116- return attributes
140+ return attributes # type: ignore # Bug in classbuilder stub file
117141
118142
119143unified_gatherer = make_unified_gatherer (SQLAttribute )
@@ -148,6 +172,7 @@ class SQLMeta(SlotMakerMeta):
148172default_methods = frozenset ({init_maker , repr_maker , eq_maker })
149173
150174
175+ @dataclass_transform (kw_only_default = True , field_specifiers = (SQLAttribute ,))
151176class SQLClass (metaclass = SQLMeta ):
152177 _meta_gatherer = unified_gatherer
153178 __slots__ = {}
@@ -167,7 +192,7 @@ def __init_subclass__(
167192 methods = methods ,
168193 flags = {"slotted" : slots , "kw_only" : True },
169194 field_getter = get_sql_fields ,
170- )
195+ ) # type: ignore # Another complex typing issue with classbuilder
171196
172197 fields = get_sql_fields (cls )
173198 valid_fields = {}
@@ -201,11 +226,11 @@ def __init_subclass__(
201226 for name , field in fields .items ():
202227 if field .primary_key :
203228 if primary_key is not None :
204- raise AttributeError ("sqlclass *must* have **only** one primary key" )
229+ raise AttributeError ("SQLClass *must* have **only** one primary key" )
205230 primary_key = name
206231
207232 if primary_key is None :
208- raise AttributeError ("sqlclass *must* have one primary key" )
233+ raise AttributeError ("SQLClass *must* have one primary key" )
209234
210235 cls .PK_NAME = primary_key
211236 cls .TABLE_NAME = caps_to_snake (cls .__name__ )
@@ -398,7 +423,7 @@ def update_row(self, con, columns: list[str]):
398423 search_condition = f"{ self .PK_NAME } = :{ self .PK_NAME } "
399424
400425 with con :
401- result = con .execute (
426+ con .execute (
402427 f"UPDATE { self .TABLE_NAME } SET { set_columns } WHERE { search_condition } " ,
403428 processed_values ,
404429 )
0 commit comments