-
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 main branch #1
base: main
Are you sure you want to change the base?
Conversation
version_info = sys.version_info[0:3] | ||
version_info = sys.version_info[:3] |
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 a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
)
return isinstance(obj, _GenericAlias) or isinstance(obj, GenericAlias) | ||
return isinstance(obj, (_GenericAlias, GenericAlias)) |
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 is_generic
refactored with the following changes:
- Merge isinstance calls (
merge-isinstance
)
if is_annotated(type): | ||
# typing.Annotated requires a special case. | ||
return Annotated[args] # type: ignore | ||
return type.__origin__[args] | ||
return Annotated[args] if is_annotated(type) else type.__origin__[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 copy_with
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
)
This removes the following comments ( why? ):
# typing.Annotated requires a special case.
# type: ignore
res = list() | ||
res = [] |
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 BaseConverter.unstructure_attrs_astuple
refactored with the following changes:
- Replace
list()
with[]
(list-literal
)
fn = make_dict_structure_fn( | ||
cl, self, _cattrs_prefer_attrib_converters=self._prefer_attrib_converters | ||
return make_dict_structure_fn( | ||
cl, | ||
self, | ||
_cattrs_prefer_attrib_converters=self._prefer_attrib_converters, | ||
) | ||
return fn |
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 BaseConverter._gen_structure_generic
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if is_generator: | ||
return handler(typ) | ||
else: | ||
return handler | ||
return handler(typ) if is_generator else handler |
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 FunctionDispatch.dispatch
refactored with the following changes:
- Swap positions of nested conditionals (
swap-nested-ifs
) - Hoist nested repeated code outside conditional statements (
hoist-similar-statement-from-if
) - Replace if statement with if expression (
assign-if-exp
)
if message | ||
else f"Extra fields in constructor for {cl.__name__}: {', '.join(extra_fields)}" | ||
or f"Extra fields in constructor for {cl.__name__}: {', '.join(extra_fields)}" |
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 ForbiddenExtraKeysError.__init__
refactored with the following changes:
- Simplify if expression by using or (
or-if-exp-identity
)
fn_name = "unstructure_" + cl_name | ||
fn_name = f"unstructure_{cl_name}" |
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 make_dict_unstructure_fn
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Swap if/else branches (
swap-if-else-branches
) - Hoist repeated code outside conditional statement (
hoist-statement-from-if
)
if not mapping: | ||
return old_mapping | ||
|
||
return mapping | ||
return mapping or old_mapping |
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 _generate_mapping
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
) - Swap if/else branches of if expression to remove negation (
swap-if-expression
) - Simplify if expression by using or (
or-if-exp-identity
)
elif a.converter is not None and not prefer_attrs_converters and type is not None: | ||
elif a.converter is not None and type is not 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 find_structure_handler
refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if
)
fn_name = "structure_" + cl_name | ||
fn_name = f"structure_{cl_name}" |
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 make_dict_structure_fn
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Merge consecutive list appends into a single extend [×2] (
merge-list-appends-into-extend
) - Remove redundant conditional [×2] (
remove-redundant-if
)
lines = [] | ||
|
||
lines.append(f"def {fn_name}(iterable):") | ||
lines.append(" res = __cattr_seq_cl(__cattr_u(i) for i in iterable)") | ||
lines = [ | ||
f"def {fn_name}(iterable):", | ||
" res = __cattr_seq_cl(__cattr_u(i) for i in iterable)", | ||
] | ||
|
||
total_lines = lines + [" return res"] | ||
|
||
eval(compile("\n".join(total_lines), "", "exec"), globs) | ||
|
||
fn = globs[fn_name] | ||
|
||
return fn | ||
return globs[fn_name] |
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 make_iterable_unstructure_fn
refactored with the following changes:
- Merge append into list declaration [×2] (
merge-list-append
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
lines = [] | ||
lines = [f"def {fn_name}(tup):"] | ||
|
||
lines.append(f"def {fn_name}(tup):") |
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 make_hetero_tuple_unstructure_fn
refactored with the following changes:
- Merge append into list declaration (
merge-list-append
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if len(args) == 2: | ||
key_arg, val_arg = args | ||
else: | ||
# Probably a Counter | ||
key_arg, val_arg = args, Any | ||
key_arg, val_arg = args if len(args) == 2 else (args, 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 make_mapping_unstructure_fn
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Merge append into list declaration [×2] (
merge-list-append
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
This removes the following comments ( why? ):
# Probably a Counter
if issubclass(args[0], Enum): | ||
key_handler = _enum_value_getter | ||
else: | ||
key_handler = None | ||
key_handler = _enum_value_getter if issubclass(args[0], Enum) 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 configure_converter
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
[list[float], list, List[float], List] | ||
if not legacy_types_only | ||
else [List, List[float], list] | ||
[List, List[float], list] | ||
if legacy_types_only | ||
else [list[float], list, List[float], List] |
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 list_typed_attrs
refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression
)
type=AbcSequence[int] if not legacy_types_only else Sequence[int], | ||
type=Sequence[int] if legacy_types_only else AbcSequence[int], |
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 seq_typed_attrs
refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression
)
type=AbcMutableSequence[float] | ||
if not legacy_types_only | ||
else MutableSequence[float], | ||
type=MutableSequence[float] | ||
if legacy_types_only | ||
else AbcMutableSequence[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 mutable_seq_typed_attrs
refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression
)
[tuple[str, ...], tuple, Tuple, Tuple[str, ...]] | ||
if not legacy_types_only | ||
else [tuple, Tuple, Tuple[str, ...]] | ||
[tuple, Tuple, Tuple[str, ...]] | ||
if legacy_types_only | ||
else [tuple[str, ...], tuple, Tuple, Tuple[str, ...]] |
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 homo_tuple_typed_attrs
refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression
)
yield c if not has_underscore else "_" + c | ||
yield f"_{c}" if has_underscore else c |
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 gen_attr_names
refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.51%.
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
main
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
main
branch, then run:Help us improve this pull request!