Skip to content

Commit c05116a

Browse files
author
Sourcery AI
committed
'Refactored by Sourcery'
1 parent bcc5a9b commit c05116a

File tree

15 files changed

+61
-105
lines changed

15 files changed

+61
-105
lines changed

examples/decorators/provide_session.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ def decorator(func: t.Callable[..., RT]) -> t.Callable[..., RT]:
4646
def wrapper(*args, **kwargs) -> RT:
4747
if "session" in kwargs or session_args_idx < len(args):
4848
return func(*args, **kwargs)
49-
else:
50-
bind = Bind.get_instance(bind_name)
49+
bind = Bind.get_instance(bind_name)
5150

52-
with create_session(bind) as session:
53-
return func(*args, session=session, **kwargs)
51+
with create_session(bind) as session:
52+
return func(*args, session=session, **kwargs)
5453

5554
return wrapper
5655

examples/usrsrv/component/repository.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ def __init__(self, session: Session):
5353
self._query = select(EntityMapper)
5454

5555
def get(self, entity_id: EntityID) -> Entity:
56-
dto = self._session.scalars(self._query.filter_by(uuid=entity_id)).one_or_none()
57-
if not dto:
56+
if dto := self._session.scalars(
57+
self._query.filter_by(uuid=entity_id)
58+
).one_or_none():
59+
return Entity(dto)
60+
else:
5861
raise NotFound(entity_id)
59-
return Entity(dto)
6062

6163
def save(self, entity: Entity) -> None:
6264
self._session.add(entity.dto)

src/quart_sqlalchemy/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ class EngineConfig(ConfigBase):
146146
@root_validator
147147
def scrub_execution_options(cls, values):
148148
if "execution_options" in values:
149-
execute_options = values["execution_options"].dict(exclude_defaults=True)
150-
if execute_options:
149+
if execute_options := values["execution_options"].dict(
150+
exclude_defaults=True
151+
):
151152
values["execution_options"] = execute_options
152153
return values
153154

src/quart_sqlalchemy/framework/extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(
1414
config: t.Optional[SQLAlchemyConfig] = None,
1515
app: t.Optional[Quart] = None,
1616
):
17-
initialize = False if config is None else True
17+
initialize = config is not None
1818
super().__init__(config, initialize=initialize)
1919

2020
if app is not None:

src/quart_sqlalchemy/model/mixins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def accumulate_mappings(class_, attribute) -> t.Dict[str, t.Any]:
301301
if base_class is class_:
302302
continue
303303
args = getattr(base_class, attribute, {})
304-
accumulated.update(args)
304+
accumulated |= args
305305

306306
return accumulated
307307

@@ -316,7 +316,7 @@ def accumulate_tuples_with_mapping(class_, attribute) -> t.Sequence[t.Any]:
316316
args = getattr(base_class, attribute, ())
317317
for arg in args:
318318
if isinstance(arg, t.Mapping):
319-
accumulated_map.update(arg)
319+
accumulated_map |= arg
320320
else:
321321
accumulated_args.append(arg)
322322

src/quart_sqlalchemy/session.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ def provide_global_contextual_session(func):
4545
@wraps(func)
4646
def wrapper(self, *args, **kwargs):
4747
session_in_args = any(
48-
[isinstance(arg, (sa.orm.Session, sa.ext.asyncio.AsyncSession)) for arg in args]
48+
isinstance(arg, (sa.orm.Session, sa.ext.asyncio.AsyncSession))
49+
for arg in args
4950
)
5051
session_in_kwargs = "session" in kwargs
5152
session_provided = session_in_args or session_in_kwargs
5253

5354
if session_provided:
5455
return func(self, *args, **kwargs)
55-
else:
56-
session = session_proxy()
56+
session = session_proxy()
5757

58-
return func(self, session, *args, **kwargs)
58+
return func(self, session, *args, **kwargs)
5959

6060
return wrapper
6161

src/quart_sqlalchemy/sim/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def auth_endpoint_security(self):
228228
results = self.authenticator.enforce(security_schemes, session)
229229
authorized_credentials = {}
230230
for result in results:
231-
authorized_credentials.update(result)
231+
authorized_credentials |= result
232232
g.authorized_credentials = authorized_credentials
233233

234234

src/quart_sqlalchemy/sim/config.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@
1616
sa = sqlalchemy
1717

1818

19+
20+
1921
class AppSettings(BaseSettings):
22+
2023
class Config:
2124
env_file = ".env", ".secrets.env"
2225

2326
LOAD_BLUEPRINTS: t.List[str] = Field(
24-
default_factory=lambda: list(("quart_sqlalchemy.sim.views.api",))
27+
default_factory=lambda: ["quart_sqlalchemy.sim.views.api"]
2528
)
2629
LOAD_EXTENSIONS: t.List[str] = Field(
27-
default_factory=lambda: list(
28-
(
29-
"quart_sqlalchemy.sim.db.db",
30-
"quart_sqlalchemy.sim.app.schema",
31-
"quart_sqlalchemy.sim.auth.auth",
32-
)
33-
)
30+
default_factory=lambda: [
31+
"quart_sqlalchemy.sim.db.db",
32+
"quart_sqlalchemy.sim.app.schema",
33+
"quart_sqlalchemy.sim.auth.auth",
34+
]
3435
)
3536
SECURITY_SCHEMES: t.Dict[str, SecuritySchemeBase] = Field(
3637
default_factory=lambda: {
@@ -52,4 +53,5 @@ class Config:
5253
WEB3_HTTPS_PROVIDER_URI: str = Field(env="WEB3_HTTPS_PROVIDER_URI")
5354

5455

56+
5557
settings = AppSettings()

src/quart_sqlalchemy/sim/db.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,13 @@ def process_bind_param(self, value, dialect):
5151
"""Data going into to the database will be transformed by this method.
5252
See ``ObjectID`` for the design and rational for this.
5353
"""
54-
if value is None:
55-
return None
56-
57-
return ObjectID(value).decode()
54+
return None if value is None else ObjectID(value).decode()
5855

5956
def process_result_value(self, value, dialect):
6057
"""Data going out from the database will be explicitly casted to the
6158
``ObjectID``.
6259
"""
63-
if value is None:
64-
return None
65-
66-
return ObjectID(value)
60+
return None if value is None else ObjectID(value)
6761

6862

6963
class MyBase(BaseMixins, sa.orm.DeclarativeBase):

src/quart_sqlalchemy/sim/handle.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,13 @@ def update_app_name_by_id(self, magic_client_id, app_name):
9696
self.session_factory(), magic_client_id, app_name=app_name
9797
)
9898

99-
if not client:
100-
return None
101-
102-
return client.app_name
99+
return client.app_name if client else None
103100

104101
def update_by_id(self, magic_client_id, **kwargs):
105-
client = self.logic.MagicClient.update_by_id(
102+
return self.logic.MagicClient.update_by_id(
106103
self.session_factory(), magic_client_id, **kwargs
107104
)
108105

109-
return client
110-
111106
def set_inactive_by_id(self, magic_client_id):
112107
"""
113108
Args:
@@ -314,11 +309,10 @@ def sync_auth_wallet(
314309
):
315310
session = self.session_factory()
316311
with session.begin_nested():
317-
existing_wallet = self.logic.AuthWallet.get_by_auth_user_id(
312+
if existing_wallet := self.logic.AuthWallet.get_by_auth_user_id(
318313
session,
319314
auth_user_id,
320-
)
321-
if existing_wallet:
315+
):
322316
raise RuntimeError("WalletExistsForNetworkAndWalletType")
323317

324318
return self.logic.AuthWallet.add(

0 commit comments

Comments
 (0)