-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery Starbot ⭐ refactored Alc-Alc/starlite #1
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to GitHub API limits, only the first 60 comments can be shown.
response = jwt_auth.login(identifier=str(data.id), response_body=data) | ||
|
||
# you can do whatever you want to update the response instance here | ||
# e.g. response.set_cookie(...) | ||
|
||
return response | ||
return jwt_auth.login(identifier=str(data.id), response_body=data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function login_handler
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
This removes the following comments ( why? ):
# you can do whatever you want to update the response instance here
# e.g. response.set_cookie(...)
response = jwt_cookie_auth.login(identifier=str(data.id), response_body=data) | ||
|
||
# you can do whatever you want to update the response instance here | ||
# e.g. response.set_cookie(...) | ||
|
||
return response | ||
return jwt_cookie_auth.login(identifier=str(data.id), response_body=data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function login_handler
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
This removes the following comments ( why? ):
# you can do whatever you want to update the response instance here
# e.g. response.set_cookie(...)
# if we do not define a response body, the login process will return a standard OAuth2 login response. Note the `Response[OAuth2Login]` return type. | ||
response = oauth2_auth.login(identifier=str(data.id)) | ||
|
||
# you can do whatever you want to update the response instance here | ||
# e.g. response.set_cookie(...) | ||
|
||
return response | ||
return oauth2_auth.login(identifier=str(data.id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function login_handler
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
This removes the following comments ( why? ):
# you can do whatever you want to update the response instance here
# e.g. response.set_cookie(...)
# if we do not define a response body, the login process will return a standard OAuth2 login response. Note the `Response[OAuth2Login]` return type.
# If you'd like to define a custom response body, use the `response_body` parameter. Note the `Response[User]` return type. | ||
response = oauth2_auth.login(identifier=str(data.id), response_body=data) | ||
|
||
# you can do whatever you want to update the response instance here | ||
# e.g. response.set_cookie(...) | ||
|
||
return response | ||
return oauth2_auth.login(identifier=str(data.id), response_body=data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function login_custom_response_handler
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
This removes the following comments ( why? ):
# you can do whatever you want to update the response instance here
# e.g. response.set_cookie(...)
# If you'd like to define a custom response body, use the `response_body` parameter. Note the `Response[User]` return type.
if not (token == VALID_TOKEN and cookie == VALID_COOKIE_VALUE): | ||
if token != VALID_TOKEN or cookie != VALID_COOKIE_VALUE: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_user
refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan
)
field_definitions: Dict[str, Tuple[Any, None]] = {} | ||
for field_name, field_type in get_type_hints(item).items(): | ||
if is_classvar(field_type): | ||
continue | ||
if not isinstance(field_type, GenericAlias) or NoneType not in field_type.__args__: | ||
field_definitions[field_name] = (Optional[field_type], None) | ||
else: | ||
field_definitions[field_name] = (field_type, None) | ||
|
||
field_definitions: Dict[str, Tuple[Any, None]] = { | ||
field_name: (Optional[field_type], None) | ||
if not isinstance(field_type, GenericAlias) | ||
or NoneType not in field_type.__args__ | ||
else (field_type, None) | ||
for field_name, field_type in get_type_hints(item).items() | ||
if not is_classvar(field_type) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Partial._create_partial_pydantic_model
refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension
) - Lift code into else after jump in control flow (
reintroduce-else
) - Remove redundant continue statement (
remove-redundant-continue
) - Replace if statement with if expression (
assign-if-exp
)
is_mount = hasattr(route, "route_handler") and getattr(route.route_handler, "is_mount", False) # pyright: ignore | ||
has_path_parameters = bool(route.path_parameters) | ||
|
||
if is_mount: # pyright: ignore | ||
if is_mount := hasattr(route, "route_handler") and getattr( | ||
route.route_handler, "is_mount", False | ||
): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function add_route_to_trie
refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block
) - Use named expression to simplify assignment and conditional (
use-named-expression
)
This removes the following comments ( why? ):
# pyright: ignore
if not mount_node.children or not any(sub_route in path for sub_route in mount_node.children): # type: ignore | ||
if not mount_node.children or all( | ||
sub_route not in path for sub_route in mount_node.children | ||
): # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function parse_path_to_route
refactored with the following changes:
- Invert any/all to simplify comparisons (
invert-any-all
)
v | ||
for v in chain.from_iterable( | ||
chain.from_iterable( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function validate_node
refactored with the following changes:
- Simplify generator expression (
simplify-generator
)
used_reserved_kwargs = {*parameter_names, *path_parameters, *dependency_keys}.intersection(RESERVED_KWARGS) | ||
if used_reserved_kwargs: | ||
if used_reserved_kwargs := { | ||
*parameter_names, | ||
*path_parameters, | ||
*dependency_keys, | ||
}.intersection(RESERVED_KWARGS): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function KwargsModel._validate_raw_kwargs
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
and (default_value is None and not (signature_field.is_optional or signature_field.is_any)), | ||
and default_value is None | ||
and not signature_field.is_optional | ||
and not signature_field.is_any, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function create_parameter_definition
refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan
)
@@ -12,7 +12,6 @@ | |||
if TYPE_CHECKING: | |||
from starlite.plugins import OpenAPISchemaPluginProtocol | |||
|
|||
if TYPE_CHECKING: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 15-15
refactored with the following changes:
- Merge repeated if statements into single if (
merge-repeated-ifs
)
if len(items) > 1: | ||
schema.items = Schema(one_of=items) | ||
else: | ||
schema.items = items[0] | ||
schema.items = Schema(one_of=items) if len(items) > 1 else items[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function create_collection_constrained_field_schema
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
if not allow_quoted and not (value[0].isalpha() or value[0] in {"_", "$"}): | ||
if ( | ||
not allow_quoted | ||
and not value[0].isalpha() | ||
and value[0] not in {"_", "$"} | ||
): | ||
raise ValueError(f"invalid typescript namespace {value}") | ||
if allow_quoted: | ||
if allowed_key_re.fullmatch(value): | ||
return value | ||
return f'"{value}"' | ||
return value if allowed_key_re.fullmatch(value) else f'"{value}"' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function normalize_typescript_namespace
refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan
) - Swap positions of nested conditionals (
swap-nested-ifs
) - Lift code into else after jump in control flow (
reintroduce-else
) - Hoist nested repeated code outside conditional statements (
hoist-similar-statement-from-if
) - Replace if statement with if expression (
assign-if-exp
)
return '"' + value + '"' | ||
return f'"{value}"' | ||
|
||
if isinstance(value, bool): | ||
return "true" if value else "false" | ||
|
||
if value is None: | ||
return "null" | ||
|
||
return str(value) | ||
return "null" if value is None else str(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function _as_string
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
) - Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
spec: dict[str, Any] = {} | ||
for key, value in location.items(): | ||
if value: | ||
spec[key] = value | ||
if not spec: | ||
if spec := {key: value for key, value in location.items() if value}: | ||
return {HTMXHeaders.LOCATION.value: encode_json(spec).decode()} | ||
else: | ||
raise ValueError("redirect_to is required parameter.") | ||
return {HTMXHeaders.LOCATION.value: encode_json(spec).decode()} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_location_headers
refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension
) - Lift code into else after jump in control flow (
reintroduce-else
) - Swap if/else branches (
swap-if-else-branches
) - Use named expression to simplify assignment and conditional (
use-named-expression
)
response = htmx_headers_dict[key](value) | ||
return response | ||
return htmx_headers_dict[key](value) | ||
if value is not None: | ||
response = htmx_headers_dict[key](value) | ||
header.update(response) | ||
header |= response |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_headers
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
) - Merge dictionary updates via the union operator (
dict-assign-update-to-union
)
if value and self.request.headers.get(name + "-URI-AutoEncoded") == "true": | ||
if value and self.request.headers.get(f"{name}-URI-AutoEncoded") == "true": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function HTMXDetails._get_header_value
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
if self._model_has_updated and do_created: | ||
data.created = now # type:ignore[attr-defined] | ||
if do_created: | ||
data.created = now # type:ignore[attr-defined] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function GenericMockRepository._update_audit_attributes
refactored with the following changes:
- Hoist a repeated condition into a parent condition (
hoist-repeated-if-condition
)
return bool(existing > 0) | ||
return existing > 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function SQLAlchemyRepository.exists
refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
if column_type.asdecimal: | ||
return Decimal | ||
return float | ||
return Decimal if column_type.asdecimal else float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function SQLAlchemyPlugin.handle_numeric_type
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
if key == "max_age": | ||
if updated_key == "max_age": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Cookie.simple_cookie
refactored with the following changes:
- Use previously assigned local variable (
use-assigned-variable
)
indices = self._find_indices(key) | ||
if not indices: | ||
self.headers.append((name_encoded, value_encoded)) | ||
else: | ||
if indices := self._find_indices(key): | ||
for i in indices[1:]: | ||
del self.headers[i] | ||
self.headers[indices[0]] = (name_encoded, value_encoded) | ||
else: | ||
self.headers.append((name_encoded, value_encoded)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function MutableScopeHeaders.__setitem__
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Swap if/else branches (
swap-if-else-branches
)
cc_items = [] | ||
for key, value in self.dict( | ||
exclude_unset=True, exclude_none=True, by_alias=True, exclude={"documentation_only"} | ||
).items(): | ||
cc_items.append(key if isinstance(value, bool) else f"{key}={value}") | ||
|
||
cc_items = [ | ||
key if isinstance(value, bool) else f"{key}={value}" | ||
for key, value in self.dict( | ||
exclude_unset=True, | ||
exclude_none=True, | ||
by_alias=True, | ||
exclude={"documentation_only"}, | ||
).items() | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CacheControlHeader._get_header_value
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
)
if self.weak: | ||
return f"W/{value}" | ||
return value | ||
return f"W/{value}" if self.weak else value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ETag._get_header_value
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
resolved_response_headers.update( | ||
{name: ResponseHeader(name=name, value=value) for name, value in layer_response_headers.items()} | ||
) | ||
resolved_response_headers |= { | ||
name: ResponseHeader(name=name, value=value) | ||
for name, value in layer_response_headers.items() | ||
} | ||
else: | ||
resolved_response_headers.update({h.name: h for h in layer_response_headers}) | ||
for extra_header in ("cache_control", "etag"): | ||
header_model: Header | None = getattr(layer, extra_header, None) | ||
if header_model: | ||
if header_model := getattr(layer, extra_header, None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function HTTPRouteHandler.resolve_response_headers
refactored with the following changes:
- Merge dictionary updates via the union operator (
dict-assign-update-to-union
) - Use named expression to simplify assignment and conditional (
use-named-expression
)
if exclude_path_pattern and exclude_path_pattern.findall( | ||
scope["path"] if not getattr(scope.get("route_handler", {}), "is_mount", False) else scope["raw_path"].decode() | ||
): | ||
return True | ||
return False | ||
return bool( | ||
exclude_path_pattern | ||
and exclude_path_pattern.findall( | ||
scope["path"] | ||
if not getattr(scope.get("route_handler", {}), "is_mount", False) | ||
else scope["raw_path"].decode() | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function should_bypass_middleware
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
)
redirect_domains: set[str] = { | ||
host.replace("www.", "") for host in config.allowed_hosts if host.startswith("www.") | ||
} | ||
if redirect_domains: | ||
if redirect_domains := { | ||
host.replace("www.", "") | ||
for host in config.allowed_hosts | ||
if host.startswith("www.") | ||
}: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function AllowedHostsMiddleware.__init__
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
host = headers.get("host", headers.get("x-forwarded-host", "")).split(":")[0] | ||
|
||
if host: | ||
if host := headers.get("host", headers.get("x-forwarded-host", "")).split( | ||
":" | ||
)[0]: | ||
if self.allowed_hosts_regex.fullmatch(host): | ||
await self.app(scope, receive, send) | ||
return | ||
|
||
if self.redirect_domains is not None and self.redirect_domains.fullmatch(host): | ||
url = URL.from_scope(scope) | ||
redirect_url = url.with_replacements(netloc="www." + url.netloc) | ||
redirect_url = url.with_replacements(netloc=f"www.{url.netloc}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function AllowedHostsMiddleware.__call__
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
origin = headers.get("origin") | ||
|
||
if not origin: | ||
await self.app(scope, receive, send) | ||
else: | ||
if origin := headers.get("origin"): | ||
await self.app(scope, receive, self.send_wrapper(send=send, origin=origin, has_cookie="cookie" in headers)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CORSMiddleware.__call__
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Swap if/else branches (
swap-if-else-branches
)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
main
branch, then run: