[ty] Allow overriding rules for specific files (#18648)

This commit is contained in:
Micha Reiser
2025-06-15 15:27:39 +02:00
committed by GitHub
parent 782363b736
commit 3a430fa6da
31 changed files with 1945 additions and 312 deletions

View File

@@ -7,7 +7,8 @@ use ruff_db::{Db as SourceDb, Upcast};
pub trait Db: SourceDb + Upcast<dyn SourceDb> {
fn is_file_open(&self, file: File) -> bool;
fn rule_selection(&self) -> &RuleSelection;
/// Resolves the rule selection for a given file.
fn rule_selection(&self, file: File) -> &RuleSelection;
fn lint_registry(&self) -> &LintRegistry;
}
@@ -126,7 +127,7 @@ pub(crate) mod tests {
!file.path(self).is_vendored_path()
}
fn rule_selection(&self) -> &RuleSelection {
fn rule_selection(&self, _file: File) -> &RuleSelection {
&self.rule_selection
}

View File

@@ -32,7 +32,7 @@ pub struct LintMetadata {
pub line: u32,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),

View File

@@ -1,10 +1,12 @@
use std::fmt::{Display, Formatter};
use ruff_macros::RustDoc;
/// The target platform to assume when resolving types.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
derive(serde::Serialize, serde::Deserialize, RustDoc),
serde(rename_all = "kebab-case")
)]
pub enum PythonPlatform {
@@ -57,6 +59,7 @@ impl Default for PythonPlatform {
#[cfg(feature = "schemars")]
mod schema {
use crate::PythonPlatform;
use ruff_db::RustDoc;
use schemars::_serde_json::Value;
use schemars::JsonSchema;
use schemars::r#gen::SchemaGenerator;
@@ -121,6 +124,10 @@ mod schema {
..SubschemaValidation::default()
})),
metadata: Some(Box::new(Metadata {
description: Some(<PythonPlatform as RustDoc>::rust_doc().to_string()),
..Metadata::default()
})),
..SchemaObject::default()
})

View File

@@ -290,7 +290,10 @@ impl<'a> CheckSuppressionsContext<'a> {
}
fn is_lint_disabled(&self, lint: &'static LintMetadata) -> bool {
!self.db.rule_selection().is_enabled(LintId::of(lint))
!self
.db
.rule_selection(self.file)
.is_enabled(LintId::of(lint))
}
fn report_lint(
@@ -315,7 +318,7 @@ impl<'a> CheckSuppressionsContext<'a> {
range: TextRange,
message: fmt::Arguments,
) {
let Some(severity) = self.db.rule_selection().severity(LintId::of(lint)) else {
let Some(severity) = self.db.rule_selection(self.file).severity(LintId::of(lint)) else {
return;
};

View File

@@ -401,7 +401,7 @@ impl<'db, 'ctx> LintDiagnosticGuardBuilder<'db, 'ctx> {
let lint_id = LintId::of(lint);
// Skip over diagnostics if the rule
// is disabled.
let (severity, source) = ctx.db.rule_selection().get(lint_id)?;
let (severity, source) = ctx.db.rule_selection(ctx.file).get(lint_id)?;
// If we're not in type checking mode,
// we can bail now.
if ctx.is_in_no_type_check() {

View File

@@ -260,7 +260,7 @@ impl ty_python_semantic::Db for CorpusDb {
!file.path(self).is_vendored_path()
}
fn rule_selection(&self) -> &RuleSelection {
fn rule_selection(&self, _file: File) -> &RuleSelection {
&self.rule_selection
}