FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

spec: clarify that passthrough __new__ is ignored when converting to callable by ashishpatel26 · Pull Request #2314 · python/typing · GitHub

/ typing Public

spec: clarify that passthrough __new__ is ignored when converting to callable - #2314

Open
ashishpatel26 wants to merge 1 commit into
python:mainfrom
ashishpatel26:fix/2158-constructor-callable-passthrough-new
Open

spec: clarify that passthrough __new__ is ignored when converting to callable#2314
ashishpatel26 wants to merge 1 commit into
python:mainfrom
ashishpatel26:fix/2158-constructor-callable-passthrough-new

Conversation

Copy link
Copy Markdown
Contributor

Summary

Fixes #2158.

The spec's step 2 for converting a constructor to a Callable type said that a __new__ method is always synthesized into a callable. The example for class B (which has __new__(cls, *args, **kwargs) -> Self and __init__(self, x: int) -> None) incorrectly showed:

reveal_type(accepts_callable(B))  # `def (*args, **kwargs) -> B | def (x: int) -> B`

However, the conformance test (Class3) already specified the correct behaviour:

reveal_type(r3)  # `def (x: int) -> Class3`

When __new__ uses only *args and **kwargs as its non-cls parameters, it is a passthrough that defers all parameter constraints to __init__. Step 2 of the conversion algorithm should produce no callable type in this case. This is the behaviour already implemented by pyright, pycroscope, and zuban (all pass the conformance test).

Changes

  • docs/spec/constructors.rst: Added passthrough exception to step 2; corrected the reveal_type comment for class B.

…callable

When __new__ uses only *args and **kwargs, it is a passthrough that does not
constrain the constructor signature. Step 2 of the conversion algorithm
should produce no callable type in this case, leaving __init__ as the sole
source of the constructor's signature.

The conformance test (Class3) already expected this behaviour; the spec
example for class B was inconsistent. Fixes python#2158.
srittau added the topic: typing spec For improving the typing spec label Jun 22, 2026
from a base class other than ``object``, a type checker should synthesize a
callable from the parameters and return type of that method after it is bound
to the class.
to the class. If the ``__new__`` method's only non-``cls`` parameters are

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why is this the right behavior? Also shouldn't there be requirements on the type annotations for the args/kwargs parameters?

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: typing spec For improving the typing spec

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spec and conformance tests disagree on converting constructors into callable for classes with __new__ and __init__

3 participants


Back | FazBrowse Home | New Git URL