Skip to content

Fix validation of nonexistent forbidden modules within root packages - #387

Merged
seddonym merged 1 commit into
seddonym:mainfrom
ozenyunus35:fix/forbidden-module-does-not-exist
Oct 13, 2026
Merged

seddonym merged 1 commit into
seddonym:mainfrom
ozenyunus35:fix/forbidden-module-does-not-exist

Conversation

@ozenyunus35

Copy link
Copy Markdown
Contributor

Summary

Fixes #386.

A forbidden contract could incorrectly pass when a forbidden module was
specified within a root package but did not exist in the import graph.

For example, a typo in a forbidden module name could result in the contract
being reported as KEPT, even though the intended restriction was never checked.

Changes

  • Validate that forbidden modules belonging to the configured root packages
    exist in the import graph.
  • Reuse the existing module-existence validation helper.
  • Preserve the existing behavior for external forbidden modules, which may
    legitimately be absent from the graph.
  • Add regression tests for both supported package configurations.
  • Add a release note and contributor entry as required by CONTRIBUTING.md.

Testing

Added regression tests covering:

  • Nonexistent forbidden modules within root packages.
  • Both as_packages configurations.
  • External forbidden modules that are not imported.

The new tests fail against the original implementation and pass with this fix.

Validation performed:

  • test_with_ui_deps_installed-3.12: 507 passed.
  • test_without_ui_deps_installed-3.12: 3 passed.
  • Ruff formatting and lint checks: passed.
  • Mypy: passed.
  • Import-linter's own import checks: passed.

The CLI reproduction now reports:

Module 'pkg.bb' does not exist.

instead of incorrectly reporting that the contract is kept.

Compatibility

This intentionally changes the behavior for nonexistent forbidden modules
within root packages: an invalid configuration now raises an error rather
than silently passing.

External forbidden modules retain their existing behavior.

A forbidden contract only searches for imports of the forbidden modules
that are in the graph, so a forbidden module within the root packages
that doesn't exist (e.g. a typo) made the contract pass without checking
anything.

Modules within the root packages are always in the graph, so raise the
same "does not exist" error as for source modules. External forbidden
modules still don't need to be imported anywhere.

Fixes seddonym#386

@seddonym seddonym left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks so much for your contribution!

@seddonym
seddonym merged commit 447ec81 into seddonym:main Oct 13, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forbidden contracts are kept when a forbidden module doesn't exist

2 participants