Skip to content

Commit 65c1a59

Browse files
authored
Merge pull request #133 from gforcada/drop-isort-4
Drop isort 4
2 parents 849b27e + eb16f5d commit 65c1a59

File tree

5 files changed

+9
-146
lines changed

5 files changed

+9
-146
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,15 @@ jobs:
1313
strategy:
1414
matrix:
1515
python-version: ["3.11", "3.10", 3.9, 3.8, 3.7, pypy-3.9]
16-
isort: [5.10.1]
16+
isort: [5.11.2]
1717
flake8: [5.0.4]
1818
include:
1919
- python-version: 3.9
20-
isort: 5.10.1
20+
isort: 5.11.2
2121
flake8: 4.0.1
2222
qa: 'true'
2323
- python-version: 3.9
24-
isort: 5.10.1
25-
flake8: 3.9.2
26-
27-
- python-version: 3.9
28-
isort: 4.3.21
29-
flake8: 5.0.4
30-
- python-version: 3.9
31-
isort: 4.3.21
32-
flake8: 4.0.1
33-
- python-version: 3.9
34-
isort: 4.3.21
24+
isort: 5.11.2
3525
flake8: 3.9.2
3626
steps:
3727
- uses: actions/checkout@v3

CHANGES.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Changelog
66
5.0.4 (unreleased)
77
------------------
88

9-
- Nothing changed yet.
10-
9+
- Drop isort 4.x support.
10+
[gforcada]
1111

1212
5.0.3 (2022-11-20)
1313
------------------

flake8_isort.py

Lines changed: 2 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import warnings
22
from contextlib import redirect_stdout
3-
from difflib import Differ, unified_diff
3+
from difflib import unified_diff
44
from io import StringIO
55
from pathlib import Path
66

@@ -47,130 +47,6 @@ def parse_options(cls, option_manager, options, args):
4747
cls.show_traceback = options.isort_show_traceback
4848

4949

50-
class Flake8Isort4(Flake8IsortBase):
51-
"""class for isort <5"""
52-
53-
def run(self):
54-
if self.filename is not self.stdin_display_name:
55-
file_path = self.filename
56-
else:
57-
file_path = None
58-
buffer = StringIO()
59-
with redirect_stdout(buffer):
60-
sort_result = isort.SortImports(
61-
file_path=file_path,
62-
file_contents=''.join(self.lines),
63-
check=True,
64-
show_diff=True,
65-
)
66-
traceback = self._format_isort_output(buffer)
67-
68-
for line_num, message in self.sortimports_linenum_msg(sort_result):
69-
if self.show_traceback:
70-
message += traceback
71-
yield line_num, 0, message, type(self)
72-
73-
def sortimports_linenum_msg(self, sort_result):
74-
"""Parses isort.SortImports for line number changes and message
75-
76-
Uses a diff.Differ comparison of SortImport `in_lines`:`out_lines` to
77-
yield the line numbers of import lines that have been moved or blank
78-
lines added.
79-
80-
Args:
81-
sort_imports (isort.SortImports): The isorts results object.
82-
83-
Yields:
84-
tuple: A tuple of the specific isort line number and message.
85-
"""
86-
if sort_result.skipped:
87-
return
88-
89-
self._fixup_sortimports_wrapped(sort_result)
90-
self._fixup_sortimports_eof(sort_result)
91-
92-
differ = Differ()
93-
diff = differ.compare(sort_result.in_lines, sort_result.out_lines)
94-
95-
line_num = 0
96-
additions = {f'+ {add_import}' for add_import in sort_result.add_imports}
97-
for line in diff:
98-
if line.startswith(' ', 0, 2):
99-
line_num += 1 # Ignore unchanged lines but increment line_num.
100-
elif line.startswith('- ', 0, 2):
101-
line_num += 1
102-
if line.strip() == '-':
103-
yield line_num, self.isort_blank_unexp
104-
else:
105-
yield line_num, self.isort_unsorted
106-
elif line.strip() == '+':
107-
# Include newline additions but do not increment line_num.
108-
yield line_num + 1, self.isort_blank_req
109-
elif line.strip() in additions:
110-
yield line_num + 1, self.isort_add_unexp
111-
112-
def _format_isort_output(self, isort_buffer):
113-
filtering_out = ('+++', '---', '@@', 'ERROR:')
114-
115-
valid_lines = ['']
116-
valid_lines += [
117-
line
118-
for line in isort_buffer.getvalue().splitlines()
119-
if line.strip().split(' ', 1)[0] not in filtering_out
120-
]
121-
122-
# Normalizing newlines:
123-
if len(valid_lines) > 1:
124-
valid_lines.insert(1, '')
125-
valid_lines.append('')
126-
127-
return '\n'.join(valid_lines)
128-
129-
@staticmethod
130-
def _fixup_sortimports_eof(sort_imports):
131-
"""Ensure single end-of-file newline in `isort.SortImports.in_lines`
132-
133-
isort fixes EOF blank lines but this change should be suppressed as
134-
Flake8 will also flag them.
135-
136-
Args:
137-
sort_imports (isort.SortImports): The isorts results object.
138-
139-
Returns:
140-
isort.SortImports: The modified isort results object.
141-
"""
142-
to_remove = {''} | set(sort_imports.add_imports)
143-
for line in reversed(sort_imports.in_lines):
144-
if line.strip() in to_remove:
145-
# If single empty line in in_lines, do nothing.
146-
if len(sort_imports.in_lines) > 1:
147-
sort_imports.in_lines.pop()
148-
else:
149-
sort_imports.in_lines.append('')
150-
break
151-
152-
@staticmethod
153-
def _fixup_sortimports_wrapped(sort_imports):
154-
"""Split-up wrapped imports newlines in `SortImports.out_lines`
155-
156-
isort combines wrapped lines into a single list entry string in
157-
`out_lines` whereas `in_lines` are separate strings so for diff
158-
comparison these need to be comparable.
159-
160-
Args:
161-
sort_imports (isort.SortImports): The isorts results object.
162-
163-
Returns:
164-
isort.SortImports: The modified isort results object.
165-
"""
166-
for idx, line in enumerate(sort_imports.out_lines):
167-
if '\n' in line:
168-
for new_idx, new_line in enumerate(
169-
sort_imports.out_lines.pop(idx).splitlines()
170-
):
171-
sort_imports.out_lines.insert(idx + new_idx, new_line)
172-
173-
17450
class Flake8Isort5(Flake8IsortBase):
17551
"""class for isort >=5"""
17652

@@ -258,4 +134,4 @@ def isort_linenum_msg(self, udiff):
258134
yield line_num, self.isort_add_unexp
259135

260136

261-
Flake8Isort = Flake8Isort5 if hasattr(isort, 'api') else Flake8Isort4
137+
Flake8Isort = Flake8Isort5

run_tests.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
"""unit tests for flake8-isort
2-
3-
the test should pass with both isort 4 and isort 5
4-
"""
1+
"""unit tests for flake8-isort"""
52

63
import collections
74
import os

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def read_file(filename):
5555
zip_safe=False,
5656
install_requires=[
5757
'flake8',
58-
'isort >= 4.3.5, <6',
58+
'isort >= 5.0.0, <6',
5959
],
6060
extras_require={
6161
'test': [

0 commit comments

Comments
 (0)