Skip to content

gh-88580: Run profile scripts in __main__ namespace - #155600

Draft
NithishKumar04 wants to merge 1 commit into
python:mainfrom
NithishKumar04:gh-88580-profile-main-module
Draft

gh-88580: Run profile scripts in __main__ namespace#155600
NithishKumar04 wants to merge 1 commit into
python:mainfrom
NithishKumar04:gh-88580-profile-main-module

Conversation

@NithishKumar04

@NithishKumar04 NithishKumar04 commented Aug 12, 2026

Copy link
Copy Markdown

Fixes #88580.

python -m profile executed the target script in a standalone globals
dictionary while sys.modules["__main__"] still referred to the profiler.
Dataclasses resolving deferred InitVar annotations through the class module
therefore inspected the wrong namespace and generated an invalid constructor.

Create a real __main__ module and execute the profiled script in its
namespace, matching direct script execution and cProfile. Add regression
coverage for the reported deferred-annotation case and a NEWS entry.

Tests:

  • make patchcheck
  • ./python -m test -v test_profile test_profiling.test_tracing_profiler
  • ./python -m test -v test_dataclasses test_pstats
  • ./python -m test -j8 (one unrelated macOS test_utime_fd timestamp failure;
    it passed on immediate isolated rerun)

The profile CLI executed script globals separately from the module stored in
sys.modules["__main__"]. Deferred dataclass annotations therefore resolved
against the profiler module and failed to recognize InitVar fields.

Create a real __main__ module and execute the profiled script in its namespace,
matching direct script execution and cProfile.
@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@StanFromIreland

StanFromIreland commented Aug 12, 2026

Copy link
Copy Markdown
Member

You must sign the CLA before we can review.

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.

from __future__ import annotations breaks profiler's handling of dataclasses

2 participants