Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call typechecker from isolated frame #260

Merged
merged 3 commits into from
Oct 23, 2024

Conversation

ojw28
Copy link
Contributor

@ojw28 ojw28 commented Oct 23, 2024

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 and param_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.

@patrick-kidger patrick-kidger merged commit a831de6 into patrick-kidger:main Oct 23, 2024
1 check passed
@patrick-kidger
Copy link
Owner

Yup, this makes sense to me / LGTM! Merged.

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.

2 participants