Skip to content

gh-77371: Clarify that types.new_class() kwds are class keywords - #161890

Open
aribaskagan wants to merge 1 commit into
python:mainfrom
aribaskagan:gh-77371-new-class-docstring
Open

aribaskagan wants to merge 1 commit into
python:mainfrom
aribaskagan:gh-77371-new-class-docstring

Conversation

@aribaskagan

@aribaskagan aribaskagan commented Nov 12, 2026 •

Copy link
Copy Markdown

Fixes #77371.

help(types.new_class) only said "Create a class object dynamically using the appropriate metaclass." Callers coming from type(name, bases, dict) were treating kwds as that namespace. As discussed on the issue, kwds is the keywords of the class statement, and the body is filled in by exec_body. Doc/library/types.rst already says this, so it is unchanged. prepare_class() gets the same one-sentence note.

Before:

Create a class object dynamically using the appropriate metaclass.

After:

Create a class object dynamically using the appropriate metaclass.

*name*, *bases*, and *kwds* are the class name, the base classes,
and the keywords of the equivalent class definition (such as
'metaclass').  *kwds* is not a namespace like the third argument
of type().  For example:

    new_class('Test', (A, B), {'metaclass': Meta, 'foo': 42})

is equivalent to:

    class Test(A, B, metaclass=Meta, foo=42):
        pass

To populate the namespace, pass *exec_body*, a callable that takes
the newly created dictionary and populates it.  For example:

    new_class('C', (MyABC,), {},
              exec_body=lambda ns: ns.update(namespace))

AI assistance was used in preparing this change; I reviewed and tested it.

kwds corresponds to keywords in the class statement, such as
metaclass, rather than the namespace dictionary accepted by type().
The class body is filled in by exec_body.  Note the same distinction
on prepare_class().

Co-authored-by: Kağan Arıbaş <aribaskagan@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 17:47
@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@bedevere-app

bedevere-app Bot commented Nov 12, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T17:50:55.047307Z adf3991 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: adf39919c7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Lib/types.py
class Test(A, B, metaclass=Meta, foo=42):
pass

To populate the namespace, pass *exec_body*, a callable that takes

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Describe exec_body's argument as a namespace mapping

With a metaclass whose __prepare__ returns a valid non-dict mapping, prepare_class() returns that object and new_class() passes it to exec_body; a mapping need not implement dictionary methods such as .update(), so calling it a “dictionary” can mislead callbacks that use dict-only methods, and the example immediately below uses .update(). Please use “prepared namespace” or “mapping,” as Doc/library/types.rst does.

AGENTS.md reference: AGENTS.md:L14-L14

Useful? React with 👍 / 👎.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

problem with ABCMeta.__prepare__ when called after types.new_class

2 participants