The package provides base classes and utils for flake8 plugin writing
Project description
flake8-plugin-utils
The package provides base classes and utils for flake8 plugin writing.
Installation
pip install flake8-plugin-utils
Example
Write simple plugin
from flake8_plugin_utils import Error, Visitor, Plugin
class MyError(Error):
code = 'X100'
message = 'my error'
class MyVisitor(Visitor):
def visit_ClassDef(self, node):
self.error_from_node(MyError, node)
class MyPlugin(Plugin):
name = 'MyPlugin'
version = '0.1.0'
visitors = [MyVisitor]
and test it with pytest
from flake8_plugin_utils import assert_error, assert_not_error
def test_code_with_error():
assert_error(MyVisitor, 'class Y: pass', MyError)
def test_code_without_error():
assert_not_error(MyVisitor, 'x = 1')
Configuration
To add configuration to a plugin, do the following:
- Implement classmethod
add_options
in your plugin class, as per the flake8 docs. - Override classmethod
parse_options_to_config
in your plugin class to return any object holding the options you need. - If you need a custom
__init__
for your visitor, make sure it accepts a keyword argument namedconfig
and pass it tosuper().__init__
- Use
self.config
in visitor code.
Example:
from flake8_plugin_utils import Error, Visitor, Plugin
class MyError(Error):
code = 'X100'
message = 'my error with {thing}'
class MyConfig:
def __init__(self, config_option):
self.config_option = config_option
class MyVisitorWithConfig(Visitor):
def visit_ClassDef(self, node):
self.error_from_node(
MyError, node, thing=f'{node.name} {self.config.config_option}'
)
class MyPluginWithConfig(Plugin):
name = 'MyPluginWithConfig'
version = '0.0.1'
visitors = [MyVisitorWithConfig]
@classmethod
def add_options(cls, options_manager):
options_manager.add_option('--config_option', ...)
@classmethod
def parse_options_to_config(cls, option_manager, options, args):
return MyConfig(config_option=options.config_option)
Formatting
Your Error
s can take formatting arguments in their message
:
from flake8_plugin_utils import Error, Visitor, assert_error
class MyFormattedError(Error):
code = 'X101'
message = 'my error with {thing}'
class MyFormattedVisitor(Visitor):
def visit_ClassDef(self, node):
self.error_from_node(MyFormattedError, node, thing=node.name)
def test_code_with_error():
assert_error(
MyFormattedVisitor,
'class Y: pass',
MyFormattedError,
thing='Y',
)
Usage with typing/mypy
The Plugin
and Visitor
classes are generic with the config class as type
parameter. If your plugin does not have any config, inherit it from
Plugin[None]
and the visitors from Visitor[None]
. Otherwise, use the
config class as the type parameter (e.g. Plugin[MyConfig]
and
Visitor[MyConfig]
in the above example).
License
MIT
Change Log
Unreleased
- ...
1.1.1 - 2020-03-02
- ignore encoding errors when reading strings for noqa validation
1.1.0 - 2020-03-01
- add ability for plugins to parse and use configuration
NB: this change breaks type-checking if you use typing/mypy. Change your
code to inherit from
Plugin[None]
andVisitor[None]
to fix.
1.0.0 - 2019-05-23
- add message formatting to Error
0.2.1 - 2019-04-01
- don`t strip before src dedent in _error_from_src
- add is_none, is_true, is_false util functions
0.2.0 - 2019.02.21
- add assert methods
0.1.0 - 2019.02.09
- initial
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for flake8-plugin-utils-1.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15664d4c4a6c1d02279f6ffcac8999d76f5538d14c7f0d487fab3f681c56cd69 |
|
MD5 | a073a5311df915da9c0c9f2d31cceb0e |
|
BLAKE2b-256 | 82d4d4d9ed61dc8ba52346a9924a57781ea86dde5430e0c10f68ed542eb5cfa5 |
Close
Hashes for flake8_plugin_utils-1.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 563caf4b8c505af8f930c25f3c7028b5ec458daa8c239bb3a27aaa636d050df6 |
|
MD5 | d6714f72d258b1d96a4e00027f3187b6 |
|
BLAKE2b-256 | b8749947856b5859bd1cd2da0588c93f575223b7171d5257249ae20621813dcb |