← 输入框|github.com|刷新
Skip to content

Add exclude_shared_descendants parameter to chain and direct import queries - #337

Open
Peter554 wants to merge 2 commits into
python-grimp:mainfrom
Peter554:exclude-shared-descendants
Open

Peter554 wants to merge 2 commits into
python-grimp:mainfrom
Peter554:exclude-shared-descendants

Conversation

@Peter554

@Peter554 Peter554 commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

find_shortest_chain, find_shortest_chains, chain_exists and direct_import_exists raise ValueError("Modules have shared descendants.") with as_packages=True when one package contains the other. There's no way to ask for chains (or direct imports) between an inner package and the rest of the outer package, e.g. between pkg.inner and everything else in pkg.

Import Linter needs this for forbidden contracts where the source and forbidden modules overlap - see import-linter/import-linter#423.

Change

Adds exclude_shared_descendants=False to the four methods. When True (and as_packages=True), the shared modules are removed from the outer package, so the search is between the inner package and the rest of the outer one. If the two packages are identical, nothing is left to search, so there are no chains.

The default is unchanged, so existing callers still get the ValueError.

Commits

  1. Extract get_importer_and_imported_modules helper. Refactor only. The chain and direct import queries each built the importer and imported module sets (including descendants) and checked for shared descendants themselves. They now share one helper on Graph, next to get_module_descendants.
  2. Add exclude_shared_descendants parameter. The behaviour change: the helper takes the new flag, which the four methods expose. Includes tests, docs and the changelog.

Open questions

  • Naming of the argument.
  • find_direct_imports_between also still raises on shared descendants, but isn't exposed in the Python API, so I've left it.

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 23 skipped benchmarks1


Comparing Peter554:exclude-shared-descendants (8ce19f3) with main (dde21ee)

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. ↩

The same logic for building the importer and imported module sets (and checking for shared
descendants) was repeated in the chain and direct import queries.

Assistant-model: Claude Code
@Peter554
Peter554 force-pushed the exclude-shared-descendants branch from fbc8d4e to f3ad50c Compare November 17, 2026 13:20
Adds the parameter to find_shortest_chain, find_shortest_chains, chain_exists and
direct_import_exists. It allows searching between packages where one contains the other, by
excluding the shared descendants from the outer package.

Assistant-model: Claude Code
@Peter554
Peter554 force-pushed the exclude-shared-descendants branch from f3ad50c to 8ce19f3 Compare November 17, 2026 13:25
@Peter554 Peter554 changed the title Add exclude_shared_descendants parameter to chain queries Add exclude_shared_descendants parameter to chain and direct import queries Nov 17, 2026
@Peter554
Peter554 marked this pull request as ready for review November 17, 2026 15:41

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.

1 participant