Ignore all non-.py wrt. implicit namespace package (#2640)

It's not only `.pyi` that should be exempt for this, but also for example scripts which don't have an extension, explicitly passed in command line args.
This commit is contained in:
Ville Skyttä 2023-02-08 01:21:59 +02:00 committed by GitHub
parent 271e4fda8c
commit 4b49fd9494
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 2 deletions

View file

@ -0,0 +1,3 @@
#!/usr/bin/env python3
print("Hello, INP001!")

View file

@ -20,6 +20,7 @@ mod tests {
#[test_case(Path::new("test_ignored"), Path::new("example.py"); "INP001_4")] #[test_case(Path::new("test_ignored"), Path::new("example.py"); "INP001_4")]
#[test_case(Path::new("test_pass_namespace_package"), Path::new("example.py"); "INP001_5")] #[test_case(Path::new("test_pass_namespace_package"), Path::new("example.py"); "INP001_5")]
#[test_case(Path::new("test_pass_pyi"), Path::new("example.pyi"); "INP001_6")] #[test_case(Path::new("test_pass_pyi"), Path::new("example.pyi"); "INP001_6")]
#[test_case(Path::new("test_pass_script"), Path::new("script"); "INP001_7")]
fn test_flake8_no_pep420(path: &Path, filename: &Path) -> Result<()> { fn test_flake8_no_pep420(path: &Path, filename: &Path) -> Result<()> {
let snapshot = format!("{}", path.to_string_lossy()); let snapshot = format!("{}", path.to_string_lossy());
let p = PathBuf::from(format!( let p = PathBuf::from(format!(

View file

@ -26,8 +26,8 @@ pub fn implicit_namespace_package(
src: &[PathBuf], src: &[PathBuf],
) -> Option<Diagnostic> { ) -> Option<Diagnostic> {
if package.is_none() if package.is_none()
// Ignore `.pyi` files, which don't require an `__init__.py`. // Ignore non-`.py` files, which don't require an `__init__.py`.
&& path.extension().map_or(true, |ext| ext != "pyi") && path.extension().map_or(false, |ext| ext == "py")
// Ignore any files that are direct children of the project root. // Ignore any files that are direct children of the project root.
&& !path && !path
.parent() .parent()

View file

@ -0,0 +1,6 @@
---
source: crates/ruff/src/rules/flake8_no_pep420/mod.rs
expression: diagnostics
---
[]