classification
Title: Report details of excess arguments in object_new & object_init
Type: enhancement Stage: needs patch
Components: Versions:
process
Status: open Resolution:
Dependencies: 31506 Superseder:
Assigned To: Nosy List: ncoghlan, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-09-20 05:17 by ncoghlan, last changed 2017-09-20 06:14 by serhiy.storchaka.

Messages (3)
msg302591 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-09-20 05:17
object_new and object_init currently use their own argument checking logic, and hence haven't benefited from the error reporting enhancements in PyArg_ParseTupleAndKeywords

Compare:

>>> str(1, 2, 3, 4, 5, x=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: str() takes at most 3 arguments (6 given)
>>> str(x=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'x' is an invalid keyword argument for this function

To:

>>> C(1, 2, 3, 4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object() takes no parameters
>>> C(x=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object() takes no parameters

Issue 31506 amends the latter to at least report the subclass name rather than the base class name, but doesn't cover any other enhancements, like reporting how many arguments were actually given, or the first unknown keyword argument name.
msg302592 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-09-20 05:19
Adding a dependency on issue 31506, as this shouldn't be tackled until those simpler amendments are resolved.
msg302597 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-20 06:14
str(x=1) is not the best example since str() takes keyword arguments. Look at tuple() and list():

>>> tuple(x=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: tuple() takes no keyword arguments
>>> list(x=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list() takes no keyword arguments

Initial version of my patch for issue31506 reported error messages similar to tuple() and list(), but at end I simplified it since in any case the error message was not perfect.
History
Date User Action Args
2017-09-20 06:14:25serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg302597
2017-09-20 05:19:45ncoghlansetdependencies: + Improve the error message logic for object_new & object_init
messages: + msg302592
2017-09-20 05:17:57ncoghlancreate