Skip to content

Commit 41bf429

Browse files
committed
chore: run autoformatters (pyupgrade, isort, black)
1 parent 4c57928 commit 41bf429

File tree

3 files changed

+118
-132
lines changed

3 files changed

+118
-132
lines changed

flake8_isort.py

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,20 @@
1-
# -*- coding: utf-8 -*-
2-
1+
import warnings
32
from contextlib import redirect_stdout
4-
from difflib import Differ
5-
from difflib import unified_diff
3+
from difflib import Differ, unified_diff
64
from io import StringIO
75
from pathlib import Path
86

97
import isort
10-
import warnings
118

129

13-
class Flake8IsortBase(object):
10+
class Flake8IsortBase:
1411
name = 'flake8_isort'
1512
version = '4.2.1'
16-
isort_unsorted = (
17-
'I001 isort found an import in the wrong position'
18-
)
19-
no_config_msg = (
20-
'I002 no configuration found (.isort.cfg or [isort] in configs)'
21-
)
22-
isort_blank_req = (
23-
'I003 isort expected 1 blank line in imports, found 0'
24-
)
25-
isort_blank_unexp = (
26-
'I004 isort found an unexpected blank line in imports'
27-
)
28-
isort_add_unexp = (
29-
'I005 isort found an unexpected missing import'
30-
)
13+
isort_unsorted = 'I001 isort found an import in the wrong position'
14+
no_config_msg = 'I002 no configuration found (.isort.cfg or [isort] in configs)'
15+
isort_blank_req = 'I003 isort expected 1 blank line in imports, found 0'
16+
isort_blank_unexp = 'I004 isort found an unexpected blank line in imports'
17+
isort_add_unexp = 'I005 isort found an unexpected missing import'
3118

3219
show_traceback = False
3320
stdin_display_name = None
@@ -43,7 +30,7 @@ def add_options(cls, parser):
4330
'--isort-show-traceback',
4431
action='store_true',
4532
parse_from_config=True,
46-
help='Show full traceback with diff from isort'
33+
help='Show full traceback with diff from isort',
4734
)
4835

4936
@classmethod
@@ -98,9 +85,7 @@ def sortimports_linenum_msg(self, sort_result):
9885
diff = differ.compare(sort_result.in_lines, sort_result.out_lines)
9986

10087
line_num = 0
101-
additions = {
102-
'+ {}'.format(add_import) for add_import in sort_result.add_imports
103-
}
88+
additions = {f'+ {add_import}' for add_import in sort_result.add_imports}
10489
for line in diff:
10590
if line.startswith(' ', 0, 2):
10691
line_num += 1 # Ignore unchanged lines but increment line_num.
@@ -173,7 +158,8 @@ def _fixup_sortimports_wrapped(sort_imports):
173158
for idx, line in enumerate(sort_imports.out_lines):
174159
if '\n' in line:
175160
for new_idx, new_line in enumerate(
176-
sort_imports.out_lines.pop(idx).splitlines()):
161+
sort_imports.out_lines.pop(idx).splitlines()
162+
):
177163
sort_imports.out_lines.insert(idx + new_idx, new_line)
178164

179165

@@ -183,12 +169,10 @@ class Flake8Isort5(Flake8IsortBase):
183169
def run(self):
184170
if self.filename is not self.stdin_display_name:
185171
file_path = Path(self.filename)
186-
isort_config = isort.settings.Config(
187-
settings_path=file_path.parent)
172+
isort_config = isort.settings.Config(settings_path=file_path.parent)
188173
else:
189174
file_path = None
190-
isort_config = isort.settings.Config(
191-
settings_path=Path.cwd())
175+
isort_config = isort.settings.Config(settings_path=Path.cwd())
192176
input_string = ''.join(self.lines)
193177
traceback = ''
194178
isort_changed = False
@@ -201,19 +185,23 @@ def run(self):
201185
input_stream=input_stream,
202186
output_stream=output_stream,
203187
config=isort_config,
204-
file_path=file_path)
188+
file_path=file_path,
189+
)
205190
except isort.exceptions.FileSkipped:
206191
pass
207192
except isort.exceptions.ISortError as e:
208193
warnings.warn(e)
209194
if isort_changed:
210195
outlines = output_stream.getvalue()
211-
diff_delta = "".join(unified_diff(
212-
input_string.splitlines(keepends=True),
213-
outlines.splitlines(keepends=True),
214-
fromfile="{}:before".format(self.filename),
215-
tofile="{}:after".format(self.filename)))
216-
traceback = (isort_stdout.getvalue() + "\n" + diff_delta)
196+
diff_delta = "".join(
197+
unified_diff(
198+
input_string.splitlines(keepends=True),
199+
outlines.splitlines(keepends=True),
200+
fromfile=f"{self.filename}:before",
201+
tofile=f"{self.filename}:after",
202+
)
203+
)
204+
traceback = isort_stdout.getvalue() + "\n" + diff_delta
217205
for line_num, message in self.isort_linenum_msg(diff_delta):
218206
if self.show_traceback:
219207
message += traceback

run_tests.py

Lines changed: 87 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -52,86 +52,90 @@ def check_isort_ret(ret, ref):
5252

5353

5454
testcases = [
55-
{'name': 'sorted_correctly_default',
56-
'code': ('import os\n'
57-
'from sys import path\n'),
58-
'ref': []},
59-
{'name': 'sorted_correctly_alpha',
60-
'config': 'force_single_line=True\n'
61-
'force_alphabetical_sort=True\n',
62-
'code': 'from sys import path\n'
63-
'\n'
64-
'import os\n',
65-
'ref': []},
66-
{'name': 'eof_blank_lines',
67-
'code': 'import os\n'
68-
'from sys import path\n'
69-
'\n'
70-
'\n'
71-
' \n',
72-
'ref': []},
73-
{'name': 'imports_requires_blank_line',
74-
'code': 'from __future__ import division\n'
75-
'import threading\n'
76-
'from sys import pid\n',
77-
'ref': [(2, 0, 'I003 ')]},
78-
{'name': 'isortcfg_skip_file',
79-
'config': 'skip=test.py',
80-
'code': 'skipped_file',
81-
'ref': []},
82-
{'name': 'file_skipped_with_comment',
83-
'code': '# isort:skip_file',
84-
'ref': []},
85-
{'name': 'imports_unexpected_blank_line',
86-
'code': 'from __future__ import division\n'
87-
'\n'
88-
'import threading\n'
89-
'\n'
90-
'from sys import pid\n',
91-
'ref': [(4, 0, 'I004 ')]},
92-
{'name': 'sorted_incorrectly_multiple',
93-
'code': 'from __future__ import division\n'
94-
'import os\n'
95-
'from sys import pid\n'
96-
'import threading\n'
97-
'\n'
98-
'import isort\n'
99-
'\n\n\n'
100-
'def func()\n',
101-
'ref': [(2, 0, 'I003 '),
102-
(4, 0, 'I001 '),
103-
(9, 0, 'I004 ')]},
104-
{'name': 'sorted_incorrectly',
105-
'config': 'force_single_line=True',
106-
'code': 'from sys import pid\n'
107-
'import threading',
108-
'ref': [(2, 0, 'I001 ')]},
109-
{'name': 'empty_file',
110-
'code': '\n\n',
111-
'ref': []},
112-
{'name': 'wrapped_imports',
113-
'config': 'wrap_length=65',
114-
'code': 'from deluge.common import (fdate, fpcnt, fpeer, fsize, fspeed,\n'
115-
' ftime, get_path_size, is_infohash,\n'
116-
' is_ip, is_magnet, is_url)\n',
117-
'ref': []},
118-
{'name': 'force_single_line_imports',
119-
'config': 'force_alphabetical_sort=True\n'
120-
'force_single_line=True',
121-
'code': 'from plone.app.testing import applyProfile\n'
122-
'from plone.app.testing import FunctionalTesting\n',
123-
'ref': []},
124-
{'name': 'missing_add_imports',
125-
'config': 'add_imports=from __future__ import unicode_literals',
126-
'code': 'import os\n',
127-
'ref': [(1, 0, 'I003'),
128-
(1, 0, 'I005')]},
55+
{
56+
'name': 'sorted_correctly_default',
57+
'code': ('import os\n' 'from sys import path\n'),
58+
'ref': [],
59+
},
60+
{
61+
'name': 'sorted_correctly_alpha',
62+
'config': 'force_single_line=True\n' 'force_alphabetical_sort=True\n',
63+
'code': 'from sys import path\n' '\n' 'import os\n',
64+
'ref': [],
65+
},
66+
{
67+
'name': 'eof_blank_lines',
68+
'code': 'import os\n' 'from sys import path\n' '\n' '\n' ' \n',
69+
'ref': [],
70+
},
71+
{
72+
'name': 'imports_requires_blank_line',
73+
'code': 'from __future__ import division\n'
74+
'import threading\n'
75+
'from sys import pid\n',
76+
'ref': [(2, 0, 'I003 ')],
77+
},
78+
{
79+
'name': 'isortcfg_skip_file',
80+
'config': 'skip=test.py',
81+
'code': 'skipped_file',
82+
'ref': [],
83+
},
84+
{'name': 'file_skipped_with_comment', 'code': '# isort:skip_file', 'ref': []},
85+
{
86+
'name': 'imports_unexpected_blank_line',
87+
'code': 'from __future__ import division\n'
88+
'\n'
89+
'import threading\n'
90+
'\n'
91+
'from sys import pid\n',
92+
'ref': [(4, 0, 'I004 ')],
93+
},
94+
{
95+
'name': 'sorted_incorrectly_multiple',
96+
'code': 'from __future__ import division\n'
97+
'import os\n'
98+
'from sys import pid\n'
99+
'import threading\n'
100+
'\n'
101+
'import isort\n'
102+
'\n\n\n'
103+
'def func()\n',
104+
'ref': [(2, 0, 'I003 '), (4, 0, 'I001 '), (9, 0, 'I004 ')],
105+
},
106+
{
107+
'name': 'sorted_incorrectly',
108+
'config': 'force_single_line=True',
109+
'code': 'from sys import pid\n' 'import threading',
110+
'ref': [(2, 0, 'I001 ')],
111+
},
112+
{'name': 'empty_file', 'code': '\n\n', 'ref': []},
113+
{
114+
'name': 'wrapped_imports',
115+
'config': 'wrap_length=65',
116+
'code': 'from deluge.common import (fdate, fpcnt, fpeer, fsize, fspeed,\n'
117+
' ftime, get_path_size, is_infohash,\n'
118+
' is_ip, is_magnet, is_url)\n',
119+
'ref': [],
120+
},
121+
{
122+
'name': 'force_single_line_imports',
123+
'config': 'force_alphabetical_sort=True\n' 'force_single_line=True',
124+
'code': 'from plone.app.testing import applyProfile\n'
125+
'from plone.app.testing import FunctionalTesting\n',
126+
'ref': [],
127+
},
128+
{
129+
'name': 'missing_add_imports',
130+
'config': 'add_imports=from __future__ import unicode_literals',
131+
'code': 'import os\n',
132+
'ref': [(1, 0, 'I003'), (1, 0, 'I005')],
133+
},
129134
]
130135

131136

132137
@pytest.mark.parametrize('mode', ["file", "code_string"])
133-
@pytest.mark.parametrize('testcase', testcases,
134-
ids=[t['name'] for t in testcases])
138+
@pytest.mark.parametrize('testcase', testcases, ids=[t['name'] for t in testcases])
135139
def test_flake8_isort(tmpdir, testcase, mode):
136140
"""Test the code examples in files and directly from string"""
137141
with tmpdir.as_cwd():
@@ -151,8 +155,7 @@ def test_flake8_isort(tmpdir, testcase, mode):
151155
def test_isortcfg_found(tmpdir):
152156
(file_path, lines) = write_python_file(
153157
tmpdir,
154-
'from sys import pid\n'
155-
'import threading',
158+
'from sys import pid\n' 'import threading',
156159
)
157160
write_isort_cfg(tmpdir, 'force_single_line=True')
158161
checker = Flake8Isort(None, file_path, lines)
@@ -162,10 +165,7 @@ def test_isortcfg_found(tmpdir):
162165

163166

164167
def test_isortcfg_not_found(tmpdir):
165-
(file_path, lines) = write_python_file(
166-
tmpdir,
167-
'from sys import pid, path'
168-
)
168+
(file_path, lines) = write_python_file(tmpdir, 'from sys import pid, path')
169169
checker = Flake8Isort(None, file_path, lines)
170170
checker.search_current = False
171171
checker.config_file = True
@@ -175,18 +175,12 @@ def test_isortcfg_not_found(tmpdir):
175175

176176
def test_isort_formatted_output(tmpdir):
177177
options = collections.namedtuple(
178-
'Options', [
179-
'no_isort_config',
180-
'isort_show_traceback',
181-
'stdin_display_name'
182-
]
178+
'Options', ['no_isort_config', 'isort_show_traceback', 'stdin_display_name']
183179
)
184180

185181
(file_path, lines) = write_python_file(
186182
tmpdir,
187-
'from __future__ import division\n'
188-
'import os\n'
189-
'from sys import pid\n',
183+
'from __future__ import division\n' 'import os\n' 'from sys import pid\n',
190184
)
191185

192186
diff = ' from __future__ import division\n+\n import os'
@@ -202,12 +196,12 @@ def test_isort_formatted_output(tmpdir):
202196

203197
@pytest.mark.parametrize(
204198
'method_to_write_config',
205-
[write_isort_cfg, write_setup_cfg, write_tox_ini, write_pyproject_toml])
199+
[write_isort_cfg, write_setup_cfg, write_tox_ini, write_pyproject_toml],
200+
)
206201
def test_if_config_file_is_used(tmpdir, method_to_write_config):
207202
(file_path, lines) = write_python_file(
208203
tmpdir,
209-
'import os\n'
210-
'from sys import path\n',
204+
'import os\n' 'from sys import path\n',
211205
)
212206
method_to_write_config(tmpdir, 'lines_between_types=1')
213207

setup.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ def read_file(filename):
4646
author_email='[email protected]',
4747
url='https://github.com/gforcada/flake8-isort',
4848
license='GPL version 2',
49-
py_modules=['flake8_isort', ],
49+
py_modules=[
50+
'flake8_isort',
51+
],
5052
include_package_data=True,
5153
test_suite='run_tests',
5254
zip_safe=False,
@@ -60,6 +62,8 @@ def read_file(filename):
6062
],
6163
},
6264
entry_points={
63-
'flake8.extension': ['I00 = flake8_isort:Flake8Isort', ],
65+
'flake8.extension': [
66+
'I00 = flake8_isort:Flake8Isort',
67+
],
6468
},
6569
)

0 commit comments

Comments
 (0)