chameleon icon indicating copy to clipboard operation
chameleon copied to clipboard

Find better linting tool

Open malthe opened this issue 1 year ago • 7 comments

We should move away from pycodestyle (used by Flake8) which has some inherent design limitations such as not being able to qualify an analysis outside of the current syntax unit, see for example https://github.com/PyCQA/pycodestyle/issues/703.

malthe avatar Jan 17 '24 09:01 malthe

@icemac do you know about an alternative?

malthe avatar Jan 17 '24 09:01 malthe

@malthe Have you had a look at flake8-bugbear and some of the other flake8 plugins out there? I find flake8 with some plugins + static analysis through a type checker is usually sufficient to catch most of the things you'd want to be able to catch.

I found that pylint usually goes too much in the other direction. But you could also check out ruff, which re-implements most of the rules from a variety of popular linters in Rust (as well as with the extended ability to auto-fix some of them), which would let you hand pick from a greater variety of rules without having to install dozens of different tools/plugins.

Can you give some examples of rules you're missing or where the rate of false positive/negatives is too high, besides the bare except case?

Daverball avatar Jan 18 '24 15:01 Daverball

On the note of tooling, have you considered adding pre-commit? I use it pretty much in all my projects now and it's saved me a lot of headaches. It's really easy to forget to run one of the tox environments, such as type checking or linting, because you were focused on fixing a test or vice versa, so pre-commit helps to save you from yourself.

Daverball avatar Jan 18 '24 16:01 Daverball

A rule that I'm missing is that bare except is okay on re-raise. Flake8 can't do that because it can't look ahead!

malthe avatar Jan 18 '24 16:01 malthe

@malthe flake8-bugbear is an AST plugin, so you probably could implement that exception to the rule fairly easily and contribute it.

Generally you can achieve a lot with plugins, the reason why pycodestyle isn't as powerful is because it just looks at the source code directly, rather than the AST, but flake8 plugins are free to either use AST or source code lines, so it's not a limitation with flake8 in general.

Daverball avatar Jan 18 '24 17:01 Daverball

I'd also suggest ruff, even though I've only used it once. But I tried the bare-except issue and it does not complain in contrast to flake8.

I did:

try:
    1/0
except:
    print('error')
    raise
  • ruff does not return anything, meaning all good
  • flake8says: E722 do not use bare 'except'

icemac avatar Jan 19 '24 07:01 icemac