Browse Source

tools/hooks/run-flake8.py: use flake8-import-order plugin

But it's pep8 rule keeps application-imports in the same group as
relative-imports. Changing it is not a matter of configuration but defining
our own style, which is tedious as it would require a setuptools entrypoint
just for that, so we inject manually the style.
Vincent Ardisson 6 months ago
parent
commit
9c714c916c
3 changed files with 34 additions and 7 deletions
  1. 1
    0
      .ci/requirements.txt
  2. 32
    7
      tools/hooks/run-flake8.py
  3. 1
    0
      tools/pyflakes-strict.sh

+ 1
- 0
.ci/requirements.txt View File

@@ -10,3 +10,4 @@ Sphinx==3.0.3
10 10
 xunitparser==1.3.3
11 11
 asttokens
12 12
 virtualenv
13
+flake8-import-order

+ 32
- 7
tools/hooks/run-flake8.py View File

@@ -2,12 +2,39 @@
2 2
 
3 3
 from pathlib import Path
4 4
 import runpy
5
-import subprocess
6 5
 import sys
6
+from types import ModuleType
7
+
8
+from flake8.main.cli import main
9
+from flake8_import_order.styles import PEP8
10
+import pkg_resources
7 11
 
8 12
 
9 13
 mod = runpy.run_path(str(Path(__file__).with_name('checkerlib.py')))
10 14
 
15
+args = mod['parser'].parse_args()
16
+
17
+
18
+# flake8-import-order's PEP8 style merges app imports and relative imports
19
+class AllSeparateStyle(PEP8):
20
+    @staticmethod
21
+    def same_section(previous, current):
22
+        return current.type == previous.type
23
+
24
+
25
+# flake8-import-order is almost only configurable through entry_points
26
+# ugly hack to avoid having to create a separate package with entry_point, setup, etc.
27
+style_mod = ModuleType('fake_module_all_separate')
28
+style_mod.AllSeparateStyle = AllSeparateStyle
29
+sys.modules['fake_module_all_separate'] = style_mod
30
+
31
+d = pkg_resources.Distribution('/lol.py')
32
+ep = pkg_resources.EntryPoint.parse('fake_module_all_separate = fake_module_all_separate:AllSeparateStyle')
33
+ep.dist = d
34
+d._ep_map = {'flake8_import_order.styles': {'fake_module_all_separate': ep}}
35
+pkg_resources.working_set.add(d, 'fake_module_all_separate')
36
+
37
+
11 38
 # E501: Line too long
12 39
 #   Disabled because it doesn't allow exceptions, for example URLs or log
13 40
 #   messages shouldn't be split, less readable or searchable.
@@ -15,11 +42,9 @@ mod = runpy.run_path(str(Path(__file__).with_name('checkerlib.py')))
15 42
 #   Disabling it follows pep8 (see W504).
16 43
 # E266: Too many leading '#' for block comment
17 44
 #   But it's a nice visual separator sometimes.
18
-
19
-args = mod['parser'].parse_args()
20
-
21
-execution = subprocess.run([
22
-    'flake8', '--ignore=E501,W503,E266',
45
+main([
46
+    '--ignore=E501,W503,E266',
47
+    '--application-import-names=weboob',
48
+    '--import-order-style=fake_module_all_separate',
23 49
     *map(str, mod['files_to_check'](args)),
24 50
 ])
25
-sys.exit(execution.returncode)

+ 1
- 0
tools/pyflakes-strict.sh View File

@@ -10,6 +10,7 @@ die () {
10 10
 }
11 11
 
12 12
 $PYTHON3 -c 'import flake8' || die "Please install flake8 (e.g. apt install flake8)"
13
+$PYTHON3 -c 'import flake8_import_order' || die "Please install flake8-import-order (e.g. pip3 install flake8-import-order)"
13 14
 $PYTHON3 -c 'import bugbear' || die "Please install flake8-bugbear (e.g. pip3 install flake8-bugbear)"
14 15
 $PYTHON3 -c 'import asttokens' || die "Please install asttokens (e.g. apt install python3-asttokens)"
15 16