2
2
Callable as _Callable ,
3
3
Dict as _Dict ,
4
4
Iterable as _Iterable ,
5
- ClassVar as _ClassVar ,
5
+ Union as _Union ,
6
6
)
7
+ from copy import deepcopy as _deepcopy
7
8
from misc .codegen .lib import schema as _schema
8
9
import inspect as _inspect
9
10
from dataclasses import dataclass as _dataclass
@@ -75,7 +76,7 @@ class _Namespace:
75
76
""" simple namespacing mechanism """
76
77
_name : str
77
78
78
- def add (self , pragma : "_PragmaBase" , key : str | None = None ):
79
+ def add (self , pragma : _Union [ "_PragmaBase" , "_Parametrized" ] , key : str | None = None ):
79
80
self .__dict__ [pragma .pragma ] = pragma
80
81
pragma .pragma = key or f"{ self ._name } _{ pragma .pragma } "
81
82
@@ -101,6 +102,10 @@ def negate(self) -> _schema.PropertyModifier:
101
102
@_dataclass
102
103
class _PragmaBase :
103
104
pragma : str
105
+ value : object = None
106
+
107
+ def _apply (self , pragmas : _Dict [str , object ]) -> None :
108
+ pragmas [self .pragma ] = self .value
104
109
105
110
106
111
@_dataclass
@@ -109,7 +114,6 @@ class _ClassPragma(_PragmaBase):
109
114
For schema classes it acts as a python decorator with `@`.
110
115
"""
111
116
inherited : bool = False
112
- value : object = None
113
117
114
118
def __call__ (self , cls : type ) -> type :
115
119
""" use this pragma as a decorator on classes """
@@ -122,23 +126,19 @@ def __call__(self, cls: type) -> type:
122
126
self ._apply (cls ._pragmas )
123
127
return cls
124
128
125
- def _apply (self , pragmas : _Dict [str , object ]) -> None :
126
- pragmas [self .pragma ] = self .value
127
-
128
129
129
130
@_dataclass
130
- class _Pragma (_ClassPragma , _schema .PropertyModifier ):
131
- """ A class or property pragma.
132
- For properties, it functions similarly to a `_PropertyModifier` with `|`, adding the pragma.
133
- For schema classes it acts as a python decorator with `@`.
131
+ class _PropertyPragma (_PragmaBase , _schema .PropertyModifier ):
132
+ """ A property pragma.
133
+ It functions similarly to a `_PropertyModifier` with `|`, adding the pragma.
134
134
"""
135
135
remove : bool = False
136
136
137
137
def modify (self , prop : _schema .Property ):
138
138
self ._apply (prop .pragmas )
139
139
140
140
def negate (self ) -> _schema .PropertyModifier :
141
- return _Pragma (self .pragma , remove = True )
141
+ return _PropertyPragma (self .pragma , remove = not self . remove )
142
142
143
143
def _apply (self , pragmas : _Dict [str , object ]) -> None :
144
144
if self .remove :
@@ -148,31 +148,38 @@ def _apply(self, pragmas: _Dict[str, object]) -> None:
148
148
149
149
150
150
@_dataclass
151
- class _ParametrizedClassPragma (_PragmaBase ):
152
- """ A class parametrized pragma.
153
- Needs to be applied to a parameter to give a class pragma.
151
+ class _Pragma (_ClassPragma , _PropertyPragma ):
152
+ """ A class or property pragma.
153
+ For properties, it functions similarly to a `_PropertyModifier` with `|`, adding the pragma.
154
+ For schema classes it acts as a python decorator with `@`.
154
155
"""
155
- _pragma_class : _ClassVar [type ] = _ClassPragma
156
156
157
- inherited : bool = False
158
- factory : _Callable [..., object ] = None
159
157
160
- def __post_init__ (self ):
161
- self .__signature__ = _inspect .signature (self .factory ).replace (return_annotation = self ._pragma_class )
158
+ class _Parametrized [P , ** Q , T ]:
159
+ """ A parametrized pragma.
160
+ Needs to be applied to a parameter to give a pragma.
161
+ """
162
+
163
+ def __init__ (self , pragma_instance : P , factory : _Callable [Q , T ]):
164
+ self .pragma_instance = pragma_instance
165
+ self .factory = factory
166
+ self .__signature__ = _inspect .signature (self .factory ).replace (return_annotation = type (self .pragma_instance ))
162
167
163
- def __call__ (self , * args , ** kwargs ) -> _pragma_class :
164
- return self ._pragma_class (self .pragma , self .inherited , value = self .factory (* args , ** kwargs ))
168
+ @property
169
+ def pragma (self ):
170
+ return self .pragma_instance .pragma
165
171
172
+ @pragma .setter
173
+ def pragma (self , value ):
174
+ self .pragma_instance .pragma = value
166
175
167
- @_dataclass
168
- class _ParametrizedPragma (_ParametrizedClassPragma ):
169
- """ A class or property parametrized pragma.
170
- Needs to be applied to a parameter to give a pragma.
171
- """
172
- _pragma_class : _ClassVar [type ] = _Pragma
176
+ def __invert__ (self ) -> "_Parametrized[P, Q, T]" :
177
+ return _Parametrized (~ self .pragma_instance , factory = self .factory )
173
178
174
- def __invert__ (self ) -> _Pragma :
175
- return _Pragma (self .pragma , remove = True )
179
+ def __call__ (self , * args : Q .args , ** kwargs : Q .kwargs ) -> T :
180
+ ret = _deepcopy (self .pragma_instance )
181
+ ret .value = self .factory (* args , ** kwargs )
182
+ return ret
176
183
177
184
178
185
class _Optionalizer (_schema .PropertyModifier ):
@@ -232,30 +239,31 @@ def __getitem__(self, item):
232
239
233
240
use_for_null = _ClassPragma ("null" )
234
241
235
- qltest .add (_Pragma ("skip" ))
242
+ qltest .add (_ClassPragma ("skip" ))
236
243
qltest .add (_ClassPragma ("collapse_hierarchy" ))
237
244
qltest .add (_ClassPragma ("uncollapse_hierarchy" ))
238
- qltest .add (_ParametrizedClassPragma ( "test_with" , inherited = True , factory = _schema .get_type_name ))
245
+ qltest .add (_Parametrized ( _ClassPragma ( "test_with" , inherited = True ) , factory = _schema .get_type_name ))
239
246
240
- ql .add (_ParametrizedClassPragma ( "default_doc_name" , factory = lambda doc : doc ))
247
+ ql .add (_Parametrized ( _ClassPragma ( "default_doc_name" ) , factory = lambda doc : doc ))
241
248
ql .add (_ClassPragma ("hideable" , inherited = True ))
242
249
ql .add (_Pragma ("internal" ))
243
- ql .add (_ParametrizedPragma ("name" , factory = lambda name : name ))
250
+ ql .add (_Parametrized (_Pragma ("name" ), factory = lambda name : name ))
251
+ ql .add (_Parametrized (_PropertyPragma ("db_table_name" ), factory = lambda name : name ))
244
252
245
253
cpp .add (_Pragma ("skip" ))
246
254
247
- rust .add (_Pragma ("detach" ))
255
+ rust .add (_PropertyPragma ("detach" ))
248
256
rust .add (_Pragma ("skip_doc_test" ))
249
257
250
- rust .add (_ParametrizedClassPragma ( "doc_test_signature" , factory = lambda signature : signature ))
258
+ rust .add (_Parametrized ( _ClassPragma ( "doc_test_signature" ) , factory = lambda signature : signature ))
251
259
252
- group = _ParametrizedClassPragma ( "group" , inherited = True , factory = lambda group : group )
260
+ group = _Parametrized ( _ClassPragma ( "group" , inherited = True ) , factory = lambda group : group )
253
261
254
262
255
- synth .add (_ParametrizedClassPragma ( "from_class" , factory = lambda ref : _schema .SynthInfo (
263
+ synth .add (_Parametrized ( _ClassPragma ( "from_class" ) , factory = lambda ref : _schema .SynthInfo (
256
264
from_class = _schema .get_type_name (ref ))), key = "synth" )
257
- synth .add (_ParametrizedClassPragma ( "on_arguments" , factory = lambda ** kwargs :
258
- _schema .SynthInfo (on_arguments = {k : _schema .get_type_name (t ) for k , t in kwargs .items ()})), key = "synth" )
265
+ synth .add (_Parametrized ( _ClassPragma ( "on_arguments" ) , factory = lambda ** kwargs :
266
+ _schema .SynthInfo (on_arguments = {k : _schema .get_type_name (t ) for k , t in kwargs .items ()})), key = "synth" )
259
267
260
268
261
269
@_dataclass (frozen = True )
0 commit comments