python / python/cpython

Import of submodule shadowing parent module attribute fails under lazy_imports mode

Open
#151,208 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core topic-lazy-imports type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

For this module structure:

└─ a/
   ├─ __init__.py    # Contains a function foo()
   └─ foo.py

This script passes on 3.14...

from a import foo
import a.foo
import types
assert isinstance(a.foo, types.ModuleType), type(a.foo)

And fails on 3.15 with lazy imports (python3.15 -X lazy_imports=all script.py):

Traceback (most recent call last):
  File "/Users/jwalls/script.py", line 4, in <module>
    assert isinstance(a.foo, types.ModuleType), type(a.foo)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: <class 'function'>
CPython versions tested on:

3.15

Operating systems tested on:

macOS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided module-layout script with Python 3.15 and -X lazy_imports=all, then inspect the lazy-import handling involved in resolving a submodule that shadows a parent attribute. Done means the assertion sees a ModuleType for a.foo, matching the behavior on Python 3.14.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.