Skip to content

Add as_package parameter to direct import methods - #335

Open
Peter554 wants to merge 1 commit into
python-grimp:mainfrom
Peter554:direct-import-methods-as-package
Open

Peter554 wants to merge 1 commit into
python-grimp:mainfrom
Peter554:direct-import-methods-as-package

Conversation

@Peter554

@Peter554 Peter554 commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Closes #25.

find_upstream_modules and find_downstream_modules can treat the supplied module as a package with as_package=True, and direct_import_exists has as_packages. find_modules_directly_imported_by and find_modules_that_directly_import didn't, which made it awkward to ask "what does this package directly import?" or "what directly imports this package?".

To do that today, you have to find the package's descendants, call the method for each one, combine the results, and remove the modules within the package.

As discussed here import-linter/import-linter#406 (comment)

Changes

Add as_package=False to both methods. With as_package=True:

  • the module's descendants are included, as for find_upstream_modules;
  • the result excludes modules within the package.

The default behaviour is unchanged. Includes tests, docs and a changelog entry.

@Peter554 Peter554 self-assigned this Nov 10, 2026
@Peter554
Peter554 force-pushed the direct-import-methods-as-package branch from 46c4fb3 to 0bb356d Compare November 10, 2026 16:46
@codspeed

codspeed Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 23 skipped benchmarks1


Comparing Peter554:direct-import-methods-as-package (f2e4cfc) with main (3c4c7c9)

Open in CodSpeed

Footnotes

  1. 23 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@Peter554
Peter554 force-pushed the direct-import-methods-as-package branch 2 times, most recently from 68662ca to f2e4cfc Compare November 10, 2026 16:51
@Peter554
Peter554 marked this pull request as ready for review November 10, 2026 16:53
@seddonym
seddonym requested a balanced review from Copilot November 13, 2026 12:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the package-aware direct import query changes.

No open issues 🎉

What changed in this PR

Adds package-aware direct import queries to Grimp, addressing #25. Callers can include a module’s descendants without manually combining and filtering results.

Changes:

  • Add optional as_package=False to both direct import query methods, preserving default behavior.
  • Implement package-level queries in Rust, excluding imports within the queried package.
  • Add Python and Rust tests, API documentation, and a changelog entry.
File Description
tests/​unit/​application/​graph/​test_direct_imports.py Test package queries and missing-module errors in both modes.
src/​grimp/​application/​graph.py Expose and document as_package in the Python API.
rust/​src/​graph/​mod.rs Pass the new option through the Python–Rust bindings.
rust/​src/​graph/​graph_manipulation.rs Adapt internal callers while preserving individual-module behavior.
rust/​src/​graph/​direct_import_queries.rs Implement package-aware queries and add Rust tests.
docs/​usage.rst Document the parameter and package filtering behavior.
CHANGELOG.rst Announce the new option and link to #25.

Review effort: Balanced

Previously this checked every import in the graph against both
expressions, which made each call proportional to the size of the graph.
This is slow when it is called many times, as Import Linter does for
each of the expressions in a contract's ignore_imports.

Instead, find the modules that match each expression, then only look at
the imports of the modules that match the importer expression.

Assistant-model: Claude Code
@seddonym
seddonym force-pushed the direct-import-methods-as-package branch from f2e4cfc to 7bc1e5c Compare November 13, 2026 12:52

This branch has not been deployed

No deployments
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.

as_package parameter for direct import methods

2 participants