mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +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
|
@ -24,32 +24,63 @@ pub mod types;
|
|||
pub mod visitor;
|
||||
pub mod whitespace;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum PySourceType {
|
||||
#[default]
|
||||
Python,
|
||||
Stub,
|
||||
Ipynb,
|
||||
/// The type of a source file.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum SourceType {
|
||||
/// The file contains Python source code.
|
||||
Python(PySourceType),
|
||||
/// The file contains TOML.
|
||||
Toml(TomlSourceType),
|
||||
}
|
||||
|
||||
impl PySourceType {
|
||||
pub const fn is_python(&self) -> bool {
|
||||
matches!(self, PySourceType::Python)
|
||||
impl Default for SourceType {
|
||||
fn default() -> Self {
|
||||
Self::Python(PySourceType::Python)
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn is_stub(&self) -> bool {
|
||||
matches!(self, PySourceType::Stub)
|
||||
impl From<&Path> for SourceType {
|
||||
fn from(path: &Path) -> Self {
|
||||
match path.file_name() {
|
||||
Some(filename) if filename == "pyproject.toml" => Self::Toml(TomlSourceType::Pyproject),
|
||||
Some(filename) if filename == "Pipfile" => Self::Toml(TomlSourceType::Pipfile),
|
||||
Some(filename) if filename == "poetry.lock" => Self::Toml(TomlSourceType::Poetry),
|
||||
_ => match path.extension() {
|
||||
Some(ext) if ext == "toml" => Self::Toml(TomlSourceType::Unrecognized),
|
||||
_ => Self::Python(PySourceType::from(path)),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn is_ipynb(&self) -> bool {
|
||||
matches!(self, PySourceType::Ipynb)
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum TomlSourceType {
|
||||
/// The source is a `pyproject.toml`.
|
||||
Pyproject,
|
||||
/// The source is a `Pipfile`.
|
||||
Pipfile,
|
||||
/// The source is a `poetry.lock`.
|
||||
Poetry,
|
||||
/// The source is an unrecognized TOML file.
|
||||
Unrecognized,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, is_macro::Is)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum PySourceType {
|
||||
/// The source is a Python file (`.py`).
|
||||
#[default]
|
||||
Python,
|
||||
/// The source is a Python stub file (`.pyi`).
|
||||
Stub,
|
||||
/// The source is a Jupyter notebook (`.ipynb`).
|
||||
Ipynb,
|
||||
}
|
||||
|
||||
impl From<&Path> for PySourceType {
|
||||
fn from(path: &Path) -> Self {
|
||||
match path.extension() {
|
||||
Some(ext) if ext == "py" => PySourceType::Python,
|
||||
Some(ext) if ext == "pyi" => PySourceType::Stub,
|
||||
Some(ext) if ext == "ipynb" => PySourceType::Ipynb,
|
||||
_ => PySourceType::Python,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue