|
1 | 1 | import warnings
|
2 | 2 | from contextlib import redirect_stdout
|
3 |
| -from difflib import Differ, unified_diff |
| 3 | +from difflib import unified_diff |
4 | 4 | from io import StringIO
|
5 | 5 | from pathlib import Path
|
6 | 6 |
|
@@ -47,130 +47,6 @@ def parse_options(cls, option_manager, options, args):
|
47 | 47 | cls.show_traceback = options.isort_show_traceback
|
48 | 48 |
|
49 | 49 |
|
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 |
| - |
174 | 50 | class Flake8Isort5(Flake8IsortBase):
|
175 | 51 | """class for isort >=5"""
|
176 | 52 |
|
@@ -258,4 +134,4 @@ def isort_linenum_msg(self, udiff):
|
258 | 134 | yield line_num, self.isort_add_unexp
|
259 | 135 |
|
260 | 136 |
|
261 |
| -Flake8Isort = Flake8Isort5 if hasattr(isort, 'api') else Flake8Isort4 |
| 137 | +Flake8Isort = Flake8Isort5 |
0 commit comments