Skip to content

Import hoist verifier: a local import re-pointed after its definition moved is not a clash - #15622

Merged
danielhanchen merged 11 commits into
mainfrom
ci-import-hoist-moved-definitions
Oct 28, 2026
Merged

danielhanchen merged 11 commits into
mainfrom
ci-import-hoist-moved-definitions

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Source lint blocks refactors that move helpers into a new module, because scripts/verify_import_hoist.py reads every re-pointed function-local import as a rename clash. Two open PRs are stuck on it today:

Cause

TARGET-CHANGED exists 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 updates from old import N inside a function to from new import N binds the same object, but neither import is module-level, so it is reported as a clash.

Change

compare() takes an optional moved(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). A TARGET-CHANGED is downgraded to an INFO MOVED line only when every lost from:old:N pairs with exactly one gained from:new:N of the same name, and either:

  • both imports reach one definition, following top-level from ... import re-exports at their own ref (the old and new module re-export it); or
  • N was 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, the HOISTED-IMPORT-UNUSED check 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 old and new targets name the same symbol, and each lost import pairs one-to-one with a gained import;
  • both modules resolve to exactly one file, meaning an absolute import is not ambiguous between roots, and a relative import does not climb to or past the repo root;
  • the destination did not bind the name before the change, and binds it exactly once now, unconditionally at module level.

The last point treats a binding as ambiguous when any of these appears for the name:

  • a second binding site;
  • a binding inside an if / try / with / loop / match block, including TYPE_CHECKING;
  • a bare annotation;
  • del, global, := (including in a lambda default), except ... as, or a match capture;
  • import *.

Verification

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T02:34:09.107605Z fd31e8d Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +1055 to +1059
# 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +1004 to +1008
return [f"{stem}.py", f"{stem}/__init__.py"]
rel = module.replace(".", "/")
roots = [importer_dir, *importer_dir.parents]
out = []
for root in roots:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread scripts/verify_import_hoist.py Outdated
Comment on lines +972 to +976
if (
isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef))
and node.name == name
):
return "def"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread scripts/verify_import_hoist.py Outdated
Comment on lines +571 to +576
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread scripts/verify_import_hoist.py Outdated
Comment on lines +990 to +992
elif isinstance(node, (ast.AnnAssign, ast.AugAssign)):
if name in targets(node.target):
found.append("def")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread scripts/verify_import_hoist.py Outdated
Comment on lines +1025 to +1029
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 [])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread scripts/verify_import_hoist.py Outdated
Comment on lines +1007 to +1009
elif isinstance(node, ast.ImportFrom):
for alias in node.names:
if (alias.asname or alias.name) == name:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread scripts/verify_import_hoist.py Outdated
Comment on lines +1045 to +1048
visit(tree.body)
if not found:
return None
return found[0] if len(found) == 1 else "ambiguous"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +1037 to +1041
for handler in node.handlers:
guarded(handler.body)
if isinstance(node, getattr(ast, "Match", ())):
for case in node.cases:
guarded(case.body)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +1073 to +1074
if isinstance(child, ast.Lambda):
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +1113 to +1114
for _ in range(level - 1):
base = base.parent

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: fd31e8dfff

ℹ️ 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".

@danielhanchen
danielhanchen merged commit e7bf2e9 into main Oct 28, 2026
0 of 15 checks passed
@danielhanchen
danielhanchen deleted the ci-import-hoist-moved-definitions branch October 28, 2026 02:40
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