ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Add default linter rules

Open AndreuCodina opened this issue 1 year ago • 4 comments

In my company we have lots of microservices and nanoservices, and a Python project prototype I maintain to guide how Python projects must be developed and updated. It's a common complaint not to have default rules in our pyproject.toml files, and instead have "magic letters" just because "I chose them". My job is to ensure data engineers develop software as we do in .NET, i.e. with types.

I want to propose a group of default linter rules, and choose them with groups (letters), instead of choosing a subset.

pyproject.toml

[tool.ruff.lint]
select = ["F", "E", "W", "N", "I", "ANN"]
ignore = ["E501", "ANN101", "ANN102", "ANN401"]

Reasoning of select:

  • F: Unused variables, comparison with ==, etc.
  • E, W, N: PEP 8.
  • I: Sort imports.
  • ANN: Mandatory return types.

Reasoning of ignore:

  • E501: Line too long hasn't an automatic fix.
  • ANN101, ANN102: Typing self and cls is strange in Python.
  • ANN401: The Python ecosystem is not prepared to not use Any.

Besides, I always use this configuration:

[tool.pyright]
typeCheckingMode = "strict"

[tool.ruff]
src = ["src", "tests"]
extend-include = ["*.ipynb"]

AndreuCodina avatar May 13 '24 20:05 AndreuCodina

Hi!

We're going to recategorize all of the rules (roughly discussed in https://github.com/astral-sh/ruff/issues/1774) and define new defaults eventually. We're actively working on this, but I'm not sure when we'll be done because it's a big project and we want to get it right.

zanieb avatar May 13 '24 21:05 zanieb

Similarly, this request fits into https://github.com/astral-sh/ruff/issues/1773 which is a part of that effort.

zanieb avatar May 13 '24 21:05 zanieb

@zanieb Do you think this can be merged into either of the linked issue?

dhruvmanila avatar May 14 '24 08:05 dhruvmanila

I'm okay with keeping it open for now unless there's a duplicate issue regarding defaults.

We should add a dedicated tracking issue for the full re-categorization work eventually.

zanieb avatar May 14 '24 14:05 zanieb

Should we include default rules for some project types? For instance, there're FastAPI rules (https://docs.astral.sh/ruff/rules/#fastapi-fast). It's a library very used, but I don't know if Ruff knows if a package (fastapi in this case) is installed and then it doesn't waste resources with those rules.

AndreuCodina avatar Aug 18 '24 09:08 AndreuCodina

Idea: how about declaring the rule "strict" as an alias for the linter rules I posted?

[tool.ruff.lint]
select = ["strict"]

This aligns with Pyright:

[tool.pyright]
typeCheckingMode = "strict"

AndreuCodina avatar Oct 08 '24 07:10 AndreuCodina

I'll link to https://github.com/astral-sh/ruff/issues/12352 and https://github.com/astral-sh/ruff/discussions/3363#discussioncomment-7266932 as well. With which you could make your own custom defaults for your needs and "levels of strictness"

Avasam avatar Oct 11 '24 01:10 Avasam