spec: clarify that passthrough __new__ is ignored when converting to callable#2314
Open
ashishpatel26 wants to merge 1 commit into
Open
Conversation
…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.
| 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 |
Member
There was a problem hiding this comment.
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2158.
The spec's step 2 for converting a constructor to a
Callabletype said that a__new__method is always synthesized into a callable. The example for classB(which has__new__(cls, *args, **kwargs) -> Selfand__init__(self, x: int) -> None) incorrectly showed:However, the conformance test (
Class3) already specified the correct behaviour:When
__new__uses only*argsand**kwargsas its non-clsparameters, 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 thereveal_typecomment for classB.