How to add custom Flake8 rules?

Viewed 1208

Is there any way to add custom rules to flake8?

There are bunch of rules here https://lintlyci.github.io/Flake8Rules/ but I can't find the rules' source code in flake8's git repo.

I want to write a custom rule.

1 Answers

You need to write your own Flake8 plugin.

From the Flake8's docs:

Flake8 is useful on its own but a lot of Flake8’s popularity is due to its extensibility. Our community has developed plugins that augment Flake8’s behaviour. Most of these plugins are uploaded to PyPI. The developers of these plugins often have some style they wish to enforce.

You can't exactly add custom rules to Flake8 itself, apart from submitting a feature-request and hoping it gets accepted. But it allows you to publish a plugin that can be installed in addition to Flake8, and Flake8 can then find it and use it along with its own built-in checks.

They have nice developer documentation on how to write one: Writing Plugins for Flake8. If it's a custom rule that you want checked, then it's probably a Check Plugin. It receives the same info about the code as its built-in checkers does, then you need to write your own code for checking the lines.

You can also check out the source codes of existing Flake8 plugins for inspiration, such as:

Related