Flake8 Polyfill
Flake8-polyfill is a compatibility layer for Flake8 plugins, designed to ease the transition and support for plugins working across Flake8 versions 2.x and 3.x. It primarily addresses differences in option registration and standard input handling. The current version is 1.0.2, last released in December 2017, suggesting an inactive maintenance cadence.
Warnings
- breaking Flake8 3.0 changed the `add_option` method, introducing new parameters like `parse_from_config`, `comma_separated_list`, and `normalize_paths` that are not present in Flake8 2.x. Directly using these in `parser.add_option()` will cause `TypeError` on older Flake8 versions.
- breaking Between Flake8 2.5 and 3.0, the way standard input (stdin) is retrieved by plugins changed significantly. Flake8 2.5 monkey-patched `pep8`, 2.6 switched to `pycodestyle`, and 3.0 no longer monkey-patches either, requiring plugins to manage stdin retrieval themselves or use Flake8's internal mechanisms.
- gotcha Flake8 2.x represented its version as a string (`flake8.__version__`), while Flake8 3.x introduced `flake8.__version_info__` as a tuple for structured version comparison. Direct string comparisons can be error-prone or impossible for complex version logic.
- deprecated The `flake8-polyfill` library itself appears to be in an inactive maintenance state, with the last release in late 2017. While it addresses critical compatibility issues between older Flake8 versions (2.x and 3.x), its continued relevance might diminish as Flake8 evolves and older versions become less common.
Install
-
pip install flake8-polyfill
Imports
- options
from flake8_polyfill import options
- stdin
from flake8_polyfill import stdin
- version
from flake8_polyfill import version
Quickstart
from flake8_polyfill import options, stdin, version
class MyFlake8Plugin:
name = 'my_plugin'
version = '1.0.0'
@classmethod
def add_options(cls, parser):
options.register(
parser,
'--my-custom-option',
default='default_value',
parse_from_config=True,
help='A custom option for my plugin.',
)
@classmethod
def parse_options(cls, values):
cls.my_custom_option = values.my_custom_option
def __init__(self, tree, filename):
self.tree = tree
self.filename = filename
# Example of stdin usage (though not typically in __init__)
# For actual use, call stdin.monkey_patch() once at plugin load.
# stdin.monkey_patch('all')
# self.stdin_value = stdin.get_stdin_value()
def run(self):
# Example of version comparison
if (3, 0) <= version.version_info < (4, 0):
# Logic specific to Flake8 3.x
pass
elif version.version_info < (3, 0):
# Logic specific to Flake8 2.x
pass
# Your linting logic here
yield (1, 0, 'MYP001 This is a custom check.', type(self))