Skip to content

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

Merged
encukou merged 1 commit into
python:mainfrom
soreavis:gh-62534-type-prepare-note
Jul 19, 2026
Merged

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

Conversation

@soreavis

@soreavis soreavis commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

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

@encukou
encukou merged commit 5625b18 into python:main Jul 19, 2026
36 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Docs PRs Jul 19, 2026
@encukou

encukou commented Jul 19, 2026

Copy link
Copy Markdown
Member

Thanks for fixing this long-standing omission!

@encukou encukou added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jul 19, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @soreavis for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app

Copy link
Copy Markdown

Thanks @soreavis for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app

Copy link
Copy Markdown

Thanks @soreavis for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jul 19, 2026

Copy link
Copy Markdown

GH-154164 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jul 19, 2026
@bedevere-app

bedevere-app Bot commented Jul 19, 2026

Copy link
Copy Markdown

GH-154165 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 19, 2026
@bedevere-app

bedevere-app Bot commented Jul 19, 2026

Copy link
Copy Markdown

GH-154166 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants