-
-
Notifications
You must be signed in to change notification settings - Fork 612
Add separate_packages option #2313
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
ec2b340
5c749de
250fec7
157edb2
ecd1dba
0dee7d5
3397539
7808f3f
efb710a
1fd0105
425b825
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,13 @@ | |
| from . import parse, sorting, wrap | ||
| from .comments import add_to_line as with_comments | ||
| from .identify import STATEMENT_DECLARATIONS | ||
| from .place import module_with_reason | ||
| from .settings import DEFAULT_CONFIG, Config | ||
|
|
||
|
|
||
| # Ignore DeepSource cyclomatic complexity check for this function. It was | ||
|
||
| # already complex when this check was enabled. | ||
| # skipcq: PY-R1000 | ||
| def sorted_imports( | ||
| parsed: parse.ParsedContent, | ||
| config: Config = DEFAULT_CONFIG, | ||
|
|
@@ -149,6 +153,38 @@ def sorted_imports( | |
| section_output.append("") # Empty line for black compatibility | ||
| section_output.append(section_comment_end) | ||
|
|
||
| if section in config.separate_packages: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks a lot for this @alex-liang3. I know the complexy of this method was already high, but this pirce of code you added here is a perfect candidate for an extract method. With that we could start the needed refactor here to reduce the complexity. Could you please do this refactor? |
||
| group_keys: Set[str] = set() | ||
| comments_above: List[str] = [] | ||
| processed_section_output: List[str] = [] | ||
| for section_line in section_output: | ||
| if section_line.startswith("#"): | ||
| comments_above.append(section_line) | ||
| continue | ||
|
|
||
| package_name: str = section_line.split(" ")[1] | ||
| _, reason = module_with_reason(package_name, config) | ||
|
|
||
| if "Matched configured known pattern" in reason: | ||
| package_depth = len(reason.split(".")) - 1 # minus 1 for re.compile | ||
| key = ".".join(package_name.split(".")[: package_depth + 1]) | ||
| else: | ||
| key = package_name.split(".")[0] | ||
|
|
||
| if key not in group_keys: | ||
| if group_keys: | ||
| processed_section_output.append("") | ||
|
|
||
| group_keys.add(key) | ||
|
|
||
| if comments_above: | ||
| processed_section_output.extend(comments_above) | ||
| comments_above = [] | ||
|
|
||
| processed_section_output.append(section_line) | ||
|
|
||
| section_output = processed_section_output | ||
|
|
||
| if pending_lines_before or not no_lines_before: | ||
| output += [""] * config.lines_between_sections | ||
|
|
||
|
|
||
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.
Would you mind adding an example here to make it more clear?