Restore the typechecking flag when the body raises - #1031
Open
rakhimovv wants to merge 1 commit into
Open
Conversation
disable_typechecking() sets the process-global jaxtyping_disable flag and restores it after the yield. Without try/finally an exception escaping the body skips the restore, so the flag stays True for the life of the process and every @at.typecheck stops checking, silently. No behaviour change on the happy path: contextlib resumes the generator at the yield in both cases. Nesting still restores to the outer value, since initial is captured per entry. Adds one regression test.
rakhimovv
requested review from
Michael-Equi,
jimmyt857 and
kvablack
as code owners
August 31, 2026 20:11
jimmyt857
removed their request for review
August 31, 2026 20:46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
disable_typechecking()sets the process-globaljaxtyping_disableflag and restores it afterthe
yield. With notry/finally, an exception escaping the body skips the restore, so the flagstays
Truefor the life of the process and every@at.typecheckin the codebase stops checking —nothing raised, nothing logged.
Where this actually bites. Not the training scripts —
training/checkpoints.pyhas notry/exceptandscripts/train.pycallsrestore_stateat top level, so a failed restore endsthe process and the flag never outlives it. It bites in any process that catches and continues:
so later tests silently stop being typechecked
array_typing._check_dataclass_annotationsbreaks inside REPLs #596 / Fix array_typing breakage in REPL #604No behaviour change on the happy path.
contextlib._GeneratorContextManager.__exit__resumes thegenerator at the
yieldin both cases;return/breakout of the body already route through thenormal-exit path. Nesting still restores to the outer value, since
initialis captured per entry.This does not make the flag thread-safe — it is still process-global and restore is last-writer-wins;
this only fixes the leak on exception.
Test. One regression test, in
src/openpi/shared/array_typing_test.pyalongside the existingdownload_test.py/image_tools_test.py/normalize_test.py. It fails onmainand passes withthe fix. It compares against the flag's initial value rather than asserting a literal, so it is
unaffected by
JAXTYPING_DISABLEin the environment. Happy to drop it if you would rather keep thechange to the three lines.
CONTRIBUTING.md asks for an issue or discussion first — glad to move this to one if you prefer; it
seemed small enough to send directly.