Skip to content

refactor(python): compute relative-import dot prefix explicitly#4810

Merged
dbrattli merged 1 commit into
mainfrom
refactor/python-relative-import-dots
Jul 17, 2026
Merged

refactor(python): compute relative-import dot prefix explicitly#4810
dbrattli merged 1 commit into
mainfrom
refactor/python-relative-import-dots

Conversation

@dbrattli

Copy link
Copy Markdown
Collaborator

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's relativePath so the fix is structural rather than incidental.

What changed

The old code mapped each .. segment to its own dot and relied on String.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.api instead of ...sibling.api for 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:

let markers, names = parts |> Array.partition (fun p -> p = "." || p = "..")
let prefix =
    if isLibrary && markers.Length > 0 then
        let parents = markers |> Array.filter ((=) "..") |> Array.length
        String.replicate (parents + 1) "."
    else ""

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 output
["."; "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

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>
@dbrattli
dbrattli merged commit f116866 into main Jul 17, 2026
31 checks passed
@dbrattli
dbrattli deleted the refactor/python-relative-import-dots branch July 17, 2026 18:19
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.

[Python] Nested Fable package imports contain too many leading dots

1 participant