-
-
Notifications
You must be signed in to change notification settings - Fork 615
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 all commits
ec2b340
5c749de
250fec7
157edb2
ecd1dba
0dee7d5
3397539
7808f3f
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,6 +8,7 @@ | |
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 | ||
|
||
|
||
|
@@ -149,6 +150,9 @@ def sorted_imports( | |
section_output.append("") # Empty line for black compatibility | ||
section_output.append(section_comment_end) | ||
|
||
if section in config.separate_packages: | ||
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? |
||
section_output = _separate_packages(section_output, config) | ||
|
||
if pending_lines_before or not no_lines_before: | ||
output += [""] * config.lines_between_sections | ||
|
||
|
@@ -674,3 +678,37 @@ def _with_star_comments(parsed: parse.ParsedContent, module: str, comments: List | |
if star_comment: | ||
return [*comments, star_comment] | ||
return comments | ||
|
||
|
||
def _separate_packages(section_output: List[str], config: Config) -> List[str]: | ||
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) | ||
|
||
return processed_section_output |
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?