{"id":4007,"library":"flake8-polyfill","title":"Flake8 Polyfill","description":"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.","status":"maintenance","version":"1.0.2","language":"en","source_language":"en","source_url":"https://github.com/PyCQA/flake8-polyfill","tags":["flake8","plugin","polyfill","compatibility","linter"],"install":[{"cmd":"pip install flake8-polyfill","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides compatibility helpers for Flake8 plugins.","package":"flake8","optional":false}],"imports":[{"note":"Directly using parser.add_option() with Flake8 3.x specific arguments like `parse_from_config` will break on Flake8 2.x. Use `options.register` for cross-version compatibility.","wrong":"parser.add_option(..., parse_from_config=True)","symbol":"options","correct":"from flake8_polyfill import options"},{"note":"Flake8 2.6 changed from `pep8` to `pycodestyle` for stdin handling, and Flake8 3.0 removed monkey-patching altogether, making direct `pep8` or `pycodestyle` imports inconsistent. Use `stdin.monkey_patch()` for reliable cross-version stdin access.","wrong":"import pep8; stdin_value = pep8.get_stdin_value()","symbol":"stdin","correct":"from flake8_polyfill import stdin"},{"note":"Flake8 2.x provides version as a string, while 3.x introduced `__version_info__` as a tuple for easier comparison. `flake8_polyfill.version.version_info` normalizes this for consistent version checks.","wrong":"import flake8; flake8.__version__","symbol":"version","correct":"from flake8_polyfill import version"}],"quickstart":{"code":"from flake8_polyfill import options, stdin, version\n\nclass MyFlake8Plugin:\n    name = 'my_plugin'\n    version = '1.0.0'\n\n    @classmethod\n    def add_options(cls, parser):\n        options.register(\n            parser,\n            '--my-custom-option',\n            default='default_value',\n            parse_from_config=True,\n            help='A custom option for my plugin.',\n        )\n\n    @classmethod\n    def parse_options(cls, values):\n        cls.my_custom_option = values.my_custom_option\n\n    def __init__(self, tree, filename):\n        self.tree = tree\n        self.filename = filename\n        # Example of stdin usage (though not typically in __init__)\n        # For actual use, call stdin.monkey_patch() once at plugin load.\n        # stdin.monkey_patch('all')\n        # self.stdin_value = stdin.get_stdin_value()\n\n    def run(self):\n        # Example of version comparison\n        if (3, 0) <= version.version_info < (4, 0):\n            # Logic specific to Flake8 3.x\n            pass\n        elif version.version_info < (3, 0):\n            # Logic specific to Flake8 2.x\n            pass\n\n        # Your linting logic here\n        yield (1, 0, 'MYP001 This is a custom check.', type(self))","lang":"python","description":"This quickstart demonstrates how to use `flake8-polyfill` to register options, handle Flake8 version comparisons, and hints at standard input handling within a custom Flake8 plugin, ensuring compatibility across Flake8 2.x and 3.x. The option registration uses `options.register` to handle differences in `add_option` arguments. Version checks use `version.version_info` for consistent tuple-based comparison. Standard input is shown commented out as it's typically set up globally for a plugin."},"warnings":[{"fix":"Use `from flake8_polyfill import options` and then `options.register(parser, ...)` instead of `parser.add_option(...)`. The `options.register` function handles the compatibility layer, making your code work on both versions.","message":"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.","severity":"breaking","affected_versions":"Flake8 2.x when trying to use 3.x option features directly."},{"fix":"Use `from flake8_polyfill import stdin` and call `stdin.monkey_patch('all')`, `stdin.monkey_patch('pep8')`, or `stdin.monkey_patch('pycodestyle')` early in your plugin's lifecycle. Then, use `stdin.get_stdin_value()` to retrieve the content reliably across versions.","message":"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.","severity":"breaking","affected_versions":"Flake8 2.5, 2.6, and 3.x when handling standard input."},{"fix":"Import `version` from `flake8_polyfill` and use `version.version_info` for all version comparisons. This attribute provides a consistent tuple format `(major, minor, patch)` regardless of the underlying Flake8 version.","message":"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.","severity":"gotcha","affected_versions":"Flake8 2.x and 3.x when performing version comparisons."},{"fix":"For new plugins targeting recent Flake8 versions (e.g., 4.x, 5.x, 6.x, 7.x), directly consult the official Flake8 plugin developer guide for the most current best practices and APIs. `flake8-polyfill` is specifically designed for cross-compatibility between Flake8 2.x and 3.x.","message":"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.","severity":"deprecated","affected_versions":"All versions of `flake8-polyfill`."}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}