Skip to content

Private names in the bases of a generic class nested in a class are not mangled #162574

Backlog

Description

@Robertboy18

Bug report

Bug description:

In a class body, class Plain(__Base) finds the outer class's _Outer__Base, but with type parameters the same base is looked up unmangled:

class Outer:
    __Base = dict
    class Plain(__Base): pass
    print(Plain.__bases__)
    try:
        class Generic[T](__Base): pass
        print(Generic.__bases__)
    except NameError as e:
        print("NameError:", e)
(<class 'dict'>,)
NameError: name '__Base' is not defined

__Base occurs textually in the definition of Outer, so it's a private name of Outer (Private name mangling), and adding [T] to the nested class shouldn't change how its bases resolve. Since gh-119311, _Py_MaybeMangle (Python/symtable.c) leaves every name in the type parameter scope of a generic class unmangled except the type parameters, so it is no longer mangled with the new class's name, but not with the enclosing class's either. The same happens for bounds and class keywords. I found it while writing a Lean spec of Python.

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux

Activity

  1. added
    interpreter-core
    type-bug
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-coretopic-typingtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions