Add an Unrecognized variant to PySourceType

This commit is contained in:
Charlie Marsh 2023-08-27 23:13:28 -04:00
parent a33883d51a
commit 9c5988f23e
4 changed files with 26 additions and 10 deletions

View File

@ -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;
};

View File

@ -42,7 +42,7 @@ pub(crate) fn format(cli: &Arguments, overrides: &Overrides) -> Result<ExitStatu
let path = entry.path();
if matches!(
SourceType::from(path),
SourceType::Python(PySourceType::Python | PySourceType::Stub)
SourceType::Python(PySourceType::Py | PySourceType::Pyi | PySourceType::Pyw)
) {
let line_length = resolver.resolve(path, &pyproject_config).line_length;
let options = PyFormatOptions::from_extension(path)

View File

@ -35,7 +35,7 @@ pub enum SourceType {
impl Default for SourceType {
fn default() -> 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,
}
}
}

View File

@ -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,
}
}