Skip to content

Commit 915aa68

Browse files
committedJul 28, 2021
Fixed #1792: Sorting literals sometimes ignored when placed on first few lines of file.
1 parent 8ae8d50 commit 915aa68

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
99
- Made all exceptions pickleable.
1010
- Fixed #1779: Pylama integration ignores pylama specific isort config overrides.
1111
- Fixed #1781: `--from-first` CLI flag shouldn't take any arguments.
12+
- Fixed #1792: Sorting literals sometimes ignored when placed on first few lines of file.
1213

1314
### 5.9.2 July 8th 2021
1415
- Improved behavior of `isort --check --atomic` against Cython files.

‎isort/core.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,13 @@ def process(
176176
(index == 0 or (index in (1, 2) and not contains_imports))
177177
and stripped_line.startswith("#")
178178
and stripped_line not in config.section_comments
179+
and stripped_line not in CODE_SORT_COMMENTS
179180
):
180181
in_top_comment = True
181182
elif in_top_comment and (
182-
not line.startswith("#") or stripped_line in config.section_comments
183+
not line.startswith("#")
184+
or stripped_line in config.section_comments
185+
or stripped_line in CODE_SORT_COMMENTS
183186
):
184187
in_top_comment = False
185188
first_comment_index_end = index - 1

‎tests/unit/test_regressions.py

+41
Original file line numberDiff line numberDiff line change
@@ -1828,3 +1828,44 @@ def test_isort_should_only_add_imports_to_valid_location_issue_1769():
18281828
v=""""""
18291829
'''
18301830
)
1831+
1832+
1833+
def test_literal_sort_at_top_of_file_issue_1792():
1834+
assert (
1835+
isort.code(
1836+
'''"""I'm a docstring! Look at me!"""
1837+
1838+
# isort: unique-list
1839+
__all__ = ["Foo", "Foo", "Bar"]
1840+
1841+
from typing import final # arbitrary
1842+
1843+
1844+
@final
1845+
class Foo:
1846+
...
1847+
1848+
1849+
@final
1850+
class Bar:
1851+
...
1852+
'''
1853+
)
1854+
== '''"""I'm a docstring! Look at me!"""
1855+
1856+
# isort: unique-list
1857+
__all__ = ['Bar', 'Foo']
1858+
1859+
from typing import final # arbitrary
1860+
1861+
1862+
@final
1863+
class Foo:
1864+
...
1865+
1866+
1867+
@final
1868+
class Bar:
1869+
...
1870+
'''
1871+
)

0 commit comments

Comments
 (0)
Please sign in to comment.