@@ -39,8 +39,8 @@ def select(*args):
39
39
q = sa .select ([getattr (owner , x ) for x in args ])
40
40
if instance is not None :
41
41
q = q .where (instance .lookup ())
42
- return q .execution_options (model = weakref .ref (owner ),
43
- return_model = False )
42
+ return q .execution_options (model = weakref .ref (owner ), return_model = False )
43
+
44
44
return select
45
45
46
46
@@ -79,7 +79,8 @@ class UpdateRequest:
79
79
specific model instance and its database row.
80
80
81
81
"""
82
- def __init__ (self , instance : 'CRUDModel' ):
82
+
83
+ def __init__ (self , instance : "CRUDModel" ):
83
84
self ._instance = instance
84
85
self ._values = {}
85
86
self ._props = {}
@@ -116,8 +117,10 @@ async def apply(self, bind=None, timeout=DEFAULT):
116
117
"""
117
118
if self ._locator is None :
118
119
raise TypeError (
119
- 'Model {} has no table, primary key or custom lookup()' .format (
120
- self ._instance .__class__ .__name__ ))
120
+ "Model {} has no table, primary key or custom lookup()" .format (
121
+ self ._instance .__class__ .__name__
122
+ )
123
+ )
121
124
cls = type (self ._instance )
122
125
values = self ._values .copy ()
123
126
@@ -137,36 +140,38 @@ async def apply(self, bind=None, timeout=DEFAULT):
137
140
for prop_name , updates in json_updates .items ():
138
141
prop = getattr (cls , prop_name )
139
142
from .dialects .asyncpg import JSONB
143
+
140
144
if isinstance (prop .type , JSONB ):
141
145
if self ._literal :
142
146
values [prop_name ] = prop .concat (updates )
143
147
else :
144
148
values [prop_name ] = prop .concat (
145
- sa .func .jsonb_build_object (
146
- * itertools . chain ( * updates . items ())) )
149
+ sa .func .jsonb_build_object (* itertools . chain ( * updates . items ()))
150
+ )
147
151
else :
148
- raise TypeError ('{} is not supported to update json '
149
- 'properties in Gino. Please consider using '
150
- 'JSONB.' .format (prop .type ))
152
+ raise TypeError (
153
+ "{} is not supported to update json "
154
+ "properties in Gino. Please consider using "
155
+ "JSONB." .format (prop .type )
156
+ )
151
157
152
158
opts = dict (return_model = False )
153
159
if timeout is not DEFAULT :
154
- opts [' timeout' ] = timeout
155
- clause = type ( self . _instance ). update . where (
156
- self ._locator ,
157
- ). values (
158
- ** self ._instance ._get_sa_values (values ),
159
- ) .returning (
160
- * [ getattr ( cls , key ) for key in values ],
161
- ). execution_options ( ** opts )
160
+ opts [" timeout" ] = timeout
161
+ clause = (
162
+ type ( self ._instance )
163
+ . update . where ( self . _locator ,)
164
+ . values ( ** self ._instance ._get_sa_values (values ),)
165
+ .returning (* [ getattr ( cls , key ) for key in values ],)
166
+ . execution_options ( ** opts )
167
+ )
162
168
if bind is None :
163
169
bind = cls .__metadata__ .bind
164
170
row = await bind .first (clause )
165
171
if not row :
166
172
raise NoSuchRowError ()
167
173
for k , v in row .items ():
168
- self ._instance .__values__ [
169
- self ._instance ._column_name_map .invert_get (k )] = v
174
+ self ._instance .__values__ [self ._instance ._column_name_map .invert_get (k )] = v
170
175
for prop in self ._props :
171
176
prop .reload (self ._instance )
172
177
return self
@@ -208,11 +213,11 @@ def update(self, **values):
208
213
for key , value in values .items ():
209
214
prop = cls .__dict__ .get (key )
210
215
if isinstance (prop , json_support .JSONProperty ):
211
- value_from = ' __profile__'
216
+ value_from = " __profile__"
212
217
method = self ._set_prop
213
218
k = prop
214
219
else :
215
- value_from = ' __values__'
220
+ value_from = " __values__"
216
221
method = self ._set
217
222
k = key
218
223
if not isinstance (value , ClauseElement ):
@@ -227,16 +232,19 @@ class Alias:
227
232
Experimental proxy for table alias on model.
228
233
229
234
"""
235
+
230
236
def __init__ (self , model , * args , ** kwargs ):
231
237
# noinspection PyProtectedMember
232
238
model ._check_abstract ()
233
239
self .model = model
234
240
self .alias = model .__table__ .alias (* args , ** kwargs )
235
241
236
242
def __getattr__ (self , item ):
237
- rv = getattr (self .alias .columns , item ,
238
- getattr (self .alias , item ,
239
- getattr (self .model , item , DEFAULT )))
243
+ rv = getattr (
244
+ self .alias .columns ,
245
+ item ,
246
+ getattr (self .alias , item , getattr (self .model , item , DEFAULT )),
247
+ )
240
248
if rv is DEFAULT :
241
249
raise AttributeError
242
250
return rv
@@ -429,16 +437,15 @@ def _init_table(cls, sub_cls):
429
437
if isinstance (v , json_support .JSONProperty ):
430
438
if not hasattr (sub_cls , v .prop_name ):
431
439
raise AttributeError (
432
- 'Requires "{}" JSON[B] column.' .format (
433
- v . prop_name ) )
440
+ 'Requires "{}" JSON[B] column.' .format (v . prop_name )
441
+ )
434
442
v .name = k
435
443
if rv is not None :
436
444
rv .__model__ = weakref .ref (sub_cls )
437
445
return rv
438
446
439
447
@classmethod
440
- async def _create_without_instance (cls , bind = None , timeout = DEFAULT ,
441
- ** values ):
448
+ async def _create_without_instance (cls , bind = None , timeout = DEFAULT , ** values ):
442
449
return await cls (** values )._create (bind = bind , timeout = timeout )
443
450
444
451
async def _create (self , bind = None , timeout = DEFAULT ):
@@ -462,13 +469,14 @@ async def _create(self, bind=None, timeout=DEFAULT):
462
469
# insert into database
463
470
opts = dict (return_model = False , model = cls )
464
471
if timeout is not DEFAULT :
465
- opts [' timeout' ] = timeout
472
+ opts [" timeout" ] = timeout
466
473
# noinspection PyArgumentList
467
- q = cls .__table__ .insert ().values (
468
- ** self ._get_sa_values (self .__values__ )
469
- ).returning (
470
- * cls
471
- ).execution_options (** opts )
474
+ q = (
475
+ cls .__table__ .insert ()
476
+ .values (** self ._get_sa_values (self .__values__ ))
477
+ .returning (* cls )
478
+ .execution_options (** opts )
479
+ )
472
480
if bind is None :
473
481
bind = cls .__metadata__ .bind
474
482
row = await bind .first (q )
@@ -517,9 +525,9 @@ async def get(cls, ident, bind=None, timeout=DEFAULT):
517
525
columns = cls .__table__ .primary_key .columns
518
526
if len (ident_ ) != len (columns ):
519
527
raise ValueError (
520
- ' Incorrect number of values as primary key: '
521
- ' expected {}, got {}.' .format (
522
- len ( columns ), len ( ident_ )) )
528
+ " Incorrect number of values as primary key: "
529
+ " expected {}, got {}." .format (len ( columns ), len ( ident_ ))
530
+ )
523
531
clause = cls .query
524
532
for i , c in enumerate (columns ):
525
533
try :
@@ -575,9 +583,11 @@ def lookup(self):
575
583
if exps :
576
584
return sa .and_ (* exps )
577
585
else :
578
- raise LookupError ('Instance-level CRUD operations not allowed on '
579
- 'models without primary keys or lookup(), please'
580
- ' use model-level CRUD operations instead.' )
586
+ raise LookupError (
587
+ "Instance-level CRUD operations not allowed on "
588
+ "models without primary keys or lookup(), please"
589
+ " use model-level CRUD operations instead."
590
+ )
581
591
582
592
def _update (self , ** values ):
583
593
return self ._update_request_cls (self ).update (** values )
@@ -758,10 +768,15 @@ class QueryModel(type):
758
768
Metaclass of Model classes used for subqueries.
759
769
760
770
"""
771
+
761
772
def __getattr__ (self , item ):
762
- rv = getattr (self ._query .columns , item ,
763
- getattr (self ._model .__table__ .columns , item ,
764
- getattr (self ._model , item , DEFAULT )))
773
+ rv = getattr (
774
+ self ._query .columns ,
775
+ item ,
776
+ getattr (
777
+ self ._model .__table__ .columns , item , getattr (self ._model , item , DEFAULT )
778
+ ),
779
+ )
765
780
# replace `cls` in classmethod in models to `self`
766
781
if inspect .ismethod (rv ) and inspect .isclass (rv .__self__ ):
767
782
return lambda * args , ** kwargs : rv .__func__ (self , * args , ** kwargs )
0 commit comments