Repository navigation
Import hoist verifier: a local import re-pointed after its definition moved is not a clash - #15622
Conversation
… moved is not a clash
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8deb5eeda3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| # Defined in the new module now, and was defined in the old one before. | ||
| if _top_level_binding(show(after_ref, new_path), name) != "def": | ||
| return False | ||
| if _top_level_binding(show(before_ref, old_path), name) != "def": | ||
| return False |
There was a problem hiding this comment.
Verify that the destination definition actually moved
When pkg.new already contains an unrelated helper, deleting pkg.old.helper and accidentally re-pointing a function-local import to pkg.new.helper now passes the verifier. These checks only establish that the same name was defined in the old module before and the new module after; they never inspect the destination before the change or compare the definitions. I reproduced this with two helpers returning different values and an unchanged destination: compare() emits only INFO findings instead of the previous TARGET-CHANGED blocker. Require evidence of an actual relocation, and retain the blocker when the destination is an unrelated pre-existing definition.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, fixed in 3cf36a8. The relocation branch now also requires that the destination module did not bind the name at the base ref, so a destination that already carried its own helper is treated as a different object and the TARGET-CHANGED blocker stays. Checked in a scratch repo: old module deleted plus a function-local import re-pointed at a pre-existing pkg.b.helper now fails, while the genuine move, the re-export case and the #15596 refactor (63 moved imports) still pass, and the clash, duplicate-definition and rename cases still fail.
…y define the name
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3cf36a8c46
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| return [f"{stem}.py", f"{stem}/__init__.py"] | ||
| rel = module.replace(".", "/") | ||
| roots = [importer_dir, *importer_dir.parents] | ||
| out = [] | ||
| for root in roots: |
There was a problem hiding this comment.
Resolve absolute imports without assuming package-local roots
When a package module uses an absolute import, searching its directory before the repository root can select a different module than Python imports and incorrectly suppress TARGET-CHANGED. For example, with pkg/client.py changing from pkg.old import helper to from util import helper, moving the original helper into pkg/util.py makes this verifier report only INFO findings, even when the actual absolute import resolves to an unrelated, unchanged root-level util.py. I reproduced this: the move predicate returns true while executing the changed function returns the root helper's different result. Resolve against established import roots, or retain the blocker when resolution is ambiguous, rather than treating every ancestor directory as a search root.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, fixed in 9978ad7. An absolute import now only resolves when exactly one candidate path exists at that ref; if both pkg/util.py and a root util.py exist, the destination is ambiguous and the move exemption does not apply, so TARGET-CHANGED stays. The re-export check on the old module goes through the same unique resolution. Your scenario (pkg/client.py switching from pkg.old import helper to from util import helper with the helper moved into pkg/util.py and an unrelated root util.py) passed on the previous commit and now fails; #15596 still reports 63 moved imports and no blockers.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9978ad77c9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if ( | ||
| isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)) | ||
| and node.name == name | ||
| ): | ||
| return "def" |
There was a problem hiding this comment.
Account for later rebinding before declaring a definition
If a module defines helper and later executes from .actual import helper, this early return reports the shadowed definition rather than the binding Python actually exports. Moving that definition into a new module, removing it and the overriding import from the old module, and re-pointing a function-local import now incorrectly qualifies as a move. I reproduced this with the original exported helper returning 2 and the relocated definition returning 1: compare() reports only INFO findings despite the changed behavior. Inspect subsequent bindings, or conservatively reject multiply bound names, before granting the move exemption.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Right, fixed in 7ff77c5. _top_level_binding now collects every module-level binding of the name, including ones inside top-level if / try / with / match blocks, tuple targets and plain import. Exactly one binding reports its kind; more than one reports ambiguous, which never satisfies the move predicate, and the final re-export check only accepts a single from ... import. Your shadowing case (a def helper later rebound by from .actual import helper, then relocated and re-pointed) passed on the previous commit and now fails; a plain relocation still passes, and #15596 still reports 63 moved imports with no blockers.
…t a plain definition
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7ff77c557a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| for target in lost: | ||
| parts = _from_parts(target) | ||
| candidates = gained_by_name.get(parts[1], []) if parts else [] | ||
| if len(candidates) != 1 or not moved(target, candidates[0]): | ||
| return False | ||
| return True |
There was a problem hiding this comment.
Require a one-to-one pairing of lost and gained targets
When two conditional imports bind the same name from different old modules, both lost targets can match the same gained target, leaving another gained target unchecked. For example, if pkg.old1 and pkg.old2 both re-export pkg.shared.helper, changing one branch to from pkg.shared import helper and the other to from pkg.other import other as helper passes this loop: both old imports match the shared helper, while the unrelated other import is ignored. I reproduced compare() emitting only INFO findings despite the second branch changing behavior. Consume each matched gained target, or otherwise require a bijection, before granting the exemption.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in ecb8e4e. _is_definition_move now consumes each gained target once and requires every gained target to be consumed, so two lost imports can no longer both pair with from pkg.shared import helper while from pkg.other import other as helper goes unchecked. Your scenario is now the self-test two_lost_imports_cannot_share_one_gained_target, which reports no blocker on the previous commit and a TARGET-CHANGED blocker now.
| elif isinstance(node, (ast.AnnAssign, ast.AugAssign)): | ||
| if name in targets(node.target): | ||
| found.append("def") |
There was a problem hiding this comment.
Exclude annotation-only declarations from definition bindings
When the destination contains only an annotation such as helper: int, this branch reports a definition even though Python does not bind helper. Removing the old definition and re-pointing a function-local import to that destination consequently qualifies as a move and suppresses TARGET-CHANGED, although executing the import raises ImportError. I reproduced this with an annotation-only destination and received only INFO findings. Require an AnnAssign to have a value before treating it as a definition eligible for the move exemption.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in ecb8e4e. A bare annotation (helper: int) no longer counts as a binding; an AnnAssign needs a value, and an augmented assignment is treated as ambiguous rather than a definition. Removing the old definition and re-pointing a local import at an annotation-only destination passed on the previous commit and now fails with TARGET-CHANGED.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ecb8e4e284
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if try_like or isinstance( | ||
| node, (ast.If, ast.For, ast.AsyncFor, ast.While, ast.With, ast.AsyncWith) | ||
| ): | ||
| for field in ("body", "orelse", "finalbody"): | ||
| visit(getattr(node, field, []) or []) |
There was a problem hiding this comment.
Keep guarded definitions ineligible for the move exemption
When the destination defines helper only inside if TYPE_CHECKING, this unconditional traversal still classifies it as a runtime definition. Removing the old definition and redirecting a function-local import to that destination consequently suppresses TARGET-CHANGED, even though calling the function now raises ImportError. I reproduced this against the reviewed commit: compare() emits only INFO findings, while executing the destination module leaves helper unbound. Track guarded bindings separately and conservatively retain the blocker unless the destination is known to bind the name at runtime.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 0719bf4. A binding found inside any nested block at module level (if TYPE_CHECKING:, other if branches, try / except / finally, loops, with, match cases) is now reported as ambiguous, so only an unconditional top-level binding can qualify a destination for the move exemption. Your case (old definition removed, local import re-pointed at a module that defines helper only under TYPE_CHECKING) passed on the previous commit and now fails with TARGET-CHANGED; #15596 still reports 63 moved imports and no blockers.
…qualify as a moved definition
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0719bf440c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| elif isinstance(node, ast.ImportFrom): | ||
| for alias in node.names: | ||
| if (alias.asname or alias.name) == name: |
There was a problem hiding this comment.
Treat wildcard imports as potentially shadowing definitions
When a module defines helper and subsequently executes from .actual import *, the wildcard import can overwrite helper, but this scan ignores it because the alias name is *. Unlike the previously fixed explicit-import case, this wildcard case still reproduces against the reviewed commit: moving the shadowed definition into pkg.new, removing it and the wildcard import from pkg.old, and redirecting the caller produces only INFO findings, although the caller's runtime result changes from 2 to 1. Conservatively mark wildcard-import bindings as ambiguous unless their exports are resolved, so this case retains the TARGET-CHANGED blocker.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 7dd8fce. A top-level from ... import * (including one inside a guarded block) now records an ambiguous binding for every name, since its exports are not resolved, so a module with a star import never qualifies as the old or new side of a move. Your case (def helper later shadowed by from .actual import *, then relocated and re-pointed) passed on the previous commit and now fails with TARGET-CHANGED; #15596 still reports 63 moved imports and no blockers.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7dd8fce152
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| visit(tree.body) | ||
| if not found: | ||
| return None | ||
| return found[0] if len(found) == 1 else "ambiguous" |
There was a problem hiding this comment.
Account for deleted names before granting the move exemption
When the destination defines helper and subsequently executes del helper, this scan still returns "def" because it ignores ast.Delete. Removing the old definition and redirecting a function-local import to that destination therefore suppresses TARGET-CHANGED, even though the new import raises ImportError. I reproduced compare() emitting only INFO findings while executing the destination left helper unbound. Treat deletion of the queried name, including deletion inside guarded blocks, as ambiguous so it cannot qualify for the move exemption.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 4f6cca5. del helper at module level, including inside guarded blocks, now records an ambiguous binding. While here I closed the other rebinding routes the statement scan could not see: a global helper declaration in any function of the module, and a module-level walrus (helper := ...), both make the name ambiguous. Your case (destination defines helper then deletes it) passed on the previous commit and now fails with TARGET-CHANGED; #15596 still reports 63 moved imports and no blockers.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4f6cca58cb
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| for handler in node.handlers: | ||
| guarded(handler.body) | ||
| if isinstance(node, getattr(ast, "Match", ())): | ||
| for case in node.cases: | ||
| guarded(case.body) |
There was a problem hiding this comment.
Include exception and pattern capture targets in binding checks
When a destination defines helper but also uses except ValueError as helper or a match capture such as case helper:, scanning only the block bodies misses those bindings. The exception target is deleted after the handler executes, while the match capture overwrites the function. I reproduced both cases: _top_level_binding() returns "def", and redirecting a function-local import from the old module produces only INFO findings, although the destination leaves helper unbound or bound to an integer. Mark matching exception targets and pattern captures as ambiguous before granting the move exemption.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 2a9b057, and generalised rather than adding one more case. _top_level_binding now also counts every module-scope binding site of the name: assignment, del, for and with targets, walrus, except ... as, match captures (case name, *name, **name), imports including import *, def / class, decorators and defaults (which run at module scope), and global name in any function. Function and class bodies are skipped since they bind locally. More than one site makes the name ambiguous, so it cannot qualify for the move exemption. Both of your cases (except ValueError as helper and case helper: after the definition) passed on the previous commit and now fail with TARGET-CHANGED; #15596 still reports 63 moved imports and no blockers.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2a9b057a3c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if isinstance(child, ast.Lambda): | ||
| continue |
There was a problem hiding this comment.
Inspect lambda defaults for module-scope rebinding
When a module defines helper and then evaluates callback = lambda value=(helper := 42): value, the default expression overwrites the module-level helper, but skipping the entire lambda hides that binding. I reproduced _top_level_binding() returning "def" and compare() emitting only INFO findings after moving the original definition into this destination and redirecting the local import, although calling the imported helper now raises TypeError. Inspect lambda defaults in the enclosing scope while continuing to exclude the lambda body.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 53923e2. The binding-site walk now visits a lambda's arguments (defaults and annotations, evaluated at module scope) and still skips its body, which is its own scope. callback = lambda value=(helper := 42): value after def helper returned "def" on the previous commit and now makes helper ambiguous, so the move exemption no longer applies; a walrus inside the lambda body and a lambda parameter named helper are still not counted. #15596 still reports 63 moved imports and no blockers.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 53923e2ca2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| for _ in range(level - 1): | ||
| base = base.parent |
There was a problem hiding this comment.
Reject relative imports that climb beyond the package root
When a re-pointed import has too many leading dots, PurePosixPath.parent silently stops at . instead of rejecting the traversal, allowing an invalid import to qualify as a definition move. For example, moving helper from pkg/old.py to root-level new.py and changing pkg/client.py from from .old import helper to from ...new import helper makes _git_moved() return true and compare() emit only INFO findings, although Python raises ImportError: attempted relative import beyond top-level package. Validate the relative-import depth before generating candidates and retain TARGET-CHANGED when it exceeds the package boundary.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in fd31e8d. _module_paths now returns no candidates when a relative import's climb reaches the repo root (or would go past it), since the root is not a package and Python raises attempted relative import beyond top-level package there. An unresolvable destination cannot satisfy the move predicate, so your case (from .old import helper re-pointed to from ...new import helper with helper moved to a root-level new.py) passed on the previous commit and now fails with TARGET-CHANGED. In-package climbs such as from ..x import y in pkg/sub/mod.py still resolve to pkg/x.py, and #15596 still reports 63 moved imports and no blockers.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Source lintblocks refactors that move helpers into a new module, becausescripts/verify_import_hoist.pyreads every re-pointed function-local import as a rename clash. Two open PRs are stuck on it today:llama_cpp.py): 63TARGET-CHANGEDblockers, e.g.'_can_reset_epoch' ... ['from:core.inference.llama_cpp:_can_reset_epoch'] -> ['from:core.inference.chat_compaction:_can_reset_epoch'].resolve_local_single_fileintodiffusion_local_files.py): 2 of the same.Cause
TARGET-CHANGEDexists to catch a same-name load that now resolves to a different object. Its only exemption is a module-level import removed and re-added in the same file. A refactor that moves a definition and updatesfrom old import Ninside a function tofrom new import Nbinds the same object, but neither import is module-level, so it is reported as a clash.Change
compare()takes an optionalmoved(old_target, new_target)predicate.main()builds it from git: modules are resolved to files at the before and after refs, the same way the importing file would see them (relative imports against its package, absolute ones against its ancestor directories). ATARGET-CHANGEDis downgraded to an INFOMOVEDline only when every lostfrom:old:Npairs with exactly one gainedfrom:new:Nof the same name, and either:from ... importre-exports at their own ref (the old and new module re-export it); orNwas defined at the top level of the old module before, is defined at the top level of the new module after, and the old module no longer defines it, at most re-importing it from the new module.Anything else stays a blocker: a re-point to a different
N, the old module keeping its own definition, or an alias rename across modules. The module-level relocation rule, theHOISTED-IMPORT-UNUSEDcheck and its__init__.py-only__all__exemption are unchanged.When a re-point does not count as a move
A re-pointed import keeps its TARGET-CHANGED blocker unless every one of the following holds:
The last point treats a binding as ambiguous when any of these appears for the name:
if/try/with/ loop /matchblock, includingTYPE_CHECKING;del,global,:=(including in a lambda default),except ... as, or a match capture;import *.Verification
--self-test: all cases pass, including three new ones (a re-point after a move, the same re-point when nothing moved, an alias rename across modules).MOVED.HOISTED-IMPORT-UNUSEDre-export indiffusion.py, which the existing rule keeps on purpose.