Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST]: typing.SupportsInt type hint #5158

Open
3 tasks done
gentlegiantJGC opened this issue Jun 10, 2024 · 1 comment
Open
3 tasks done

[FEATURE REQUEST]: typing.SupportsInt type hint #5158

gentlegiantJGC opened this issue Jun 10, 2024 · 1 comment
Labels
triage New bug, unverified

Comments

@gentlegiantJGC
Copy link

gentlegiantJGC commented Jun 10, 2024

Required prerequisites

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

2.12.0

Problem description

It would be nice if functions/methods that accept objects which can be coerced into an int would be typed as such in the docstrings.

Functions that accept a C++ int type currently generate stub files that look like the following.

def test(value: int) -> None:
    ...

The function can also be called with any object that implements __int__ including numpy int types and custom objects.

from numpy import uint16
test(uint16(5))
class MyInt:
    def __int__(self) -> int:
        return 5

test(MyInt())

mypy complains about the function call because they are not subclasses of int.

If the intention of pybind in these cases is to accept any object that can be coerced into an int it would be nice if they could be type hinted as such.

The correct result would look like this

import typing
def test(value: typing.Union[int, typing.SupportsInt]) -> None:
    ...

or just

import typing
def test(value: typing.SupportsInt) -> None:
    ...

Reproducible example code

No response

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

Not a regression

@gentlegiantJGC gentlegiantJGC added the triage New bug, unverified label Jun 10, 2024
@gentlegiantJGC
Copy link
Author

Seems like the type hint originates from here but it also effects the return type. I don't know what else that effects.

PYBIND11_TYPE_CASTER(T, const_name<std::is_integral<T>::value>("int", "float"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage New bug, unverified
Projects
None yet
Development

No branches or pull requests

1 participant