Skip to content

Commit

Permalink
Merge pull request #1793 from PyCQA/issue/1792-fix-literal-sort-at-top
Browse files Browse the repository at this point in the history
Fixed #1792: Sorting literals sometimes ignored when placed on first …
  • Loading branch information
timothycrosley authored Jul 28, 2021
2 parents 8ae8d50 + 915aa68 commit e625f8a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
- Made all exceptions pickleable.
- Fixed #1779: Pylama integration ignores pylama specific isort config overrides.
- Fixed #1781: `--from-first` CLI flag shouldn't take any arguments.
- Fixed #1792: Sorting literals sometimes ignored when placed on first few lines of file.

### 5.9.2 July 8th 2021
- Improved behavior of `isort --check --atomic` against Cython files.
Expand Down
5 changes: 4 additions & 1 deletion isort/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,13 @@ def process(
(index == 0 or (index in (1, 2) and not contains_imports))
and stripped_line.startswith("#")
and stripped_line not in config.section_comments
and stripped_line not in CODE_SORT_COMMENTS
):
in_top_comment = True
elif in_top_comment and (
not line.startswith("#") or stripped_line in config.section_comments
not line.startswith("#")
or stripped_line in config.section_comments
or stripped_line in CODE_SORT_COMMENTS
):
in_top_comment = False
first_comment_index_end = index - 1
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1828,3 +1828,44 @@ def test_isort_should_only_add_imports_to_valid_location_issue_1769():
v=""""""
'''
)


def test_literal_sort_at_top_of_file_issue_1792():
assert (
isort.code(
'''"""I'm a docstring! Look at me!"""
# isort: unique-list
__all__ = ["Foo", "Foo", "Bar"]
from typing import final # arbitrary
@final
class Foo:
...
@final
class Bar:
...
'''
)
== '''"""I'm a docstring! Look at me!"""
# isort: unique-list
__all__ = ['Bar', 'Foo']
from typing import final # arbitrary
@final
class Foo:
...
@final
class Bar:
...
'''
)

0 comments on commit e625f8a

Please sign in to comment.