Skip to content

Propagate __future__ compiler flags in exec/eval/compile replacements#1008

Merged
emeryberger merged 3 commits intomasterfrom
fix-issue-1007-future-flags-in-replacement-exec
Feb 24, 2026
Merged

Propagate __future__ compiler flags in exec/eval/compile replacements#1008
emeryberger merged 3 commits intomasterfrom
fix-issue-1007-future-flags-in-replacement-exec

Conversation

@emeryberger
Copy link
Member

@emeryberger emeryberger commented Feb 23, 2026

Summary

  • Fixes the exec/eval/compile replacement wrappers to preserve __future__ compiler flags from the real caller's context
  • Without this fix, code using from __future__ import annotations that calls exec() with a string source gets compiled without the annotations flag, causing type annotations to be evaluated at runtime instead of being kept as strings
  • Applies the fix uniformly to all three wrappers (exec, eval, compile)

Test plan

  • All 256 existing tests pass
  • Install SQLAlchemy 2.0.40 and verify scalene run --profile-all --profile-exclude site-packages scatest.py no longer raises NameError: name 'DBAPIConnection' is not defined

Fixes #1007

🤖 Generated with Claude Code

emeryberger and others added 3 commits February 6, 2026 14:54
…#1007)

The replacement wrappers for exec(), eval(), and compile() break the
implicit compiler flag inheritance chain. When code with
`from __future__ import annotations` calls exec() with a string source,
the wrapper recompiles it without the annotations flag, causing type
annotations to be evaluated at runtime instead of being kept as strings.

Fix all three wrappers to extract __future__ flags from the real caller's
code object and pass them explicitly to compile() with dont_inherit=True.

Fixes #1007

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@emeryberger emeryberger force-pushed the fix-issue-1007-future-flags-in-replacement-exec branch from 5a44086 to 70af16c Compare February 23, 2026 22:21
@emeryberger emeryberger merged commit 4b0fad8 into master Feb 24, 2026
46 checks passed
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.

Cannot import SQLAlchemy due to incorrect compile flags

1 participant