diff --git a/CHANGELOG.md b/CHANGELOG.md index c8085c362..f271a3c98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/isort/core.py b/isort/core.py index 4b24e45cd..4d0a46eb3 100644 --- a/isort/core.py +++ b/isort/core.py @@ -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 diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 449f83d53..1853893ea 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -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: + ... +''' + )