Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions crates/ty/tests/cli/rule_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,3 +1092,129 @@ fn configuration_all_rules() -> anyhow::Result<()> {

Ok(())
}

/// In TOML, key order in a table is not semantically meaningful, so specific rules should
/// still override `all` even if they sort lexicographically before `all`.
#[test]
fn configuration_all_rules_with_rule_sorting_before_all() -> anyhow::Result<()> {
let case = CliTest::with_files([
(
"pyproject.toml",
r#"
[tool.ty.rules]
all = "warn"
abstract-method-in-final-class = "error"
"#,
),
(
"test.py",
r#"
from typing import final
from abc import ABC, abstractmethod

class Base(ABC):
@abstractmethod
def foo(self) -> int:
raise NotImplementedError

@final
class Derived(Base):
pass
"#,
),
])?;

assert_cmd_snapshot!(case.command(), @"
success: false
exit_code: 1
----- stdout -----
error[abstract-method-in-final-class]: Final class `Derived` has unimplemented abstract methods
--> test.py:11:7
|
10 | @final
11 | class Derived(Base):
| ^^^^^^^ `foo` is unimplemented
12 | pass
|
::: test.py:7:9
|
5 | class Base(ABC):
6 | @abstractmethod
7 | def foo(self) -> int:
| --- `foo` declared as abstract on superclass `Base`
8 | raise NotImplementedError
|
info: rule `abstract-method-in-final-class` was selected in the configuration file

Found 1 diagnostic

----- stderr -----
");

Ok(())
}

/// Same TOML key ordering issue, but within an override's `[rules]` table.
/// `abstract-method-in-final-class` sorts before `all` lexicographically, but
/// the specific rule should still take precedence over `all`.
#[test]
fn overrides_all_rules_with_rule_sorting_before_all() -> anyhow::Result<()> {
let case = CliTest::with_files([
(
"pyproject.toml",
r#"
[[tool.ty.overrides]]
include = ["src/**"]

[tool.ty.overrides.rules]
all = "warn"
abstract-method-in-final-class = "error"
"#,
),
(
"src/test.py",
r#"
from typing import final
from abc import ABC, abstractmethod

class Base(ABC):
@abstractmethod
def foo(self) -> int:
raise NotImplementedError

@final
class Derived(Base):
pass
"#,
),
])?;

assert_cmd_snapshot!(case.command(), @"
success: false
exit_code: 1
----- stdout -----
error[abstract-method-in-final-class]: Final class `Derived` has unimplemented abstract methods
--> src/test.py:11:7
|
10 | @final
11 | class Derived(Base):
| ^^^^^^^ `foo` is unimplemented
12 | pass
|
::: src/test.py:7:9
|
5 | class Base(ABC):
6 | @abstractmethod
7 | def foo(self) -> int:
| --- `foo` declared as abstract on superclass `Base`
8 | raise NotImplementedError
|
info: rule `abstract-method-in-final-class` was selected in the configuration file

Found 1 diagnostic

----- stderr -----
");

Ok(())
}
35 changes: 34 additions & 1 deletion crates/ty_project/src/metadata/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use ruff_python_ast::PythonVersion;
use rustc_hash::FxHasher;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt::{self, Debug, Display};
use std::hash::BuildHasherDefault;
use std::ops::Deref;
Expand Down Expand Up @@ -107,10 +108,42 @@ pub struct Options {
impl Options {
pub fn from_toml_str(content: &str, source: ValueSource) -> Result<Self, TyTomlError> {
let _guard = ValueSourceGuard::new(source, true);
let options = toml::from_str(content)?;
let mut options: Self = toml::from_str(content)?;
options.prioritize_all_selectors();
Ok(options)
}

/// Ensures that the `all` selector is applied before per-rule selectors
/// in all rule tables (top-level and overrides).
///
/// This must be called after deserializing from TOML and before any
/// [`Combine::combine`] calls, because TOML tables are unordered and the
/// `toml` crate sorts keys lexicographically.
pub(crate) fn prioritize_all_selectors(&mut self) {
// Stable sort that moves all `all` selectors before non-`all` selectors
// while preserving relative order among non-`all` entries.
let sort = |rules: &mut Rules| {
rules.inner.sort_by(
|key_a, _, key_b, _| match (**key_a == "all", **key_b == "all") {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => Ordering::Equal,
},
);
};

if let Some(rules) = &mut self.rules {
sort(rules);
}
if let Some(overrides) = &mut self.overrides {
for override_option in &mut overrides.0 {
if let Some(rules) = &mut override_option.rules {
sort(rules);
}
}
}
}

pub fn deserialize_with<'de, D>(source: ValueSource, deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down
11 changes: 10 additions & 1 deletion crates/ty_project/src/metadata/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ impl PyProject {
source: ValueSource,
) -> Result<Self, PyProjectError> {
let _guard = ValueSourceGuard::new(source, true);
toml::from_str(content).map_err(PyProjectError::TomlSyntax)
let mut pyproject: Self = toml::from_str(content).map_err(PyProjectError::TomlSyntax)?;
// TOML tables are unordered and the `toml` crate sorts keys
// lexicographically. Normalize rule order so that the `all` selector
// is applied before per-rule selectors.
if let Some(tool) = &mut pyproject.tool {
if let Some(ty) = &mut tool.ty {
ty.prioritize_all_selectors();
}
}
Ok(pyproject)
}
}

Expand Down
Loading