Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect empty implicit namespace packages #14236

Merged
merged 2 commits into from
Nov 10, 2024
Merged

Detect empty implicit namespace packages #14236

merged 2 commits into from
Nov 10, 2024

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Nov 10, 2024

Summary

The implicit namespace package rule currently fails to detect cases like the following:

foo/
├── __init__.py
└── bar/
    └── baz/
        └── __init__.py

The problem is that we detect a root at foo, and then an independent root at baz. We would detect that bar is an implicit namespace package, but it doesn't contain any files! So we never check it, and have no place to raise the diagnostic.

This PR adds detection for these kinds of nested packages, and augments the INP rule to flag the __init__.py file above with a specialized message. As a side effect, I've introduced a dedicated PackageRoot struct which we can pass around in lieu of Yet Another Path.

For now, I'm only enabling this in preview (and the approach doesn't affect any other rules). It's a bug fix, but it may end up expanding the rule.

Closes #13519.

@charliermarsh charliermarsh added bug Something isn't working preview Related to preview mode features labels Nov 10, 2024
Copy link
Contributor

github-actions bot commented Nov 10, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+2 -0 violations, +0 -0 fixes in 1 projects; 53 projects unchanged)

bokeh/bokeh (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ tests/unit/bokeh/embed/ext_package_no_main/__init__.py:1:1: INP001 File `tests/unit/bokeh/embed/ext_package_no_main/__init__.py` declares a package, but is nested under an implicit namespace package. Add an `__init__.py` to `tests/unit/bokeh/embed`.
+ tests/unit/bokeh/embed/latex_label/__init__.py:1:1: INP001 File `tests/unit/bokeh/embed/latex_label/__init__.py` declares a package, but is nested under an implicit namespace package. Add an `__init__.py` to `tests/unit/bokeh/embed`.

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
INP001 2 2 0 0 0

@charliermarsh charliermarsh merged commit c7d48e1 into main Nov 10, 2024
20 checks passed
@charliermarsh charliermarsh deleted the charlie/nested branch November 10, 2024 03:03
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that this file is commited?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working preview Related to preview mode features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

INP001 doesn't trigger if there are no Python files in the immediate directory
2 participants