Skip to content

fix: a class body can hold a class - #278

Merged
jonruttan merged 1 commit into
mainfrom
fix/nested-class
Nov 29, 2026
Merged

jonruttan merged 1 commit into
mainfrom
fix/nested-class

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

What

A class defined directly in another class's body was a syntax error (unexpected token in expression):

class A:
    class B:
        y = 2

The class-body parser accepted only def, pass, nonlocal, NAME = ... and bare expressions. In lib/python/parse.x:

  • %py-class-methods-of binds a nested class, decorated or not, into the namespace the same way it binds a method. Both go through the new %py-class-entry-of.
  • %py-class-level-names counts a nested class's name as one the body binds, so later lines of the body can read it, as in w = B.y + 1.

weakref worked around the bug with a module-level _FinalizeInfo (#272). That workaround can stay.

Tests

  • New case in spec 73 (class bodies), expected output from python3.15. It covers a nested class used by the body and by a method, a decorated nested class with a base, a one-line class D: pass, and a nested body reading a module global rather than the outer class's name.
  • Full suite: 1309 tests, 0 failed. The if-ladder and path-literal checks pass.

Not here

  • __qualname__ doesn't exist on any class yet (A.B.__qualname__ raises AttributeError). It's a separate gap.

🤖 Generated with Claude Code

The class body parser took def, pass, nonlocal, NAME = and bare
expressions; a nested `class` fell to the expression parser and was a
syntax error.  A nested class, decorated or not, is now a row of the
namespace as a method is (%py-class-entry-of), and its name is one the
body binds at its own level.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonruttan
jonruttan merged commit 4a9e5a8 into main Nov 29, 2026
12 checks passed
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.

1 participant