diff --git a/crates/ruff_cli/src/commands/add_noqa.rs b/crates/ruff_cli/src/commands/add_noqa.rs index f7b219a56d..7938cf8b13 100644 --- a/crates/ruff_cli/src/commands/add_noqa.rs +++ b/crates/ruff_cli/src/commands/add_noqa.rs @@ -46,8 +46,9 @@ pub(crate) fn add_noqa( .flatten() .filter_map(|entry| { let path = entry.path(); - let SourceType::Python(source_type @ (PySourceType::Python | PySourceType::Stub)) = - SourceType::from(path) + let SourceType::Python( + source_type @ (PySourceType::Py | PySourceType::Pyi | PySourceType::Pyw), + ) = SourceType::from(path) else { return None; }; diff --git a/crates/ruff_cli/src/commands/format.rs b/crates/ruff_cli/src/commands/format.rs index fa4b4b56ba..00c63dea4d 100644 --- a/crates/ruff_cli/src/commands/format.rs +++ b/crates/ruff_cli/src/commands/format.rs @@ -42,7 +42,7 @@ pub(crate) fn format(cli: &Arguments, overrides: &Overrides) -> Result Self { - Self::Python(PySourceType::Python) + Self::Python(PySourceType::Py) } } @@ -70,20 +70,32 @@ pub enum TomlSourceType { pub enum PySourceType { /// The source is a Python file (`.py`). #[default] - Python, + Py, /// The source is a Python stub file (`.pyi`). - Stub, + Pyi, + /// The source is a Python script with a graphical user interface (`.pyw`). + Pyw, /// The source is a Jupyter notebook (`.ipynb`). Ipynb, + /// The source is an unrecognized Python file. + Unrecognized, +} + +impl PySourceType { + /// Return `true` if the source type is a stub file. + pub const fn is_stub(self) -> bool { + matches!(self, Self::Pyi) + } } 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 == "py" => PySourceType::Py, + Some(ext) if ext == "pyi" => PySourceType::Pyi, + Some(ext) if ext == "pyw" => PySourceType::Pyw, Some(ext) if ext == "ipynb" => PySourceType::Ipynb, - _ => PySourceType::Python, + _ => PySourceType::Py, } } } diff --git a/crates/ruff_python_parser/src/lib.rs b/crates/ruff_python_parser/src/lib.rs index fca224f6f8..2b8d88879d 100644 --- a/crates/ruff_python_parser/src/lib.rs +++ b/crates/ruff_python_parser/src/lib.rs @@ -312,7 +312,10 @@ pub trait AsMode { impl AsMode for PySourceType { fn as_mode(&self) -> Mode { match self { - PySourceType::Python | PySourceType::Stub => Mode::Module, + PySourceType::Py + | PySourceType::Pyi + | PySourceType::Pyw + | PySourceType::Unrecognized => Mode::Module, PySourceType::Ipynb => Mode::Jupyter, } }