Skip to content

gh-62534: Document that three-argument type() does not call __prepare__#154028

Open
soreavis wants to merge 1 commit into
python:mainfrom
soreavis:gh-62534-type-prepare-note
Open

gh-62534: Document that three-argument type() does not call __prepare__#154028
soreavis wants to merge 1 commit into
python:mainfrom
soreavis:gh-62534-type-prepare-note

Conversation

@soreavis

@soreavis soreavis commented Jul 18, 2026

Copy link
Copy Markdown

The three-argument form of type() skips the metaclass __prepare__ method — __prepare__ is called by the class statement machinery, not by the metaclass call itself — so a class created with type('C', (Parent,), {}) silently misses anything a base's metaclass prepares. The entry currently calls the form "essentially a dynamic form of the class statement" and documents the keyword-argument metaclass machinery, but never mentions this difference.

Add the note the issue thread settled on: state that the three-argument form does not call __prepare__, and point to types.new_class() for dynamic class creation with the appropriate metaclass. Raising instead was considered and rejected in the thread, since a metaclass's own __new__ legitimately calls type(name, bases, ns).

Verified at tip: a subclass created via the class statement or types.new_class() gets the prepared namespace; the same bases through three-argument type() do not.

…repare__

The three-argument form of type() skips the metaclass __prepare__
method, which is called by the class statement machinery rather than
by the metaclass call itself. Say so in the type() entry and point to
types.new_class() for dynamic class creation with the appropriate
metaclass, as directed in the issue thread.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33648983 | 📁 Comparing b0be598 against main (a1d5804)

  🔍 Preview build  

1 file changed
± library/functions.html

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

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant