@@ -39,8 +39,8 @@ def select(*args):
3939 q = sa .select ([getattr (owner , x ) for x in args ])
4040 if instance is not None :
4141 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+
4444 return select
4545
4646
@@ -79,7 +79,8 @@ class UpdateRequest:
7979 specific model instance and its database row.
8080
8181 """
82- def __init__ (self , instance : 'CRUDModel' ):
82+
83+ def __init__ (self , instance : "CRUDModel" ):
8384 self ._instance = instance
8485 self ._values = {}
8586 self ._props = {}
@@ -116,8 +117,10 @@ async def apply(self, bind=None, timeout=DEFAULT):
116117 """
117118 if self ._locator is None :
118119 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+ )
121124 cls = type (self ._instance )
122125 values = self ._values .copy ()
123126
@@ -137,36 +140,38 @@ async def apply(self, bind=None, timeout=DEFAULT):
137140 for prop_name , updates in json_updates .items ():
138141 prop = getattr (cls , prop_name )
139142 from .dialects .asyncpg import JSONB
143+
140144 if isinstance (prop .type , JSONB ):
141145 if self ._literal :
142146 values [prop_name ] = prop .concat (updates )
143147 else :
144148 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+ )
147151 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+ )
151157
152158 opts = dict (return_model = False )
153159 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+ )
162168 if bind is None :
163169 bind = cls .__metadata__ .bind
164170 row = await bind .first (clause )
165171 if not row :
166172 raise NoSuchRowError ()
167173 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
170175 for prop in self ._props :
171176 prop .reload (self ._instance )
172177 return self
@@ -208,11 +213,11 @@ def update(self, **values):
208213 for key , value in values .items ():
209214 prop = cls .__dict__ .get (key )
210215 if isinstance (prop , json_support .JSONProperty ):
211- value_from = ' __profile__'
216+ value_from = " __profile__"
212217 method = self ._set_prop
213218 k = prop
214219 else :
215- value_from = ' __values__'
220+ value_from = " __values__"
216221 method = self ._set
217222 k = key
218223 if not isinstance (value , ClauseElement ):
@@ -227,16 +232,19 @@ class Alias:
227232 Experimental proxy for table alias on model.
228233
229234 """
235+
230236 def __init__ (self , model , * args , ** kwargs ):
231237 # noinspection PyProtectedMember
232238 model ._check_abstract ()
233239 self .model = model
234240 self .alias = model .__table__ .alias (* args , ** kwargs )
235241
236242 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+ )
240248 if rv is DEFAULT :
241249 raise AttributeError
242250 return rv
@@ -429,16 +437,15 @@ def _init_table(cls, sub_cls):
429437 if isinstance (v , json_support .JSONProperty ):
430438 if not hasattr (sub_cls , v .prop_name ):
431439 raise AttributeError (
432- 'Requires "{}" JSON[B] column.' .format (
433- v . prop_name ) )
440+ 'Requires "{}" JSON[B] column.' .format (v . prop_name )
441+ )
434442 v .name = k
435443 if rv is not None :
436444 rv .__model__ = weakref .ref (sub_cls )
437445 return rv
438446
439447 @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 ):
442449 return await cls (** values )._create (bind = bind , timeout = timeout )
443450
444451 async def _create (self , bind = None , timeout = DEFAULT ):
@@ -462,13 +469,14 @@ async def _create(self, bind=None, timeout=DEFAULT):
462469 # insert into database
463470 opts = dict (return_model = False , model = cls )
464471 if timeout is not DEFAULT :
465- opts [' timeout' ] = timeout
472+ opts [" timeout" ] = timeout
466473 # 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+ )
472480 if bind is None :
473481 bind = cls .__metadata__ .bind
474482 row = await bind .first (q )
@@ -517,9 +525,9 @@ async def get(cls, ident, bind=None, timeout=DEFAULT):
517525 columns = cls .__table__ .primary_key .columns
518526 if len (ident_ ) != len (columns ):
519527 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+ )
523531 clause = cls .query
524532 for i , c in enumerate (columns ):
525533 try :
@@ -575,9 +583,11 @@ def lookup(self):
575583 if exps :
576584 return sa .and_ (* exps )
577585 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+ )
581591
582592 def _update (self , ** values ):
583593 return self ._update_request_cls (self ).update (** values )
@@ -758,10 +768,15 @@ class QueryModel(type):
758768 Metaclass of Model classes used for subqueries.
759769
760770 """
771+
761772 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+ )
765780 # replace `cls` in classmethod in models to `self`
766781 if inspect .ismethod (rv ) and inspect .isclass (rv .__self__ ):
767782 return lambda * args , ** kwargs : rv .__func__ (self , * args , ** kwargs )
0 commit comments