Skip to content

gh-162597: Mangle a private type parameter used before its definition - #162842

Open
JakeHuneau wants to merge 3 commits into
python:mainfrom
JakeHuneau:gh-162597-typeparam-private-bound
Open

JakeHuneau wants to merge 3 commits into
python:mainfrom
JakeHuneau:gh-162597-typeparam-private-bound

Conversation

@JakeHuneau

Copy link
Copy Markdown

The symbol table added a generic class's type parameters to ste_mangled_names one at a time as it visited them. In class C[T: __U, __U] the bound of T is visited before __U is in the set, so the bound's scope recorded __U unmangled. The compiler then mangles with the complete set (_Py_MaybeMangle), looks up _C__U, finds no symbol and emits LOAD_NAME in the bound's function scope, which raises SystemError: 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_ctx was 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 __U present. It fails with the reported SystemError without the fix. test_type_params, test_symtable, test_compile, test_syntax, test_typing, test_scope, test_dis, test_ast and the annotation tests pass.

…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.
@picnixz

picnixz commented Nov 30, 2026

Copy link
Copy Markdown
Member

What about the reproducer on the issue? why changing that one?

@picnixz picnixz added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 30, 2026
@bedevere-app

bedevere-app Bot commented Nov 30, 2026

Copy link
Copy Markdown

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.

Comment thread Lib/test/test_type_params.py Outdated
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]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest using run_code as well.

Comment thread Python/symtable.c
case TypeVar_kind:
name = tp->v.TypeVar.name;
break;
case TypeVarTuple_kind:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants