malthe / chameleon

Fast HTML/XML template engine for Python
https://chameleon.readthedocs.io
Other
177 stars 64 forks source link

Find better linting tool #409

Open malthe opened 10 months ago

malthe commented 10 months ago

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 commented 10 months ago

@icemac do you know about an alternative?

Daverball commented 10 months ago

@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 commented 10 months ago

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.

malthe commented 10 months ago

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!

Daverball commented 10 months ago

@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.

icemac commented 10 months ago

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
fschulze commented 2 months ago

I would also suggest ruff. I use --select=ALL and then a bunch of ignores for things I don't like or care about. See https://github.com/fschulze/devpi/blob/server612/ruff-strict.toml. I also have ignored a bunch of existing errors, but use match-diff-lines to error when changes are made: https://github.com/fschulze/devpi/blob/server612/.ci/lint-strict.sh