Skip to content

Commit

Permalink
Keep whitespace before imports
Browse files Browse the repository at this point in the history
This commits comes from

  * asottile#370
  * psf/black#4175 (comment)

because

  * asottile#367
  * asottile#366
  * psf/black#4175

Suggested-by: Achim Herwig <achim.herwig@wodca.de>
  • Loading branch information
miguelinux committed Apr 14, 2024
1 parent 1ab1be5 commit 77dfaf3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions reorder_python_imports.py
Expand Up @@ -94,10 +94,10 @@ def partition_source(src: str) -> tuple[str, list[str], str, str]:
pre_import = False
chunks.append((CodeType.IMPORT, s))
elif token_type is Tok.NEWLINE:
if s.isspace():
tp = CodeType.NON_CODE
elif pre_import:
if pre_import:
tp = CodeType.PRE_IMPORT_CODE
elif s.isspace():
tp = CodeType.NON_CODE
else:
tp = CodeType.CODE

Expand Down
12 changes: 8 additions & 4 deletions tests/reorder_python_imports_test.py
Expand Up @@ -47,7 +47,7 @@ def test_tokenize_can_match_strings(s):
@pytest.mark.parametrize(
's',
(
pytest.param('', id='trivial'),
pytest.param('\n', id='trivial'),
pytest.param('#!/usr/bin/env python\n', id='shebang'),
pytest.param('# -*- coding: UTF-8 -*-\n', id='source encoding'),
pytest.param(' # coding: UTF-8\n', id='source encoding indented'),
Expand Down Expand Up @@ -190,17 +190,21 @@ def test_partition_source_imports_only(s, expected):
assert nl == '\n'


def test_partition_source_before_removes_newlines():
def test_partition_source_before_leaves_newlines():
before, imports, after, nl = partition_source(
'# comment here\n'
'\n'
'# another comment here\n',
'# another comment here\n'
'\n'
'import os\n'
)
assert before == (
'# comment here\n'
'\n'
'# another comment here\n'
'\n'
)
assert imports == []
assert imports == ['import os\n']
assert after == ''
assert nl == '\n'

Expand Down

0 comments on commit 77dfaf3

Please sign in to comment.