PyCQA / flake8-bugbear

A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.
MIT License
1.06k stars 104 forks source link

Add flake8 to pre-commit checks, with local bugbear as a plug-in #425

Closed r-downing closed 10 months ago

r-downing commented 10 months ago

For https://github.com/PyCQA/flake8-bugbear/issues/412

Considered doing this as a repo local-hook but it would require somehow hardcoding the venv location.

Implemented using pre-commit instance of flake8, and passing some additional config (and attr dependency) so that it can use the live bugbear.py from the repo.

This had to be split out to a separate config, because if it was in the base one, it would break local venv usage of flake8 because there would be duplicate bugbear plugins.

cooperlees commented 10 months ago

O, my bad, this is for external users and not for running on CI runs? I'm super lost ... (can you tell I don't use pre-commit)

I tried to re-run the issue and PR, but still don't get what this solves / can be used for ... so if that could be explained that would be great too ... sorry, but no GitHub / precommit power user dur to working at a $bigCompany

r-downing commented 10 months ago

O, my bad, this is for external users and not for running on CI runs? I'm super lost ... (can you tell I don't use pre-commit)

Hey, sorry if I didn't explain this very well! Yeah the goal is to add a flake8 check for pre-commit. Generally for pre-commit, you specify tool repos and revs (linters and checkers, etc) in .pre-commit-confg.yaml (that's the flake8 repo and rev 6.1.0), and then pre-commit installs self-contained versions of those tools in a cache directory to run during commit, etc.

So I was trying to add that flake8 check for this repo on pre-commit.

The tricky part is, that since it's a self-contained flake8 installation - how do we also run checks defined in our local plugin? We can specify flake8-bugbear as an additional dependency in the pre-commit-config, but then it's just pulled from pip, not the local one we're editing. Another option is pre-commit allows you to specify local hooks, but the drawback here is that it would be tied to the specific venv and installation location.

So the approach I came up with was to specify for pre-commit to use its own installation of flake8, and and pass in an arg for that additional config file so that it would pick up the bugbear plugin py file from within the repo. It's a little hacky, and I'm still troubleshooting it myself - seems to hang indefinitely on my PC if I run on all files. Started a draft because I kinda wanted to see if it worked in CI. Seemed to get picked up properly in the pre-commit CI at least.

cooperlees commented 10 months ago

So, is your goal to just run flake8-bugbear via precommit? We do this with a project:

https://github.com/pypa/bandersnatch/blob/main/.pre-commit-config.yaml#L40

Isn't that what this does? What's the missing example with this approach I'm still not following?

r-downing commented 10 months ago

The goal was to run flake8-bugbear (the live one) on this project itself, so that new rules would apply in real-time

r-downing commented 10 months ago

Something wonky about this config, probably more trouble than it's worth. Adding the basic checks here instead https://github.com/PyCQA/flake8-bugbear/pull/427