Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rules table to configuration #15645

Merged
merged 2 commits into from
Jan 23, 2025
Merged

Add rules table to configuration #15645

merged 2 commits into from
Jan 23, 2025

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Jan 21, 2025

Summary

This PR adds support for enabling or disabling a rule or change its severity to warning by using a configuration file.

[tool.knot.rules]
division-by-zero = "error"

This PR doesn't yet add support for the rule = { severity="level" } format simply because we don't support any per-rule configurations other than severity just yet (like fixability). We should add support once for a sub-table once we add support for it. This PR also does not yet add support for configuring rules over the CLI.

The "hardest" part of this PR was to make the diagnostics propagate. The approach
I've taken for now is to simply collect them when calling project.check or project.check_file.
I don't love this approach because it's somewhat easy to get wrong and we have to repeat it in
every entry function that may return diagnostics (e.g. format?). However,
we currently only exactly have two, so it sort of feels okay?

I considered using salsa accumulators but doing so wouldn't solve
the problem that we have to remember collecting the diagnostics
in the project.check call, but we could use them if we want,
performance isn't as much of a concern here.

Overall I felt like defering the ideal design until we have
more settings that require validation to get a better sense of
how awkward (or good) the current design is.

The diagnostics for unknown rules currently lack any source information. I plan on adding proper spans in a follow-up PR because it requires some machinery to funnel the spans through serde.

CC @BurntSushi: I don't consider the "how we funnel diagnostics through red knot" as the main focus of your diagnostics work but I thought this might still be interesting to you.

Part of #15491

Test Plan

Added CLI tests.

@MichaReiser MichaReiser added red-knot Multi-file analysis & type inference diagnostics Related to reporting of diagnostics. labels Jan 21, 2025
@@ -128,6 +150,23 @@ impl Project {
Arc::into_inner(result).unwrap().into_inner().unwrap()
}

pub(crate) fn check_file(self, db: &dyn Db, file: File) -> Vec<Box<dyn Diagnostic>> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included the rule diagnostics in check_file because an API consumer (e.g., the LSP) can decide only to call check_file but never project.check and would never see the diagnostics.

The counter-argument is that these diagnostics aren't related to file so they shouldn't be shown.

I'm not sure what the right answer is, but I defaulted to including them for now.

@MichaReiser MichaReiser marked this pull request as ready for review January 21, 2025 15:40
Copy link
Contributor

github-actions bot commented Jan 21, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet!

crates/red_knot/tests/cli.rs Outdated Show resolved Hide resolved
crates/red_knot/tests/cli.rs Outdated Show resolved Hide resolved
crates/red_knot/tests/cli.rs Show resolved Hide resolved
@MichaReiser MichaReiser force-pushed the micha/diagnostic-optional-file branch from 54ca21c to af3bbde Compare January 23, 2025 09:18
Base automatically changed from micha/diagnostic-optional-file to main January 23, 2025 09:43
@MichaReiser MichaReiser merged commit 7b17c9c into main Jan 23, 2025
21 checks passed
@MichaReiser MichaReiser deleted the micha/rule-table branch January 23, 2025 09:57
dcreager added a commit that referenced this pull request Jan 24, 2025
* main:
  [red-knot] MDTests: Do not depend on precise public-symbol type inference (#15691)
  [red-knot] Make `infer.rs` unit tests independent of public symbol inference (#15690)
  Tidy knot CLI tests (#15685)
  [red-knot] Port comprehension tests to Markdown (#15688)
  Create Unknown rule diagnostics with a source range (#15648)
  [red-knot] Port 'deferred annotations' unit tests to Markdown (#15686)
  [red-knot] Support custom typeshed Markdown tests (#15683)
  Don't run the linter ecosystem check on PRs that only touch red-knot crates (#15687)
  Add `rules` table to configuration (#15645)
  [red-knot] Make `Diagnostic::file` optional (#15640)
  [red-knot] Add test for nested attribute access (#15684)
  [red-knot] Anchor relative paths in configurations (#15634)
  [`pyupgrade`] Handle multiple base classes for PEP 695 generics (`UP046`) (#15659)
  [`pyflakes`] Treat arguments passed to the `default=` parameter of `TypeVar` as type expressions (`F821`) (#15679)
  Upgrade zizmor to the latest version in CI (#15649)
  [`pyupgrade`] Add rules to use PEP 695 generics in classes and functions (`UP046`, `UP047`) (#15565)
  [red-knot] Ensure a gradual type can always be assigned to itself (#15675)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diagnostics Related to reporting of diagnostics. red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants