Skip to content

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

@gentlegiantJGC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions