Repository navigation
feat(lower): TypeVar/Generic annotation erasure + per-call-site monomorphization (T2-1) - #125
Merged
Merged
Conversation
…nomorphization
- T = TypeVar('T') declarations lower as no-ops; annotation uses of T
erase to the declared bound or, when unbound, an internal typevar
placeholder resolved at call sites.
- Variance keywords (covariant=/contravariant=) are ignored.
- Generic[T] bases are erased; the class lowers as if unparametrized
and records the declared typevars for specialization.
- Calls to generic functions unify argument types with the declared
typevars and emit a specialized copy per call site (ident.int).
- Construction/subscripts of user generic classes (Box[int], Box(1))
emit specialized records per instantiated type set (Box_int);
builtin aliases (list[int]) are untouched.
- Params specializing to none drop from the emitted signature and
bind as constants; class instantiation at none fences PS1003.
- Stays fenced: TypeVar/generic fn used as a value (PS2002), TypeVar
needing a str name or unsupported keywords (PS2003), unresolved
typevar locals (PS1001), inference conflicts and missing
instantiations (PS1003), isinstance on generic aliases.
Contributor
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
Deploying pyscriptc with
|
| Latest commit: |
2ad797d
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://b78d76fb.pyscriptc.pages.dev |
| Branch Preview URL: | https://work-t2-typevar-erasure.pyscriptc.pages.dev |
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.
Summary
Task T2-1 of
.agents/plans/2026-10-18-tiered-lowering-and-typefacts.md: TypeVar/Generic annotation erasure via per-call-site monomorphization (tier-2 typed compile-time lowering).T = TypeVar('T')declarations lower as no-ops (_declare_typevar); unboundTin annotations becomes an internaltypevarplaceholder that never reaches IR (_llvm_typetripwires on it),bound=erases to the bound,covariant=/contravariant=are ignored.Generic[T]bases are erased; the class records declared typevars ininfo["typevars"]and is skipped by the main class loop.self.generic_fnsare skipped by the sig loop; each call site unifies arg types against sig typevars (_unify) and emits a specialized copy (ident.int,ident.str) throughinst_queue/_drain_instances. Params specializing tononedrop from the signature and bind as consts.Box[int]/constructor calls emit specialized class records (Box_int) viaclass_queuewith substituted fields/methods; builtin aliases (list[int]) untouched.Fences (existing PS2xxx style; no existing messages reworded)
cannot infer {tv} for {name},{tv} is {a}, not {b}, bound violation,cannot instantiate {base} with noneChecks
tests/test_t2_typevar.py: 16 new tests incl. oracle-identical runs on int/str/None.uv run pytest: 575 passed; 4 failures reproduce identically onorigin/master(env Python 3.13 vs tests expecting 3.14; pre-broken py2-styleexceptinbenchmarks/compat2/cache.pyandexamples/gates_probe.py).uv run ruff checkandruff format --checkclean on changed files.Link to Devin session: https://app.devin.ai/sessions/4dc1d8884421473cb28bacb88891f6e0
Open in Devin Desktop: https://app.devin.ai/desktop/session/4dc1d8884421473cb28bacb88891f6e0?variant=devin
Requested by: @espetro