Skip to content

Conversation

@makslevental
Copy link
Contributor

@makslevental makslevental commented Nov 20, 2025

After much consternation and flaggelation, I've decided we can't/shouldn't support generics for python < 3.12. The context/reason is that I thought I had robust support for just plain TypeVars as globals (and in the closure) but recently I've realized that that support is not robust.

In the weeds: the "generic" kernel instantiation effectively mutates the TypeVar and thus when the TypeVar is a global, multiple instantiations of the same kernel (with different concrete params) will race (on that global TypeVar). TypeVars in the closure don't suffer from this because we copy the body_builder and its closure when instantiating the kernel. In 3.12 (when using the brackets def fun[M, N, ...] syntax) the TypeVars are always in the closure1. I tried to figure out somehow to move globals into closures but failed and decided it's not worth the effort anyway.

Note, the added test (test_generics) is gated behind 3.13 because generics default value assignment is a 3.13 feature and the 3.12 parser will choke on it, but everything else works in 3.12.

Footnotes

  1. That's actually a CPython implementation detail - there's a compiler transform which rewrites the function into a closed over function with those TypeVars in its closure.

@makslevental makslevental force-pushed the users/makslevental/fix-more-func-stuff branch 2 times, most recently from 705420b to 436c277 Compare November 21, 2025 19:29
@makslevental makslevental changed the title [eudsl-python-extras] fix more func stuff [eudsl-python-extras] drop support for generics for py <= 3.12 Nov 21, 2025
@makslevental makslevental changed the title [eudsl-python-extras] drop support for generics for py <= 3.12 [eudsl-python-extras] drop support for generics for py < 3.12 Nov 21, 2025
@makslevental makslevental force-pushed the users/makslevental/fix-more-func-stuff branch 2 times, most recently from 8573df5 to 52e3983 Compare November 21, 2025 19:47
@makslevental makslevental marked this pull request as ready for review November 21, 2025 19:52
@makslevental makslevental force-pushed the users/makslevental/fix-more-func-stuff branch from 52e3983 to aaafedd Compare November 21, 2025 19:55
@makslevental makslevental force-pushed the users/makslevental/fix-more-func-stuff branch from aaafedd to dc3b71a Compare November 21, 2025 20:44
@makslevental makslevental merged commit bd00241 into main Nov 21, 2025
20 checks passed
@makslevental makslevental deleted the users/makslevental/fix-more-func-stuff branch November 21, 2025 20:57
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.

3 participants