Repository navigation
gh-162597: Mangle a private type parameter used before its definition - #162842
JakeHuneau wants to merge 4 commits into
Conversation
…nition The symbol table added a generic class's type parameters to the set of names to mangle one at a time, as it visited them. A bound, constraint or default that referred to a later private type parameter was therefore recorded unmangled, while the compiler mangled it with the complete set, found no symbol and evaluated the bound with LOAD_NAME in a scope without locals, raising SystemError. Add all the type parameter names to the set before visiting any of them.
|
What about the reproducer on the issue? why changing that one? |
|
Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate. Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors. This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness. Please read our guidelines for using AI tools. |
| def test_later_type_params_are_mangled(self): | ||
| # gh-162597: a bound, constraint or default that refers to a later | ||
| # private type parameter. | ||
| class C[T: __U, __U]: |
There was a problem hiding this comment.
I'd suggest using run_code as well.
| case TypeVar_kind: | ||
| name = tp->v.TypeVar.name; | ||
| break; | ||
| case TypeVarTuple_kind: |
The symbol table added a generic class's type parameters to
ste_mangled_namesone at a time as it visited them. Inclass C[T: __U, __U]the bound ofTis visited before__Uis in the set, so the bound's scope recorded__Uunmangled. The compiler then mangles with the complete set (_Py_MaybeMangle), looks up_C__U, finds no symbol and emitsLOAD_NAMEin the bound's function scope, which raisesSystemError: no locals found.All the type parameter names are now added to the set before any of them is visited, so a bound, constraint or default that refers to a later private type parameter resolves to it, the same as it already does for an earlier one or for a non-private name. The per-name add in
symtable_add_def_ctxwas only reached for these type parameters, so it is replaced by the new helper.The new test covers a bound, a constraint and defaults (including a
ParamSpec) that refer to later private type parameters, with a module-level__Upresent. It fails with the reportedSystemErrorwithout the fix.test_type_params,test_symtable,test_compile,test_syntax,test_typing,test_scope,test_dis,test_astand the annotation tests pass.SystemError: no locals foundwhen a type parameter bound names a later private type parameter of a class #162597