Yes. Exactly this. We see this occur over and over.
Here is a concrete proposal to kick off the conversation about how to complete the Argument Clinic and stop having to live with an incomplete tool.
Given that our documentation DSL can already describe optional arguments getattr(object, name[, default])
and that the type annotation DSL can already describe the function using overloads, let’s modify arg clinic to be able to describe and generate code for a union of signatures:
/*[clinic input]
getattr as builtin_getattr2
object: object
name: str
/
getattr as builtin_getattr3
object: object
name: str
default: object
/
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.
[clinic start generated code -- signature 1]*/
static PyObject *
builtin_getattr2(PyObject *module, PyObject *object, PyString_Object *str)
/*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
[clinic start generated code -- signature 2]*/
static PyObject *
builtin_getattr3(PyObject *module, PyObject *object, PyString_Object *str, PyObject *default)
/*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
Having a union of signatures would work super well for functions where None
can’t be used like getattr
and dict.pop
for cases where None
is merely undesirable such as type(object)
vs type(name, bases, dict, **kwds)
or range(stop)
vs range(start, stop[, step])
.