-
Notifications
You must be signed in to change notification settings - Fork 0
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 refactored master branch #1
base: master
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.
if parts[-1] == '__init__': | ||
suffix = parts[-2] | ||
else: | ||
suffix = parts[-1] | ||
|
||
suffix = parts[-2] if parts[-1] == '__init__' else parts[-1] |
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_export_symbols
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
if not any(arg in sys.argv for arg in ['clean', 'check']) and 'SKIP_CYTHON' not in os.environ: | ||
if ( | ||
all(arg not in sys.argv for arg in ['clean', 'check']) | ||
and 'SKIP_CYTHON' not in os.environ | ||
): |
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 75-75
refactored with the following changes:
- Invert any/all to simplify comparisons (
invert-any-all
)
MAX_LINE_LENGTH = int(re.search(r'max_line_length = (\d+)', (EXAMPLES_DIR / '.editorconfig').read_text()).group(1)) | ||
MAX_LINE_LENGTH = int( | ||
re.search( | ||
r'max_line_length = (\d+)', | ||
(EXAMPLES_DIR / '.editorconfig').read_text(), | ||
)[1] | ||
) | ||
|
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 23-23
refactored with the following changes:
- Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups
)
m = re.search(r'^( *)print\(', lines[line_no - back]) | ||
if m: | ||
indent = m.group(1) | ||
if m := re.search(r'^( *)print\(', lines[line_no - back]): | ||
indent = m[1] |
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 build_print_statement
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups
)
file_contents = [] | ||
for f in DOCS_DIR.glob('**/*.md'): | ||
file_contents.append(f.read_text()) | ||
file_contents = [f.read_text() for f in DOCS_DIR.glob('**/*.md')] |
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 all_md_contents
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
)
if isinstance(v, str): | ||
return v.split('|') | ||
return v | ||
return v.split('|') if isinstance(v, str) else v |
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 DemoModel.split_str
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
)
assert each_item is False, '"each_item" and "whole" conflict, remove "whole"' | ||
assert not each_item, '"each_item" and "whole" conflict, remove "whole"' |
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 validator
refactored with the following changes:
- Simplify comparison to boolean (
simplify-boolean-comparison
)
ref = f_cls.__func__.__module__ + '.' + f_cls.__func__.__qualname__ | ||
ref = f'{f_cls.__func__.__module__}.{f_cls.__func__.__qualname__}' |
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 _prepare_validator
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
if validators: | ||
return {v.func.__name__: v for v in validators} | ||
else: | ||
return None | ||
return {v.func.__name__: v for v in validators} if validators else 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 ValidatorGroup.get_validators
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
unused_validators = set( | ||
if unused_validators := set( | ||
chain.from_iterable( | ||
(v.func.__name__ for v in self.validators[f] if v.check_fields) | ||
for f in (self.validators.keys() - self.used_validators) | ||
) | ||
) | ||
if unused_validators: | ||
): |
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 ValidatorGroup.check_for_unused
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
validator_config = getattr(value, VALIDATOR_CONFIG_KEY, None) | ||
if validator_config: | ||
if validator_config := getattr(value, VALIDATOR_CONFIG_KEY, 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 extract_validators
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
elif args == set(): | ||
elif not args: |
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 _generic_validator_cls
refactored with the following changes:
- Replaces an empty collection equality with a boolean operation (
simplify-empty-collection-comparison
)
elif args == set(): | ||
elif not args: |
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 _generic_validator_basic
refactored with the following changes:
- Replaces an empty collection equality with a boolean operation (
simplify-empty-collection-comparison
)
if self._rgba.alpha is None: | ||
rgb = cast(Tuple[int, int, int], self.as_rgb_tuple()) | ||
try: | ||
return COLORS_BY_VALUE[rgb] | ||
except KeyError as e: | ||
if fallback: | ||
return self.as_hex() | ||
else: | ||
raise ValueError('no named color found, use fallback=True, as_hex() or as_rgb()') from e | ||
else: | ||
if self._rgba.alpha is not None: | ||
return self.as_hex() | ||
rgb = cast(Tuple[int, int, int], self.as_rgb_tuple()) | ||
try: | ||
return COLORS_BY_VALUE[rgb] | ||
except KeyError as e: | ||
if fallback: | ||
return self.as_hex() | ||
else: | ||
raise ValueError('no named color found, use fallback=True, as_hex() or as_rgb()') from e |
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 Color.as_named
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
return '#' + as_hex | ||
return f'#{as_hex}' |
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 Color.as_hex
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
def __call__(self, settings: BaseSettings) -> Dict[str, Any]: # noqa C901 | ||
def __call__(self, settings: BaseSettings) -> Dict[str, Any]: # noqa C901 |
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 EnvSettingsSource.__call__
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
This removes the following comments ( why? ):
# field is complex but no value found so far, try explode_env_vars
if not case_sensitive: | ||
return {k.lower(): v for k, v in file_vars.items()} | ||
else: | ||
return file_vars | ||
return ( | ||
file_vars | ||
if case_sensitive | ||
else {k.lower(): v for k, v in file_vars.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 read_env_file
refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression
) - Replace if statement with if expression (
assign-if-exp
)
if isinstance(self._loc, tuple): | ||
return self._loc | ||
else: | ||
return (self._loc,) | ||
return self._loc if isinstance(self._loc, tuple) else (self._loc, ) |
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 ErrorWrapper.loc_tuple
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
ctx = error.get('ctx') | ||
if ctx: | ||
if ctx := error.get('ctx'): |
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 _display_error_type_and_ctx
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
if loc: | ||
error_loc = loc + error.loc_tuple() | ||
else: | ||
error_loc = error.loc_tuple() | ||
|
||
error_loc = loc + error.loc_tuple() if loc else error.loc_tuple() |
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 flatten_errors
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
if msg_template: | ||
msg = msg_template.format(**ctx) | ||
else: | ||
msg = str(exc) | ||
|
||
msg = msg_template.format(**ctx) if msg_template else str(exc) |
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 error_dict
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
return base_name + '.' + code | ||
return f'{base_name}.{code}' |
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_exc_type
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
# Create self validators | ||
get_validators = getattr(self.type_, '__get_validators__', None) | ||
if get_validators: | ||
if get_validators := getattr(self.type_, '__get_validators__', 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 ModelField._type_analysis
refactored with the following changes:
- Use named expression to simplify assignment and conditional [×3] (
use-named-expression
) - Use f-string instead of string concatenation [×5] (
use-fstring-for-concatenation
)
This removes the following comments ( why? ):
# Equality check as almost everything inherits form Iterable, including str
# check for Iterable and CollectionsIterable, as it could receive one even when declared with the other
# Create self validators
# priority to most common mapping: dict
if errors: | ||
return v, errors | ||
else: | ||
return tuple(result), None | ||
return (v, errors) if errors else (tuple(result), 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 ModelField._validate_tuple
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
if original_cls == dict or original_cls == Dict: | ||
if original_cls in [dict, Dict]: |
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 ModelField._get_mapping_value
refactored with the following changes:
- Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
if isinstance(obj, GetterDict): | ||
return obj | ||
return cls.__config__.getter_dict(obj) | ||
return obj if isinstance(obj, GetterDict) else cls.__config__.getter_dict(obj) |
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 BaseModel._decompose_class
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 to_dict: | ||
v_dict = v.dict( | ||
by_alias=by_alias, | ||
exclude_unset=exclude_unset, | ||
exclude_defaults=exclude_defaults, | ||
include=include, | ||
exclude=exclude, | ||
exclude_none=exclude_none, | ||
) | ||
if ROOT_KEY in v_dict: | ||
return v_dict[ROOT_KEY] | ||
return v_dict | ||
else: | ||
if not to_dict: | ||
return v.copy(include=include, exclude=exclude) | ||
|
||
v_dict = v.dict( | ||
by_alias=by_alias, | ||
exclude_unset=exclude_unset, | ||
exclude_defaults=exclude_defaults, | ||
include=include, | ||
exclude=exclude, | ||
exclude_none=exclude_none, | ||
) | ||
return v_dict[ROOT_KEY] if ROOT_KEY in v_dict else v_dict |
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 BaseModel._get_value
refactored with the following changes:
- Swap positions of nested conditionals [×2] (
swap-nested-ifs
) - Hoist nested repeated code outside conditional statements [×2] (
hoist-similar-statement-from-if
) - Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
if include is None and exclude is None and exclude_unset is False: | ||
if include is None and exclude is None and not exclude_unset: | ||
return None | ||
|
||
keys: AbstractSet[str] | ||
if exclude_unset: | ||
keys = self.__fields_set__.copy() | ||
else: | ||
keys = self.__dict__.keys() | ||
|
||
keys = self.__fields_set__.copy() if exclude_unset else self.__dict__.keys() |
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 BaseModel._calculate_keys
refactored with the following changes:
- Simplify comparison to boolean (
simplify-boolean-comparison
) - Replace if statement with if expression (
assign-if-exp
)
if sym and isinstance(sym.node, TypeInfo): # pragma: no branch | ||
# No branching may occur if the mypy cache has not been cleared | ||
if any(get_fullname(base) == BASEMODEL_FULLNAME for base in sym.node.mro): | ||
return self._pydantic_model_class_maker_callback | ||
if ( | ||
sym | ||
and isinstance(sym.node, TypeInfo) | ||
and any( | ||
get_fullname(base) == BASEMODEL_FULLNAME for base in sym.node.mro | ||
) | ||
): | ||
return self._pydantic_model_class_maker_callback |
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 PydanticPlugin.get_base_class_hook
refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs
)
This removes the following comments ( why? ):
# No branching may occur if the mypy cache has not been cleared
# pragma: no branch
if fullname.endswith('.from_orm'): | ||
return from_orm_callback | ||
return None | ||
return from_orm_callback if fullname.endswith('.from_orm') else 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 PydanticPlugin.get_method_hook
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
)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.15%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
Branch
master
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
master
branch, then run:Help us improve this pull request!