Skip to content

Add match_filter and import_filter hooks to Restructure - #918

Open
marlon-costa-dc wants to merge 5 commits into
python-rope:masterfrom
marlon-costa-dc:pr/restructure-hooks
Open

marlon-costa-dc wants to merge 5 commits into
python-rope:masterfrom
marlon-costa-dc:pr/restructure-hooks

Conversation

@marlon-costa-dc

Copy link
Copy Markdown
Contributor

Restructure accepts two optional callables:

  • match_filter(pymodule, match) is called for every match found in a module, and only the matches it accepts are restructured;
  • import_filter(resource, imports) selects which of imports are added to a changed module.

Today a client that wants to skip some matches (for example matches inside code it must not touch) or reuse an import the module already has has to override the private _compute_changes and _add_imports. Both hooks default to None, so existing callers are unaffected.

The hook type aliases (MatchFilter, ImportFilter) are defined at module level, so typing.get_type_hints(Restructure.__init__) resolves.

Tests in restructuretest.py cover filtering matches, filtering imports, and the runtime annotations. Tested locally on Python 3.10, 3.11, 3.13 and 3.14.

marlon-costa-dc and others added 2 commits October 31, 2026 16:21
Restructure accepts two optional callables:

- match_filter(pymodule, match) is consulted for every match found in a
  module; only the matches it accepts are restructured.
- import_filter(resource, imports) selects the imports added to a changed
  module.

Clients previously had to override the private _compute_changes and
_add_imports to select matches or to reuse bindings a module already has.
The hook type aliases are defined at module level, so the annotations of
Restructure.__init__ resolve at runtime.

(cherry picked from commit 4e2da5a)
(cherry picked from commit 3bc57a7)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.45%. Comparing base (ff8a306) to head (dd245d9).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #918   +/-   ##
=======================================
  Coverage   95.44%   95.45%           
=======================================
  Files         134      134           
  Lines       26910    26952   +42     
=======================================
+ Hits        25685    25727   +42     
  Misses       1225     1225           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

marlon-costa-dc added a commit to flext-sh/flext-infra that referenced this pull request Nov 22, 2026
…lter hook (flext-2lout) (#2760)

## Summary

`FlextInfraRopeRestructure` overrode Rope's private
`Restructure._add_imports` to drop the import declarations whose binding
the changed module already defines (keeping a richer public facade
instead of rebinding it). The pinned rope (`dc-use`, the same hook
proposed upstream as python-rope/rope#918) calls a public
`import_filter(resource, imports)` exactly before adding imports, so
this passes `_admit_imports` as that hook and deletes the private
override.

- Same selection: with a mapping policy, keep only the declarations
whose binding is not defined by the module; otherwise keep the requested
imports.
- No private-method coupling to Rope; 16 insertions, 22 deletions.

Bead: flext-2lout (rope fork reduced to what flext-infra needs, through
public hooks).

## Note

This lane's commit hook did not run (its workspace setup had not
finished, so prek was not installed); the integration CI is the gate for
this PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Replaces the private `Restructure._add_imports` override with the public
`import_filter` hook that the pinned rope fork exposes, keeping the same
import-admission behavior. This removes coupling to rope's private API
and trims the restructure code.

<sup>Written for commit b6b2614.
Summary will update on new commits.</sup>

<a
href="https://cubic.dev/pr/flext-sh/flext-infra/pull/2760?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>

<!-- End of auto-generated description by cubic. -->

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