Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Jan 10, 2023

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:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from xinetzone January 10, 2023 06:28
version_info = sys.version_info[0:3]
version_info = sys.version_info[:3]
Copy link
Author

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:

return isinstance(obj, _GenericAlias) or isinstance(obj, GenericAlias)
return isinstance(obj, (_GenericAlias, GenericAlias))
Copy link
Author

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:

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]
Copy link
Author

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:

This removes the following comments ( why? ):

# typing.Annotated requires a special case.
# type: ignore

res = list()
res = []
Copy link
Author

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)

Comment on lines -387 to -390
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
Copy link
Author

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:

if is_generator:
return handler(typ)
else:
return handler
return handler(typ) if is_generator else handler
Copy link
Author

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:

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)}"
Copy link
Author

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:

fn_name = "unstructure_" + cl_name
fn_name = f"unstructure_{cl_name}"
Copy link
Author

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:

Comment on lines -235 to +232
if not mapping:
return old_mapping

return mapping
return mapping or old_mapping
Copy link
Author

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:

Comment on lines -252 to +246
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:
Copy link
Author

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:

Comment on lines -292 to +286
fn_name = "structure_" + cl_name
fn_name = f"structure_{cl_name}"
Copy link
Author

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:

Comment on lines -570 to +565
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]
Copy link
Author

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:

Comment on lines -603 to -605
lines = []
lines = [f"def {fn_name}(tup):"]

lines.append(f"def {fn_name}(tup):")
Copy link
Author

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:

Comment on lines -648 to +629
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)
Copy link
Author

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:

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
Copy link
Author

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:

[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]
Copy link
Author

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:

Comment on lines -591 to +589
type=AbcSequence[int] if not legacy_types_only else Sequence[int],
type=Sequence[int] if legacy_types_only else AbcSequence[int],
Copy link
Author

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:

type=AbcMutableSequence[float]
if not legacy_types_only
else MutableSequence[float],
type=MutableSequence[float]
if legacy_types_only
else AbcMutableSequence[float],
Copy link
Author

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:

[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, ...]]
Copy link
Author

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:

yield c if not has_underscore else "_" + c
yield f"_{c}" if has_underscore else c
Copy link
Author

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:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Jan 10, 2023

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.51%.

Quality metrics Before After Change
Complexity 20.79 😞 19.96 😞 -0.83 👍
Method Length 75.30 🙂 74.89 🙂 -0.41 👍
Working memory 10.25 😞 10.23 😞 -0.02 👍
Quality 58.18% 🙂 58.69% 🙂 0.51% 👍
Other metrics Before After Change
Lines 4598 4532 -66
Changed files Quality Before Quality After Quality Change
src/cattrs/_compat.py 87.02% ⭐ 86.88% ⭐ -0.14% 👎
src/cattrs/converters.py 57.44% 🙂 58.46% 🙂 1.02% 👍
src/cattrs/disambiguators.py 29.60% 😞 29.82% 😞 0.22% 👍
src/cattrs/dispatch.py 85.99% ⭐ 86.38% ⭐ 0.39% 👍
src/cattrs/errors.py 89.38% ⭐ 89.42% ⭐ 0.04% 👍
src/cattrs/gen.py 19.54% ⛔ 19.05% ⛔ -0.49% 👎
src/cattrs/preconf/tomlkit.py 69.13% 🙂 71.22% 🙂 2.09% 👍
tests/_compat.py 97.69% ⭐ 97.04% ⭐ -0.65% 👎
tests/test_converter.py 69.59% 🙂 69.77% 🙂 0.18% 👍
tests/test_gen_dict.py 61.60% 🙂 63.78% 🙂 2.18% 👍
tests/test_structure.py 75.51% ⭐ 75.79% ⭐ 0.28% 👍
tests/typed.py 64.67% 🙂 64.71% 🙂 0.04% 👍
tests/untyped.py 77.58% ⭐ 77.58% ⭐ 0.00%

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
src/cattrs/gen.py make_dict_structure_fn 143 ⛔ 1190 ⛔ 32 ⛔ 0.65% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
src/cattrs/gen.py make_dict_unstructure_fn 59 ⛔ 560 ⛔ 24 ⛔ 4.39% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
src/cattrs/converters.py BaseConverter._structure_tuple 48 ⛔ 366 ⛔ 18 ⛔ 10.93% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
src/cattrs/gen.py make_mapping_structure_fn 39 ⛔ 376 ⛔ 17 ⛔ 13.94% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
src/cattrs/converters.py Converter.__init__ 23 😞 425 ⛔ 14 😞 24.22% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants