astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
31.16k stars 1.04k forks source link

Validation for ruff config file #10487

Open VascoSch92 opened 6 months ago

VascoSch92 commented 6 months ago

Ruff-version : 0.3.3

Hello everyone,

I'm concerned about the configuration file for Ruff (pyproject.toml,ruff.toml, ...).

Are there checks for this configuration file? I'm asking not only about the syntax but also about the content.

I'm asking because of several reasons:

To clarify my point of view, here is a snippet of the configuration file that I use:

[tool.ruff]
# Set of rules enabled
lint.select = [
    "ARG002", # Checks for the presence of unused arguments in instance method definitions
    "ANN201", # Missing return type annotation for public function
    "E", # Pycodestyle Error
    "F", # Pyflakes
    "B", # Flake8-bugbear
    "W", # Pycodestyle Warning
    "D101", # Undocumented public class
    "D102", # Undocumneted public method
    "D103", # Undocumented public function
    "D105", # Undocumented magic method
    "D200", # One-line docstring should fit on one line
    "D202", # No blank lines allowed after function docstring
    "D300", # Use triple double quotes """
    "D404", # First word of the docstring should not be "This"
    "UP003", # Use {} instead of type(...)
    "UP008", # Use super() instead of super(_class_, self)
    "UP011", # Unnecessary parentheses to functools.lru_cache
    "UP027", # Replace unpacked list comprehension with a generator expression
    "UP032", # Use f-string instead of format call
    "C4", # flake8-comprehensions
    "ICN", # flake8-import-conventions
    "NPY", # NumPy-specific rules (NPY)
    "ARG", # flake8-unused-arguments
    "PLE0604", # Invalid object in __all__, must contain only strings
    "PLE0605", # Invalid format for __all__, must be tuple or list
    "PIE", # flake8-pie
    "PTH", # flake8-use-pathlib
    "Q000",
    "LOG", # checks for impropriate use of logging
    "Q002", # flake-8 double quotes for dosctring
    "I001", # Import block is un-sorted or un-formatted
]

# Ignored rules
lint.ignore = [
    "ARG002", # Checks for the presence of unused arguments in instance method definitions
    "F821", # Checks for uses of undefined names
    "B020", # Checks for contextlib.suppress without arguments
    "B023", # Checks for function definitions that use a loop variable
    "PTH123", # Checks for uses of the open builtin
    "PIE804", # Checks for unnecessary dict kwargs
    "PIE807", # Checks for lambdas that can be replaced with the list builtin
    "W605", # Checks for invalid escape sequences
]

# Set the max length of a line
line-length = 121

# The style in which the output message should be formatted
output-format = "grouped"

lint.flake8-quotes.inline-quotes = "double"
lint.flake8-quotes.docstring-quotes = "double"
lint.flake8-quotes.multiline-quotes = "double"

lint.isort.combine-as-imports = true
lint.isort.length-sort = true

[tool.ruff.format]
indent-style = "space"
skip-magic-trailing-comma = false
quote-style = "single"

Problems:

  1. You can see that the rule "ARG002" is included in both select and ignore. As my project doesn't have unused arguments, I don't know if the rule is ignored or it is just satisfied.
  2. you can see that I have lint.flake8-quotes.inline-quotes = "double" and quote-style = "single". Which rule are we applying? In this case the first one but I had to run ruff and check the output.

At the end of the day, I'm just asking to add checks to make the config file free of situations where there may be some confusion

Thanks for your answer

PS: Ruff is really nice, you are doing a great job :-)

MichaReiser commented 6 months ago

Yeah that makes a lot of sense. I think we do some very basic verification today but it is limited to whether the pyproject.toml is valid. But we don't perform any validation to catch potential errors in the configuration.

This is related to https://github.com/astral-sh/ruff/issues/10227

zanieb commented 6 months ago

You can also verify these configuration files via their JSON schemas

e.g. https://github.com/astral-sh/ruff/blob/main/ruff.schema.json / https://www.schemastore.org/json/

VascoSch92 commented 6 months ago

Yeah that makes a lot of sense. I think we do some very basic verification today but it is limited to whether the pyproject.toml is valid. But we don't perform any validation to catch potential errors in the configuration. This is related to https://github.com/astral-sh/ruff/issues/10227

I understand. Regarding issue https://github.com/astral-sh/ruff/issues/10227, I wanted to inquire about implementing rules to verify configuration files (and I see that the config file of Ruff can be one of these :-( ;-) ).

My idea is to introduce additional checks/verifications on the Ruff configuration file to potentially block linting or formatting if the configuration file is ambiguous. For example:

By implementing these checks, we can ensure that the Ruff configuration file is unambiguous before proceeding with linting or formatting. This could help for the following cases:

You can also verify these configuration files via their JSON schemas e.g. https://github.com/astral-sh/ruff/blob/main/ruff.schema.json / https://www.schemastore.org/json/

Yes, but this is just for the structure not for the content right?

charliermarsh commented 6 months ago

You can also run ruff check --show-settings to view the final, resolved settings for your project.

aneeshusa commented 5 months ago

+1 to this, I accidentally ran into Ensuring there are no rules that appear both in the select and ignore sections simultaneously. when updating an old POC to use latest ruff. Went to write an internal test and realized this is something ruff could be handling itself