Skip to content

[BUG]: Type hint conflict with noconvert float #5878

Closed

Description

@gentlegiantJGC

Required prerequisites

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

3.0.1

Problem description

If a float argument is marked as .noconvert() it will only accept python float arguments and will be type hinted as float.
PEP 484 says that a float type hint can accept float or int which is in conflict with the type caster.

This issue was spawned from #5767

Solutions

  1. Allow noconvert float to accept int
    • I have no idea what breaking side effects this wil have
  2. Accept that the type hint is broken
    • I don't think it is possible to type hint for only a float

Reproducible example code

m.def("test_float", [](float arg) { }, py::arg("arg"));
m.def("test_float_noconvert", [](float arg) { }, py::arg("arg").noconvert());
>>> from m import test_float, test_float_noconvert
>>> test_float.__doc__
'test_float(arg: typing.SupportsFloat) -> None\n'
>>> test_float_noconvert.__doc__
'test_float_noconvert(arg: float) -> None\n'
>>> test_int.__doc__
'test_int(arg: typing.SupportsInt) -> None\n'
>>> test_int_noconvert.__doc__
'test_int_noconvert(arg: int) -> None\n'
>>> test_float(5.5)
>>> test_float(5)
>>> test_float_noconvert(5.5)
>>> test_float_noconvert(5)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: test_float_noconvert(): incompatible function arguments. The following argument types are supported:
    1. (arg: float) -> None
Invoked with: 5

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

Not a regression

Activity

  1. added
    triage
    on Oct 23, 2025
  2. gentlegiantJGC commented on Oct 23, 2025

    @gentlegiantJGC
    ContributorAuthor

    There is a long (and ongoing) disccussion about the float type hint here
    https://discuss.python.org/t/clarifying-the-float-int-complex-special-case/54018

    In my personal opinion the decision in PEP 484 was a mistake but one that can't easily be undone.

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