Repository navigation
Merge namespace package portions for from X import A, B - #11898
Megh Parikh (meghprkh) wants to merge 2 commits into
Conversation
A from-import of several submodules of a namespace package split across search paths (e.g. "from ns import a, b" with a and b in different portions) only kept the implicit imports of one portion, so the other symbols were reported as unknown import symbols. Merge the implicit imports of all namespace portions found during resolution, with the earliest portion winning for a shared submodule. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
On pinned |
🔴 Pyright CLI QA —
|
Only merge imports whose full path consists of namespace packages. Preserve the origin of merged implicit imports so typed third-party packages retain private-export diagnostics.
|
Thanks Heejae Chang (@heejaechang) I have pushed a commit, however at this point I am fairly out of comfort with the changes being made, please let me know if this works or I will close the PR as at this point its 100% AI-ed with me just acting as a proxy (Especially since it changes an interface in |
Related: #7494, #9165, #10056
Problem
When a PEP 420 namespace package is split across several search paths
(
extraPaths, editable installs, site-packages), pyright reports"B" is unknown import symbolfor:Python accepts this, and pyright accepts the same imports on separate lines.
Import sorters such as isort/ruff merge those lines back together, so the
separate-line workaround doesn't stick.
Cause
_resolveBestAbsoluteImportalready resolves the import against every searchpath, but
_pickBestImportkeeps only one result, and so only one portion'simplicit imports (submodules).
_isNamespacePackageResolvedaccepts a portionif it provides any requested symbol, so the first portion containing
Awinsand
Bis lost.Change
While walking the search paths, collect results that are namespace portions
(found, namespace, no
__init__.py(i)in the final directory, not a stubpackage). If the best result is one of them, return a copy whose implicit
imports are the union of all portions' implicit imports. When several portions
provide the same submodule, the earliest one wins, matching runtime order.
each entry carries its own URI.
Tests
New cases in
importResolver.test.ts:Performance
No extra file system access. Absolute import results are cached per module
name, so a merged result also avoids repeated full resolutions: a cached
single-portion result that lacks the next requested symbol is currently
re-resolved against every search path.
Synthetic project: one namespace package split across 40
extraPathsroots(25 modules each), 200 user files (median of 5 runs, same commit with and
without this change):
from ns import mfrom ns import m1, …, m5from ns import *from ns import *passes an empty symbol set, which keeps all implicit imports,so it now pulls in the submodules of every portion rather than one. This was
already the behaviour for a single portion.
It also makes resolution independent of import order. Today the first file to
resolve
from ns import ...decides which portion is cached forns, so asubpackage installed in two search paths can be loaded from either depending on
which file imported it. With this change every file gets the same copy.