Skip to content

Commit eb61e24

Browse files
author
Sourcery AI
committed
'Refactored by Sourcery'
1 parent 51ccb5c commit eb61e24

File tree

9 files changed

+60
-140
lines changed

9 files changed

+60
-140
lines changed

src/quart_sqlalchemy/model/mixins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def __lt__(self, other):
6363
if column.primary_key:
6464
continue
6565

66-
if not (getattr(self, key) == getattr(other, key)):
66+
if getattr(self, key) != getattr(other, key):
6767
return False
6868
return True
6969

@@ -263,7 +263,7 @@ def accumulate_mappings(class_, attribute) -> t.Dict[str, t.Any]:
263263
if base_class is class_:
264264
continue
265265
args = getattr(base_class, attribute, {})
266-
accumulated.update(args)
266+
accumulated |= args
267267

268268
return accumulated
269269

@@ -278,7 +278,7 @@ def accumulate_tuples_with_mapping(class_, attribute) -> t.Sequence[t.Any]:
278278
args = getattr(base_class, attribute, ())
279279
for arg in args:
280280
if isinstance(arg, t.Mapping):
281-
accumulated_map.update(arg)
281+
accumulated_map |= arg
282282
else:
283283
accumulated_args.append(arg)
284284

src/quart_sqlalchemy/sim/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def auth_endpoint_security(self):
249249
results = self.authenticator.enforce(security_schemes, session)
250250
authorized_credentials = {}
251251
for result in results:
252-
authorized_credentials.update(result)
252+
authorized_credentials |= result
253253
g.authorized_credentials = authorized_credentials
254254

255255

src/quart_sqlalchemy/sim/db.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,13 @@ def process_bind_param(self, value, dialect):
3232
"""Data going into to the database will be transformed by this method.
3333
See ``ObjectID`` for the design and rational for this.
3434
"""
35-
if value is None:
36-
return None
37-
38-
return ObjectID(value).decode()
35+
return None if value is None else ObjectID(value).decode()
3936

4037
def process_result_value(self, value, dialect):
4138
"""Data going out from the database will be explicitly casted to the
4239
``ObjectID``.
4340
"""
44-
if value is None:
45-
return None
46-
47-
return ObjectID(value)
41+
return None if value is None else ObjectID(value)
4842

4943

5044
class MyBase(Base):

src/quart_sqlalchemy/sim/handle.py

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,12 @@ def update_app_name_by_id(self, magic_client_id, app_name):
145145
self.session, magic_client_id, app_name=app_name
146146
)
147147

148-
if not client:
149-
return None
150-
151-
return client.app_name
148+
return client.app_name if client else None
152149

153150
def update_by_id(self, magic_client_id, **kwargs):
154-
client = self.logic.MagicClient.update_by_id(self.session, magic_client_id, **kwargs)
155-
156-
return client
151+
return self.logic.MagicClient.update_by_id(
152+
self.session, magic_client_id, **kwargs
153+
)
157154

158155
def set_inactive_by_id(self, magic_client_id):
159156
"""
@@ -272,37 +269,17 @@ def get_or_create_by_email_and_client_id(
272269
client_id,
273270
user_type=EntityType.MAGIC.value,
274271
):
275-
auth_user = self.logic.AuthUser.get_by_email_and_client_id(
272+
return self.logic.AuthUser.get_by_email_and_client_id(
276273
self.session,
277274
email,
278275
client_id,
279276
user_type=user_type,
277+
) or self.logic.AuthUser.add_by_email_and_client_id(
278+
self.session,
279+
client_id,
280+
email=email,
281+
user_type=user_type,
280282
)
281-
if not auth_user:
282-
# try:
283-
# email = enhanced_email_validation(
284-
# email,
285-
# source=MAGIC,
286-
# # So we don't affect sign-up.
287-
# silence_network_error=True,
288-
# )
289-
# except (
290-
# EnhanceEmailValidationError,
291-
# EnhanceEmailSuggestionError,
292-
# ) as e:
293-
# logger.warning(
294-
# "Email Start Attempt.",
295-
# exc_info=True,
296-
# )
297-
# raise EnhancedEmailValidation(error_message=str(e)) from e
298-
299-
auth_user = self.logic.AuthUser.add_by_email_and_client_id(
300-
self.session,
301-
client_id,
302-
email=email,
303-
user_type=user_type,
304-
)
305-
return auth_user
306283

307284
def get_by_id_and_validate_exists(self, auth_user_id):
308285
"""This function helps formalize how a non-existent auth user should be handled."""
@@ -327,14 +304,12 @@ def create_verified_user(
327304
email,
328305
user_type=user_type,
329306
).id
330-
auth_user = self.logic.AuthUser._update_by_id(
307+
return self.logic.AuthUser._update_by_id(
331308
self.session,
332309
auid,
333310
date_verified=datetime.utcnow(),
334311
)
335312

336-
return auth_user
337-
338313
# def get_auth_user_from_public_address(self, public_address):
339314
# wallet = self.logic.AuthWallet.get_by_public_address(public_address)
340315

@@ -458,7 +433,7 @@ def search_by_client_id_and_substring(
458433
if not isinstance(substring, str) or len(substring) < 3:
459434
raise InvalidSubstringError()
460435

461-
auth_users = self.logic.AuthUser.get_by_client_id_with_substring_search(
436+
return self.logic.AuthUser.get_by_client_id_with_substring_search(
462437
self.session,
463438
client_id,
464439
substring,
@@ -467,15 +442,6 @@ def search_by_client_id_and_substring(
467442
# join_list=join_list,
468443
)
469444

470-
# mfa_enablements = self.auth_user_mfa_handler.is_active_batch(
471-
# [auth_user.id for auth_user in auth_users],
472-
# )
473-
# for auth_user in auth_users:
474-
# if mfa_enablements[auth_user.id] is False:
475-
# auth_user.mfa_methods = []
476-
477-
return auth_users
478-
479445
def is_magic_connect_enabled(self, auth_user_id=None, auth_user=None):
480446
if auth_user is None and auth_user_id is None:
481447
raise Exception("At least one argument needed: auth_user_id or auth_user.")
@@ -575,11 +541,10 @@ def sync_auth_wallet(
575541
encrypted_private_address,
576542
wallet_management_type,
577543
):
578-
existing_wallet = self.logic.AuthWallet.get_by_auth_user_id(
544+
if existing_wallet := self.logic.AuthWallet.get_by_auth_user_id(
579545
self.session,
580546
auth_user_id,
581-
)
582-
if existing_wallet:
547+
):
583548
raise RuntimeError("WalletExistsForNetworkAndWalletType")
584549

585550
return self.logic.AuthWallet.add(

src/quart_sqlalchemy/sim/logic.py

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,7 @@ def _get_or_add_by_phone_number_and_client_id(
230230
provenance=Provenance.SMS,
231231
)
232232
logger.info(
233-
"New auth user (id: {}) created by phone number (client_id: {})".format(
234-
row.id,
235-
client_id,
236-
),
233+
f"New auth user (id: {row.id}) created by phone number (client_id: {client_id})"
237234
)
238235

239236
return row
@@ -261,10 +258,7 @@ def _add_by_email_and_client_id(
261258
user_type=user_type,
262259
):
263260
logger.exception(
264-
"User duplication for email: {} (client_id: {})".format(
265-
email,
266-
client_id,
267-
),
261+
f"User duplication for email: {email} (client_id: {client_id})"
268262
)
269263
raise DuplicateAuthUser()
270264

@@ -276,10 +270,7 @@ def _add_by_email_and_client_id(
276270
**kwargs,
277271
)
278272
logger.info(
279-
"New auth user (id: {}) created by email (client_id: {})".format(
280-
row.id,
281-
client_id,
282-
),
273+
f"New auth user (id: {row.id}) created by email (client_id: {client_id})"
283274
)
284275

285276
return row
@@ -305,10 +296,7 @@ def _add_by_client_id(
305296
date_verified=datetime.utcnow() if is_verified else None,
306297
)
307298
logger.info(
308-
"New auth user (id: {}) created by (client_id: {})".format(
309-
row.id,
310-
client_id,
311-
),
299+
f"New auth user (id: {row.id}) created by (client_id: {client_id})"
312300
)
313301

314302
return row
@@ -559,20 +547,21 @@ def _get_by_client_ids_and_user_type(
559547
offset=None,
560548
limit=None,
561549
):
562-
if not client_ids:
563-
return []
564-
565-
return self._repository.get_by(
566-
session,
567-
filters=[
568-
auth_user_model.client_id.in_(client_ids),
569-
auth_user_model.user_type == user_type,
570-
# auth_user_model.is_active == True, # noqa: E712,
571-
auth_user_model.date_verified != None,
572-
],
573-
offset=offset,
574-
limit=limit,
575-
order_by_clause=auth_user_model.id.desc(),
550+
return (
551+
self._repository.get_by(
552+
session,
553+
filters=[
554+
auth_user_model.client_id.in_(client_ids),
555+
auth_user_model.user_type == user_type,
556+
# auth_user_model.is_active == True, # noqa: E712,
557+
auth_user_model.date_verified != None,
558+
],
559+
offset=offset,
560+
limit=limit,
561+
order_by_clause=auth_user_model.id.desc(),
562+
)
563+
if client_ids
564+
else []
576565
)
577566

578567
# get_by_client_ids_and_user_type = with_db_session(ro=True)(
@@ -597,7 +586,7 @@ def _get_by_client_id_with_substring_search(
597586
or_(
598587
auth_user_model.provenance == Provenance.SMS,
599588
auth_user_model.provenance == Provenance.LINK,
600-
auth_user_model.provenance == None, # noqa: E711
589+
auth_user_model.provenance is None,
601590
),
602591
or_(
603592
auth_user_model.phone_number.contains(substring),
@@ -691,7 +680,8 @@ def _get_by_email_for_interop(
691680
.options(contains_eager(auth_user_model.wallets))
692681
.join(
693682
auth_user_model.magic_client.and_(
694-
magic_client_model.connect_interop == ConnectInteropStatus.ENABLED,
683+
magic_client_model.connect_interop
684+
== ConnectInteropStatus.ENABLED,
695685
),
696686
)
697687
.options(contains_eager(auth_user_model.magic_client))
@@ -708,8 +698,7 @@ def _get_by_email_for_interop(
708698
.filter(
709699
auth_user_model.email == email,
710700
auth_user_model.user_type == EntityType.MAGIC.value,
711-
# auth_user_model.is_active == 1,
712-
auth_user_model.linked_primary_auth_user_id == None, # noqa: E711
701+
auth_user_model.linked_primary_auth_user_id is None,
713702
)
714703
.populate_existing()
715704
)
@@ -764,7 +753,7 @@ def _add(
764753
management_type=None,
765754
auth_user_id=None,
766755
):
767-
new_row = self._repository.add(
756+
return self._repository.add(
768757
session,
769758
auth_user_id=auth_user_id,
770759
public_address=public_address,
@@ -774,8 +763,6 @@ def _add(
774763
network=network,
775764
)
776765

777-
return new_row
778-
779766
# add = with_db_session(ro=False)(_add)
780767
add = _add
781768

@@ -813,10 +800,7 @@ def get_by_public_address(self, session, public_address, network=None, is_active
813800

814801
row = self._repository.get_by(session, filters=filters, allow_inactive=not is_active)
815802

816-
if not row:
817-
return None
818-
819-
return one(row)
803+
return one(row) if row else None
820804

821805
# @with_db_session(ro=True)
822806
def get_by_auth_user_id(
@@ -857,10 +841,7 @@ def get_by_auth_user_id(
857841
session, filters=filters, join_list=join_list, allow_inactive=not is_active
858842
)
859843

860-
if not rows:
861-
return []
862-
863-
return rows
844+
return rows or []
864845

865846
def _update_by_id(self, session, model_id, **kwargs):
866847
self._repository.update(session, model_id, **kwargs)

0 commit comments

Comments
 (0)