Skip to content

[BUG]: py::float_ does not support casting from python int #5840

Closed

Description

@gentlegiantJGC

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

3.0.0

Problem description

py::float_ can be constructed with a python int but the type caster does not consider it valid.

The following works as expected

py::int_ i(5);
py::float_ f(i);

I would expect the following to work but it does not.

m.def("float_cast", [](py::float_ f){ return f; });
float_cast(5);  # call with an int
Traceback (most recent call last):
TypeError: float_cast(): incompatible function arguments. The following argument types are supported:
    1. (arg0: float) -> float
Invoked with: 5

The issue seems to stem from the pyobject type caster only casting if the object is an instance of float which int is not.

I think the solution here is to add a custom type caster for py::float_ but I don't know how to do this.

Is this a regression? Put the last known working version here if it is.

Not a regression

Activity

  1. added
    triage
    on Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions