-
Notifications
You must be signed in to change notification settings - Fork 3
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
Conversation
In case of module docstring or __future__ import, the new import should be added after.
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
89a7dc2
to
9421fcc
Compare
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:], | ||
] | ||
) |
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 ImportStatementChecker.check_import_from.AutoOptionalTransformer.leave_Module
refactored with the following changes:
- Add guard clause (
last-if-guard
)
Sourcery Code Quality Report❌ Merging this PR will decrease code quality in the affected files by 3.53%.
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! |
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:
Merge this Pull Request instead of the original, or
Ask your contributor to locally incorporate these commits and push them to
the original Pull Request
Incorporate changes via command line
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!