mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Add TOML files to SourceType
(#6929)
## Summary
This PR adds a higher-level enum (`SourceType`) around `PySourceType` to
allow us to use the same detection path to handle TOML files. Right now,
we have ad hoc `is_pyproject_toml` checks littered around, and some
codepaths are omitting that logic altogether (like `add_noqa`). Instead,
we should always be required to check the source type and handle TOML
files as appropriate.
This PR will also help with our pre-commit capabilities. If we add
`toml` to pre-commit (to support `pyproject.toml`), pre-commit will
start to pass _other_ files to Ruff (along with `poetry.lock` and
`Pipfile` -- see
[identify](b59996304f/identify/extensions.py (L355)
)).
By detecting those files and handling those cases, we avoid attempting
to parse them as Python files, which would lead to pre-commit errors.
(We tried to add `toml` to pre-commit here
(https://github.com/astral-sh/ruff-pre-commit/pull/44), but had to
revert here (https://github.com/astral-sh/ruff-pre-commit/pull/45) as it
led to the pre-commit hook attempting to parse `poetry.lock` files as
Python files.)
This commit is contained in:
parent
2893a9f6b5
commit
58f5f27dc3
7 changed files with 135 additions and 136 deletions
|
@ -8,7 +8,7 @@ use rayon::prelude::*;
|
|||
|
||||
use ruff::linter::add_noqa_to_path;
|
||||
use ruff::warn_user_once;
|
||||
use ruff_python_stdlib::path::{is_jupyter_notebook, is_project_toml};
|
||||
use ruff_python_ast::{PySourceType, SourceType};
|
||||
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig};
|
||||
|
||||
use crate::args::Overrides;
|
||||
|
@ -46,15 +46,17 @@ pub(crate) fn add_noqa(
|
|||
.flatten()
|
||||
.filter_map(|entry| {
|
||||
let path = entry.path();
|
||||
if is_project_toml(path) || is_jupyter_notebook(path) {
|
||||
let SourceType::Python(source_type @ (PySourceType::Python | PySourceType::Stub)) =
|
||||
SourceType::from(path)
|
||||
else {
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let package = path
|
||||
.parent()
|
||||
.and_then(|parent| package_roots.get(parent))
|
||||
.and_then(|package| *package);
|
||||
let settings = resolver.resolve(path, pyproject_config);
|
||||
match add_noqa_to_path(path, package, settings) {
|
||||
match add_noqa_to_path(path, package, source_type, settings) {
|
||||
Ok(count) => Some(count),
|
||||
Err(e) => {
|
||||
error!("Failed to add noqa to {}: {e}", path.display());
|
||||
|
|
|
@ -12,7 +12,7 @@ use tracing::{span, Level};
|
|||
use ruff::fs;
|
||||
use ruff::warn_user_once;
|
||||
use ruff_formatter::LineWidth;
|
||||
use ruff_python_ast::PySourceType;
|
||||
use ruff_python_ast::{PySourceType, SourceType};
|
||||
use ruff_python_formatter::{format_module, FormatModuleError, PyFormatOptions};
|
||||
use ruff_workspace::resolver::python_files_in_path;
|
||||
|
||||
|
@ -37,23 +37,21 @@ pub(crate) fn format(cli: &Arguments, overrides: &Overrides) -> Result<ExitStatu
|
|||
|
||||
let result = paths
|
||||
.into_par_iter()
|
||||
.map(|dir_entry| {
|
||||
let dir_entry = dir_entry?;
|
||||
let path = dir_entry.path();
|
||||
let source_type = PySourceType::from(path);
|
||||
if !(source_type.is_python() || source_type.is_stub())
|
||||
|| path
|
||||
.extension()
|
||||
.is_some_and(|extension| extension == "toml")
|
||||
{
|
||||
return Ok(());
|
||||
.map(|entry| {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if matches!(
|
||||
SourceType::from(path),
|
||||
SourceType::Python(PySourceType::Python | PySourceType::Stub)
|
||||
) {
|
||||
let line_length = resolver.resolve(path, &pyproject_config).line_length;
|
||||
let options = PyFormatOptions::from_extension(path)
|
||||
.with_line_width(LineWidth::from(NonZeroU16::from(line_length)));
|
||||
|
||||
format_path(path, options)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
let line_length = resolver.resolve(path, &pyproject_config).line_length;
|
||||
let options = PyFormatOptions::from_extension(path)
|
||||
.with_line_width(LineWidth::from(NonZeroU16::from(line_length)));
|
||||
|
||||
format_path(path, options)
|
||||
})
|
||||
.map(|result| {
|
||||
result.map_err(|err| {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue