Feature
float() accepts any number of positional arguments in RustPython when the first one is an exact float, silently ignoring the extras, but CPython raises TypeError.
Reproduction
RustPython:
CPython:
TypeError: float expected at most 1 argument, got 2
Root cause
PyFloat::slot_new's fast path tests args.args.first(), which succeeds for any number of positional arguments, so float(1.5, True) returns the first argument and never reaches the args.bind(vm) call at L188 that would reject the extra one. CPython's clinic-generated float_new runs _PyArg_CheckPositional("float", PyTuple_GET_SIZE(args), 0, 1) before it fetches the first argument, so arity is validated first. The fast path was hoisted out of py_new into slot_new in 5f496c9 (#6398), which placed it ahead of argument binding, and the same commit gave PyInt::slot_new an args.args.len() == 1 guard that PyStr::slot_new and PyComplex::slot_new also carry and float does not.
Fix
Add args.args.len() == 1 to the fast path condition, matching PyInt::slot_new; the extra argument then falls through to args.bind(vm), where FuncArgs::bind raises TypeError. That message reads expected at most 1 arguments, got 2, so matching CPython's wording needs an explicit count check like PyFrozenSet::slot_new.
Environment
- RustPython d248a04 (Python 3.14.0), also reproduced on 0.5.0
- CPython v3.14.3
- OS: Debian 12
Python Documentation or reference to CPython source code
-
CPython float_new: Objects/clinic/floatobject.c.h#L218-L241 — the arity check runs before the first argument is fetched:
if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
!_PyArg_NoKeywords("float", kwargs)) {
goto exit;
}
if (!_PyArg_CheckPositional("float", PyTuple_GET_SIZE(args), 0, 1)) {
goto exit;
}
if (PyTuple_GET_SIZE(args) < 1) {
goto skip_optional;
}
x = PyTuple_GET_ITEM(args, 0);
-
CPython _PyArg_CheckPositional: Python/getargs.c#L2718-L2723 — raises the TypeError when nargs exceeds the declared maximum:
if (nargs > max) {
if (name != NULL)
PyErr_Format(
PyExc_TypeError,
"%.200s expected %s%zd argument%s, got %zd",
name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
-
The signature is documented as float(x=0, /) in the clinic docstring: Objects/clinic/floatobject.c.h#L209-L213
-
Documentation: builtins.float
Related arity-validation gaps in other builtin constructors: #8460, #8462.
AI disclosure: this report was drafted with Claude Code (claude-opus-5) and reviewed by me before submission. The divergence comes from a differential-testing run against CPython 3.14.3, and every source reference above was checked against the linked revisions.
Feature
float()accepts any number of positional arguments in RustPython when the first one is an exactfloat, silently ignoring the extras, but CPython raisesTypeError.Reproduction
RustPython:
CPython:
Root cause
PyFloat::slot_new's fast path testsargs.args.first(), which succeeds for any number of positional arguments, sofloat(1.5, True)returns the first argument and never reaches theargs.bind(vm)call at L188 that would reject the extra one. CPython's clinic-generatedfloat_newruns_PyArg_CheckPositional("float", PyTuple_GET_SIZE(args), 0, 1)before it fetches the first argument, so arity is validated first. The fast path was hoisted out ofpy_newintoslot_newin 5f496c9 (#6398), which placed it ahead of argument binding, and the same commit gavePyInt::slot_newanargs.args.len() == 1guard thatPyStr::slot_newandPyComplex::slot_newalso carry andfloatdoes not.Fix
Add
args.args.len() == 1to the fast path condition, matchingPyInt::slot_new; the extra argument then falls through toargs.bind(vm), whereFuncArgs::bindraisesTypeError. That message readsexpected at most 1 arguments, got 2, so matching CPython's wording needs an explicit count check likePyFrozenSet::slot_new.Environment
Python Documentation or reference to CPython source code
CPython
float_new:Objects/clinic/floatobject.c.h#L218-L241— the arity check runs before the first argument is fetched:CPython
_PyArg_CheckPositional:Python/getargs.c#L2718-L2723— raises theTypeErrorwhennargsexceeds the declared maximum:The signature is documented as
float(x=0, /)in the clinic docstring:Objects/clinic/floatobject.c.h#L209-L213Documentation:
builtins.floatRelated arity-validation gaps in other builtin constructors: #8460, #8462.
AI disclosure: this report was drafted with Claude Code (claude-opus-5) and reviewed by me before submission. The divergence comes from a differential-testing run against CPython 3.14.3, and every source reference above was checked against the linked revisions.