Call typechecker from isolated frame #260
Merged
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.
The version of TypeGuard that JaxTyping depends on grabs the calling frame's locals using
sys._getframe(1).f_locals
, and returns a wrapping function that holds a reference to these locals. JaxTyping currently calls the typechecker from a frame that contains some locals. Of these,full_fn
andparam_fn
are problematic because they, in combination with what TypeGuard is doing, end up creating reference cycles.This commit calls the typechecker from an isolated frame in JaxTyping. This means
f_locals
of the calling frame doesn't contain anything that can end up creating cycles.It's somewhat debatable whether this should be considered a TypeGuard problem, but I think it would be tricky to fix there. TypeGuard appears to need a mutable version of the locals to see forward defined variables, so it's not viable just to copy them and remove anything that's problematic. Even if it were, TypeGuard doesn't really know which locals are problematic and not required. So pragmatically, changing JaxTyping to guard against the issue feels like the correct thing to do.