diff options
author | Timothy Edmund Crosley <timothy.crosley@gmail.com> | 2020-01-13 23:00:37 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-13 23:00:37 -0800 |
commit | cfd85329f41cfce2c3b9454b6d57ec0278dc97d0 (patch) | |
tree | 07e7f212730233b686a12b96f2203aa03d273ee6 | |
parent | 500bafabbd51a6005c11a00c4738a2438990e48a (diff) | |
parent | 2a5d7ec54a07595ded44aad3e7c5c70accdd0655 (diff) | |
download | isort-cfd85329f41cfce2c3b9454b6d57ec0278dc97d0.tar.gz |
Merge pull request #1099 from timothycrosley/feature/fix-issue-1091
Feature/fix issue 1091
-rw-r--r-- | isort/output.py | 19 | ||||
-rw-r--r-- | tests/test_isort.py | 12 |
2 files changed, 18 insertions, 13 deletions
diff --git a/isort/output.py b/isort/output.py index 4343f71a..f7a4b2f7 100644 --- a/isort/output.py +++ b/isort/output.py @@ -333,18 +333,18 @@ def _with_from_imports( ) comments = None else: + above_comments = parsed.categorized_comments["above"]["from"].pop(module, None) + if above_comments: + if new_section_output and config.ensure_newline_before_comments: + new_section_output.append("") + new_section_output.extend(above_comments) + while from_imports and from_imports[0] in as_imports: from_import = from_imports.pop(0) as_imports[from_import] = sorting.naturally(as_imports[from_import]) from_comments = parsed.categorized_comments["straight"].get( f"{module}.{from_import}" ) - above_comments = parsed.categorized_comments["above"]["from"].pop(module, None) - if above_comments: - if new_section_output and config.ensure_newline_before_comments: - new_section_output.append("") - new_section_output.extend(above_comments) - if ( config.keep_direct_and_as_imports and parsed.imports[section]["from"][module][from_import] @@ -399,13 +399,6 @@ def _with_from_imports( single_import_line += ( f"{comments and ';' or config.comment_prefix} " f"{comment}" ) - above_comments = parsed.categorized_comments["above"]["from"].pop( - module, None - ) - if above_comments: - if new_section_output and config.ensure_newline_before_comments: - new_section_output.append("") - new_section_output.extend(above_comments) new_section_output.append( wrap.line(single_import_line, parsed.line_separator, config) ) diff --git a/tests/test_isort.py b/tests/test_isort.py index a0f5dfc0..2580fae7 100644 --- a/tests/test_isort.py +++ b/tests/test_isort.py @@ -4771,3 +4771,15 @@ try: import bar """ assert SortImports(file_contents=test_input).output == test_input + + +def test_comments_top_of_file(): + """Test to ensure comments at top of file are correctly handled. See issue #1091.""" + test_input = """# comment 1 + +# comment 2 +# comment 3 +# comment 4 +from foo import * +""" + assert SortImports(file_contents=test_input).output == test_input |