Skip to content

Commit d498c1f

Browse files
committed
Simplify logical expression in Multiple Functions
1 parent e61ecaa commit d498c1f

File tree

6 files changed

+55
-65
lines changed

6 files changed

+55
-65
lines changed

fastapi_contrib/auth/backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async def authenticate(
4242
if not authorization:
4343
return False, None
4444
scheme, credentials = get_authorization_scheme_param(authorization)
45-
if not (authorization and scheme and credentials):
45+
if not scheme or not credentials:
4646
raise AuthenticationError("Not authenticated")
4747
if scheme.lower() != "token":
4848
raise AuthenticationError("Invalid authentication credentials")

fastapi_contrib/common/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ def get_current_app() -> FastAPI:
7272
:return: FastAPI app
7373
"""
7474
# TODO: cache this
75-
app = resolve_dotted_path(settings.fastapi_app)
76-
return app
75+
return resolve_dotted_path(settings.fastapi_app)
7776

7877

7978
def async_timing(func):

fastapi_contrib/db/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ def get_db_client():
6363
"""
6464
from fastapi_contrib.db.client import MongoDBClient
6565

66-
client = MongoDBClient()
67-
return client
66+
return MongoDBClient()
6867

6968

7069
def get_models() -> list:

fastapi_contrib/exception_handlers.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ def parse_error(
2222
"""
2323

2424
if isinstance(err.exc, EnumError):
25-
permitted_values = ", ".join(
26-
[f"'{val}'" for val in err.exc.enum_values]
27-
)
25+
permitted_values = ", ".join(f"'{val}'" for val in err.exc.enum_values)
2826
message = f"Value is not a valid enumeration member; " \
2927
f"permitted: {permitted_values}."
3028
elif isinstance(err.exc, StrRegexError):
@@ -51,13 +49,14 @@ def parse_error(
5149
name = str(err.loc_tuple()[0])
5250
else:
5351
name = "__all__"
52+
elif (
53+
len(err.loc_tuple()) == 2
54+
or len(err.loc_tuple()) != 2
55+
and len(err.loc_tuple()) == 1
56+
):
57+
name = str(err.loc_tuple()[0])
5458
else:
55-
if len(err.loc_tuple()) == 2:
56-
name = str(err.loc_tuple()[0])
57-
elif len(err.loc_tuple()) == 1:
58-
name = str(err.loc_tuple()[0])
59-
else:
60-
name = "__all__"
59+
name = "__all__"
6160

6261
if name in field_names:
6362
return None

fastapi_contrib/routes.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ async def custom_route_handler(request: Request) -> Response:
1717
return await original_route_handler(request)
1818
except RequestValidationError as exc:
1919
body = await request.body()
20-
if not body:
21-
status_code = 400
22-
data = {
23-
"code": status_code,
24-
"detail": "Empty body for this request is not valid.",
25-
"fields": [],
26-
}
27-
return UJSONResponse(data, status_code=status_code)
28-
else:
20+
if body:
2921
raise exc
3022

23+
status_code = 400
24+
data = {
25+
"code": status_code,
26+
"detail": "Empty body for this request is not valid.",
27+
"fields": [],
28+
}
29+
return UJSONResponse(data, status_code=status_code)
30+
3131
return custom_route_handler

fastapi_contrib/serializers/common.py

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,23 @@ async def update_one(
137137
138138
:return: result of update operation
139139
"""
140-
if (
141-
hasattr(self, "Meta")
142-
and getattr(self.Meta, "model", None) is not None
143-
):
144-
data = {}
145-
fields = self.dict(skip_defaults=skip_defaults)
146-
147-
if not array_fields:
148-
array_fields = []
149-
150-
if array_fields:
151-
tmp_data = {}
152-
for i in array_fields:
153-
tmp_data[i] = {"$each": fields.pop(i)}
154-
data.update({"$push": tmp_data})
155-
if fields:
156-
data.update({"$set": fields})
157-
return await self.Meta.model.update_one(
158-
filter_kwargs=filter_kwargs, **data
159-
)
140+
if not hasattr(self, "Meta") or getattr(self.Meta, "model", None) is None:
141+
return
142+
143+
data = {}
144+
fields = self.dict(skip_defaults=skip_defaults)
145+
146+
if not array_fields:
147+
array_fields = []
148+
149+
if array_fields:
150+
tmp_data = {i: {"$each": fields.pop(i)} for i in array_fields}
151+
data.update({"$push": tmp_data})
152+
if fields:
153+
data.update({"$set": fields})
154+
return await self.Meta.model.update_one(
155+
filter_kwargs=filter_kwargs, **data
156+
)
160157

161158
async def update_many(
162159
self,
@@ -170,26 +167,23 @@ async def update_many(
170167
171168
:return: result of update many operation
172169
"""
173-
if (
174-
hasattr(self, "Meta")
175-
and getattr(self.Meta, "model", None) is not None
176-
):
177-
data = {}
178-
fields = self.dict(skip_defaults=skip_defaults)
179-
180-
if not array_fields:
181-
array_fields = []
182-
183-
if array_fields:
184-
tmp_data = {}
185-
for i in array_fields:
186-
tmp_data[i] = {"$each": fields.pop(i)}
187-
data.update({"$push": tmp_data})
188-
if fields:
189-
data.update({"$set": fields})
190-
return await self.Meta.model.update_many(
191-
filter_kwargs=filter_kwargs, **data
192-
)
170+
if not hasattr(self, "Meta") or getattr(self.Meta, "model", None) is None:
171+
return
172+
173+
data = {}
174+
fields = self.dict(skip_defaults=skip_defaults)
175+
176+
if not array_fields:
177+
array_fields = []
178+
179+
if array_fields:
180+
tmp_data = {i: {"$each": fields.pop(i)} for i in array_fields}
181+
data.update({"$push": tmp_data})
182+
if fields:
183+
data.update({"$set": fields})
184+
return await self.Meta.model.update_many(
185+
filter_kwargs=filter_kwargs, **data
186+
)
193187

194188
def dict(self, *args, **kwargs) -> dict:
195189
"""
@@ -212,8 +206,7 @@ def dict(self, *args, **kwargs) -> dict:
212206
exclude.update(self.Meta.write_only_fields)
213207

214208
kwargs.update({"exclude": exclude})
215-
original = super().dict(*args, **kwargs)
216-
return original
209+
return super().dict(*args, **kwargs)
217210

218211
class Meta(AbstractMeta):
219212
...

0 commit comments

Comments
 (0)