refactor(python): compute relative-import dot prefix explicitly#4810
Merged
Conversation
Rewrite MakeImportPath's relativePath so the leading-dot count is computed directly (one dot per parent level plus one for the package) instead of emerging from String.concat "." separators. The old approach mapped every ".." segment to its own dot and relied on the join separator to add another, which produced one dot too many for imports two or more levels up (e.g. "....sibling.api" instead of "...sibling.api"). Also adds a docstring with worked examples. This is a follow-up cleanup to #4803, which fixed the same off-by-one with a minimal one-line change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up cleanup to #4803 (which fixed #4797).
#4803 fixed an off-by-one in the number of leading dots for relative imports of nested files with a minimal one-line change. This PR refactors
MakeImportPath'srelativePathso the fix is structural rather than incidental.What changed
The old code mapped each
..segment to its own dot and relied onString.concat "."inserting a separator per join to add the rest — so the leading-dot count emerged from the join rather than being computed. That coupling is exactly what produced the original off-by-one (....sibling.apiinstead of...sibling.apifor imports two levels up).The refactor computes the prefix explicitly — one dot for the package, plus one per parent level (
..) — and joins only the module-name segments:It also adds a docstring with worked examples.
Behavior
This is a behavioral no-op relative to
main(i.e. relative to #4803): identical output wherever the old code was correct, and the fix is correct at any nesting depth by construction. Verified against representative inputs:parts["."; "module"].module[".."; "sibling"; "api"]..sibling.api[".."; ".."; "sibling"; "api"]...sibling.api[".."; ".."; ".."; "s"; "api"]....s.api[".."; "My.Dir"; "api"]..My_Dir.api(dir dots →_, #3079)🤖 Generated with Claude Code