Flake8 Polyfill

1.0.2 · maintenance · verified Sat Apr 11

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

Install

Imports

Quickstart

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.

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))

view raw JSON →