Skip to content

gh-87613: Name the subclass in Argument Clinic __new__ argument errors - #160799

Closed
cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:clinic_new_tp_name
Closed

cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:clinic_new_tp_name

Conversation

@cmaloney

@cmaloney cmaloney commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

The tp_new slot is inherited. Because the base type name was embedded in the errors this meant argument errors showed as the base type rather than the subclass. Change them to be the subclass name.

While tp_name would be slightly faster to use it includes the module name like _thread.RLock() which adds confusion. Use _PyType_Name(type) which returns RLock(). Include a test to check.

_PyArg_NoPositional() only short-circuits when args is NULL, which tp_new never gets; use _PyArg_NoPositionalStack() to keep the name lookup off the success path.

The vectorcall cases keep using constant strings since that slot does not inherit.


This is needed to keep parity moving frozenset to Argument Clinic I think in general it makes these error messages a little better for new.

…nt errors

tp_new is inherited, so argument errors from a subclass named the base
type. While `tp_name` would be slightly faster to use it includes the
module name like `_thread.RLock()` which adds confusion. Use
`_PyType_Name(type)` which returns `RLock()`. Include a test to check.

_PyArg_NoPositional() only short-circuits when args is NULL, which
tp_new never gets; use _PyArg_NoPositionalStack() to keep the name
lookup off the success path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cmaloney

Copy link
Copy Markdown
Contributor Author

Changed my mind on this, pure python is the other way and I'd argue AC should match that:

>>> class Base:
...     def __new__(cls, a): ...
... class Sub(Base): pass
... 
... Sub(1, 2)  # Base.__new__() takes 2 positional arguments but 3 were given
... Sub(x=1)   # Base.__new__() got an unexpected keyword argument 'x'
... 
Traceback (most recent call last):
  File "<python-input-0>", line 5, in <module>
    Sub(1, 2)  # Base.__new__() takes 2 positional arguments but 3 were given
    ~~~^^^^^^
TypeError: Base.__new__() takes 2 positional arguments but 3 were given

@cmaloney cmaloney closed this Oct 28, 2026
@cmaloney
cmaloney deleted the clinic_new_tp_name branch October 28, 2026 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant