Ignore direct source-children in implicit-namespace-package (#2560)

This commit is contained in:
Charlie Marsh 2023-02-03 19:20:27 -05:00 committed by GitHub
parent e6316b185e
commit 4149bc7be8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View file

@ -13,7 +13,7 @@ pub fn check_file_path(
// flake8-no-pep420
if settings.rules.enabled(&Rule::ImplicitNamespacePackage) {
if let Some(diagnostic) = implicit_namespace_package(path, package) {
if let Some(diagnostic) = implicit_namespace_package(path, package, &settings.src) {
diagnostics.push(diagnostic);
}
}

View file

@ -1,4 +1,4 @@
use std::path::Path;
use std::path::{Path, PathBuf};
use ruff_macros::derive_message_formats;
@ -19,8 +19,19 @@ impl Violation for ImplicitNamespacePackage {
}
/// INP001
pub fn implicit_namespace_package(path: &Path, package: Option<&Path>) -> Option<Diagnostic> {
if package.is_none() && path.extension().map_or(true, |ext| ext != "pyi") {
pub fn implicit_namespace_package(
path: &Path,
package: Option<&Path>,
src: &[PathBuf],
) -> Option<Diagnostic> {
if package.is_none()
// Ignore `.pyi` files, which don't require an `__init__.py`.
&& path.extension().map_or(true, |ext| ext != "pyi")
// Ignore any files that are direct children of a source directory (e.g., `src/manage.py`).
&& !path
.parent()
.map_or(false, |parent| src.iter().any(|src| src == parent))
{
#[cfg(all(test, windows))]
let path = path
.to_string_lossy()