Skip to content

Commit 7843b5b

Browse files
committed
Support Flake8 version 5, drop support for earlier versions.
As of version 5.0.0, due to some refactoring, Flake8 locates and parses the config files differently. The old hook therefore no longer works and was replaced with a new one tailored to Flake8 5.0+. Consequently, we no longer support Flake8 4.x and earlier. But the dependency declarations should take care of it, so that an older version, which supported Flake8 4.x, is pip-installed in that case. One test case, `config_mixed`, had to be modified. It seems that Flake8 will no longer merge configurations from multiple config files, such as when `pyproject.toml` and `.flake8` and `tox.ini` are all found in the project folder. We now test that the configuration is read from `pyproject.toml` in such a scenario, provided it defines a `tool.flake8` section.
1 parent 072b444 commit 7843b5b

File tree

6 files changed

+32
-22
lines changed

6 files changed

+32
-22
lines changed

flake8p/hook.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
"""Hooks TOML parser into Flake8."""
22

3+
import flake8.main.cli
4+
import flake8.options.config
35
import tomli
46
from pathlib import Path
5-
from flake8.main.cli import main as flake8_main
6-
from flake8.options import config as flake8_config
77

88

9-
class PyprojectRawConfigParser(flake8_config.configparser.RawConfigParser):
10-
"""Mixes the TOML parser into Flake8's INI parser."""
9+
# Remember original Flake8 objects.
10+
flake8_RawConfigParser = flake8.options.config.configparser.RawConfigParser
11+
flake8_find_config_file = flake8.options.config._find_config_file
12+
13+
14+
# Implement derived objects that are aware of `pyproject.toml`.
15+
16+
class RawConfigParser(flake8_RawConfigParser):
17+
"""Mixes the TOML parser into Flake8's config-file parser."""
1118

1219
def _read(self, stream, path):
1320
file = Path(path)
1421
if file.name == 'pyproject.toml':
1522
with file.open('rb') as f:
1623
settings = tomli.load(f)
17-
if 'tool' not in settings:
18-
return
19-
if 'flake8' not in settings['tool']:
20-
return
2124
if not self.has_section('flake8'):
2225
self.add_section('flake8')
2326
for (key, value) in settings['tool']['flake8'].items():
@@ -28,15 +31,20 @@ def _read(self, stream, path):
2831
super()._read(stream, path)
2932

3033

31-
class PyprojectConfigFileFinder(flake8_config.ConfigFileFinder):
32-
"""Adds `pyproject.toml` to the list of accepted configuration files."""
33-
34-
def __init__(self, *args, **kwargs):
35-
super().__init__(*args, **kwargs)
36-
self.project_filenames = self.project_filenames + ('pyproject.toml',)
34+
def find_config_file(path):
35+
"""Convinces Flake8 to prefer `pyproject.toml` over other config files."""
36+
file = Path(path)/'pyproject.toml'
37+
if file.exists():
38+
with file.open('rb') as f:
39+
settings = tomli.load(f)
40+
if 'tool' in settings and 'flake8' in settings['tool']:
41+
return str(file)
42+
return flake8_find_config_file(path)
3743

3844

39-
flake8_config.configparser.RawConfigParser = PyprojectRawConfigParser
40-
flake8_config.ConfigFileFinder = PyprojectConfigFileFinder
45+
# Monkey-patch Flake8 with our modified objects.
46+
flake8.options.config.configparser.RawConfigParser = RawConfigParser
47+
flake8.options.config._find_config_file = find_config_file
4148

42-
main = flake8_main
49+
# Just call Flake8 when we are called.
50+
main = flake8.main.cli.main

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ readme = 'PyPI.md'
1818
dynamic = ['version', 'description']
1919
requires-python = '>=3.6'
2020
dependencies = [
21-
'Flake8<5',
21+
'Flake8 >= 5, < 6',
2222
'TOMLi',
23-
'TOMLi<2; python_version < "3.7"',
23+
'TOMLi < 2; python_version < "3.7"',
2424
]
2525

2626
[project.scripts]
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
[flake8]
22
per-file-ignores =
3-
module.py:F401

tests/fixtures/config_mixed/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ ignore = [
33
'E231', # Missing whitespace after ',', ';', or ':'.
44
'E241', # Multiple spaces after ','.
55
]
6+
per-file-ignores = ['module.py:F401']
7+
max-line-length = 70
8+
count = true
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[flake8]
2-
max-line-length = 70
2+
max-line-length = 80
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[flake8]
2-
count = true
2+
count = false

0 commit comments

Comments
 (0)