You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Imports in a finally or else clause are lazy under __lazy_modules__ #162573
The docs say "Imports inside functions, class bodies, or try/except/finally blocks are always eager, regardless of __lazy_modules__" (Compatibility via __lazy_modules__), and lazy import in a finally or else clause is a SyntaxError ("lazy import not allowed inside try/except blocks"). Imports in the try and except bodies are eager, but one in the finally clause, on the normal path, or in the else clause of a try without finally is lazy:
I expected module module. The normal-path copy of the finally body (codegen_try_finally in Python/codegen.c) and the else body are compiled outside the fblocks that _PyCompile_InExceptionHandler checks. I found it while writing a Lean spec of Python.
I would like to work on this #162573 issue. I plan to reproduce the behavior on the CPython Fork branch, investigate the compiler handling of imports in else and finally clauses, and implement a fix with regression tests. Please let me know if anyone is already working on this.
Thank you!
Activity
vyankateshk97 commentedon Nov 25, 2026
Hi @Robertboy18,
I would like to work on this #162573 issue. I plan to reproduce the behavior on the CPython Fork branch, investigate the compiler handling of imports in else and finally clauses, and implement a fix with regression tests. Please let me know if anyone is already working on this.
Thank you!