Skip to content

float(1.5, True) returns the first argument instead of raising TypeError #8461

Description

@jseop-lim

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

print(float(1.5, True))

RustPython:

1.5

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions