codemap icon indicating copy to clipboard operation
codemap copied to clipboard

python: 'from pkg import submodule' resolves to __init__.py, 'from . import name' drops the edge

Open JordanCoin opened this issue 1 month ago • 0 comments

Two related package-member resolution misses:

  1. from pkg import submodule resolves to pkg/__init__.py instead of pkg/submodule.py when submodule is a module file, not a name defined in __init__.py (yt-dlp: from yt_dlp import socks).
  2. from . import name / from .. import name, where name is defined in the package's __init__.py, yields no edge at all (spec-kit: 177 of its 260 false negatives target an __init__.py).

Python's rule: for from P import X, X is the submodule P/X.py (or P/X/) if it exists, otherwise a name from P/__init__.py.

Repro (codemap 4.7.0):

pkg/__init__.py:          def helper(): pass
pkg/socks.py
use_sub.py:               from pkg import socks
pkg/sub/__init__.py:      def g(): pass
pkg/sub/child.py:         from . import g
codemap --importers pkg/socks.py          # none, should be use_sub.py
codemap --importers pkg/sub/__init__.py   # none, should be pkg/sub/child.py

Found by the import-accuracy benchmark (#264). Part of #240.

JordanCoin avatar Nov 03 '26 18:11 JordanCoin