python / python/cpython

inspect.getframeinfo forces all lazy modules to be loaded

Open
#139,669 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-importlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Create two files as follows:

main.py:

import importlib.util
import inspect
import sys

# https://docs.python.org/3/library/importlib.html#implementing-lazy-imports
def lazy_import(name):
    spec = importlib.util.find_spec(name)
    loader = importlib.util.LazyLoader(spec.loader)
    spec.loader = loader
    module = importlib.util.module_from_spec(spec)
    sys.modules[name] = module
    loader.exec_module(module)
    return module
    
lazy_import('m')

f = inspect.currentframe()
inspect.getframeinfo(f)

m.py:

print("I'm loaded!")

Now run:

$ python main.py 
I'm loaded!

This behavior doesn't make much sense, since f isn't even inside m.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs
  • gh-151006

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 with the main.py and m.py reproducer, then trace inspect.getframeinfo(f) and the lazy-import setup shown in the report. Done means calling inspect.getframeinfo on a frame outside m.py no longer prints "I'm loaded!"; note that linked PR gh-151006 indicates work is already underway.

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
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.