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

🔥 Fix import added too early (Sourcery refactored) #18

Closed
wants to merge 3 commits into from

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Dec 3, 2021

Pull Request #17 refactored by Sourcery.

Since the original Pull Request was opened as a fork in a contributor's
repository, we are unable to create a Pull Request branching from it.

To incorporate these changes, you can either:

  1. Merge this Pull Request instead of the original, or

  2. Ask your contributor to locally incorporate these commits and push them to
    the original Pull Request

    Incorporate changes via command line
    git fetch https://github.com/Luttik/auto-optional pull/17/head
    git merge --ff-only FETCH_HEAD
    git push

NOTE: As code is pushed to the original Pull Request, Sourcery will
re-run and update (force-push) this Pull Request with new refactorings as
necessary. If Sourcery finds no refactorings at any point, this Pull Request
will be closed automatically.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Help us improve this pull request!

In case of module docstring or __future__ import,
the new import should be added after.
@codecov-commenter
Copy link

codecov-commenter commented Dec 3, 2021

Codecov Report

Merging #18 (9421fcc) into main (af7358c) will increase coverage by 1.08%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #18      +/-   ##
==========================================
+ Coverage   94.91%   96.00%   +1.08%     
==========================================
  Files           5        5              
  Lines         118      125       +7     
  Branches       23       26       +3     
==========================================
+ Hits          112      120       +8     
  Misses          1        1              
+ Partials        5        4       -1     
Impacted Files Coverage Δ
src/auto_optional/code_processing.py 95.77% <100.00%> (+2.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update af7358c...9421fcc. Read the comment docs.

Comment on lines -102 to +152
if self.optional_import_checker.import_names and self.optional_import_needed:
import_statement = cst.SimpleStatementLine(
body=[
cst.ImportFrom(
module=cst.Name(
value="typing",
),
names=[
cst.ImportAlias(
name=cst.Name(
value="Optional",
),
if (
not self.optional_import_checker.import_names
or not self.optional_import_needed
):
return updated_node
import_statement = cst.SimpleStatementLine(
body=[
cst.ImportFrom(
module=cst.Name(
value="typing",
),
names=[
cst.ImportAlias(
name=cst.Name(
value="Optional",
),
],
),
],
)
]
)
insert_at = 0
for index, body_part in enumerate(updated_node.body):
if (
isinstance(body_part, cst.SimpleStatementLine)
and len(body_part.children) > 0
and (
( # module level docstring
index == 0
and m.matches(
body_part.children[0], m.Expr(m.SimpleString())
)
)
]
)
return updated_node.with_changes(
body=[import_statement, *updated_node.body]
)
return updated_node
or m.matches( # future import
body_part.children[0],
m.ImportFrom(module=m.Name("__future__")),
)
)
):
# module docstring
insert_at += 1
continue
if index > 1:
break
return updated_node.with_changes(
body=[
*updated_node.body[:insert_at],
import_statement,
*updated_node.body[insert_at:],
]
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ImportStatementChecker.check_import_from.AutoOptionalTransformer.leave_Module refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Dec 3, 2021

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 3.53%.

Quality metrics Before After Change
Complexity 5.28 ⭐ 6.50 ⭐ 1.22 👎
Method Length 38.15 ⭐ 44.08 ⭐ 5.93 👎
Working memory 8.83 🙂 8.80 🙂 -0.03 👍
Quality 75.06% 71.53% 🙂 -3.53% 👎
Other metrics Before After Change
Lines 175 205 30
Changed files Quality Before Quality After Quality Change
src/auto_optional/code_processing.py 75.06% ⭐ 71.53% 🙂 -3.53% 👎

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

File Function Complexity Length Working Memory Quality Recommendation
src/auto_optional/code_processing.py ImportStatementChecker.check_import_from.AutoOptionalTransformer.leave_Module.leave_Param 10 🙂 103 🙂 14 😞 52.27% 🙂 Extract out complex expressions
src/auto_optional/code_processing.py ImportStatementChecker.check_import_from.AutoOptionalTransformer.leave_Module 10 🙂 130 😞 10 😞 55.46% 🙂 Try splitting into smaller methods. Extract out complex expressions
src/auto_optional/code_processing.py ImportStatementChecker.check_import_from 16 🙂 61 ⭐ 12 😞 56.24% 🙂 Extract out complex expressions
src/auto_optional/code_processing.py ImportStatementChecker.check_import_from.AutoOptionalTransformer.leave_Module.__check_if__union_none 2 ⭐ 54 ⭐ 13 😞 68.65% 🙂 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!

@sourcery-ai sourcery-ai bot closed this Dec 3, 2021
@sourcery-ai sourcery-ai bot deleted the sourcery/pull-17 branch December 3, 2021 18:06
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.

2 participants