-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add type hints for args and kwargs #5357
base: master
Are you sure you want to change the base?
Conversation
The current implementation disallows subclasses of args and kwargs
The tests are failing because I haven't updated them. |
Do you have "before" and "after" this PR examples? Could you please add to the PR description? Could you describe in more detail what's better after this PR? |
I have updated the post. |
typing Any isn't the right typing for this anyway, typing_extensions has specific typing args for kwargs and args params, doesn't it? |
Sorry I somehow missed your reply last week. Quoting from the updated PR description: Currently this will generate stub files that look like this
If this is used with mypy it will generate this error.
The generated stub looks good to me, but I agree the mypy error is annoying. What's the ideal desired behavior, assuming we can change both pybind11 and mypy? My mind is going to: The generated stub is exactly what I'd want to see, we just need a way to express that that's intentional here, so that mypy does not complain. |
From what I can see it has
What type hint would you suggest? I tried typing it as Here is the section in PEP 484 about typing hinting
The type hints here are implicitly A workaround could be to add |
I have created #5381 which includes only the part allowing subclasses of args and kwargs. Hopefully this will be easier to merge and allow me to implement a workaround until you can find a solution for this. |
A less intrusive workaround would be to leave the type hints for |
Modified the tests from pybind#5381 to use the real Args and KWArgs classes
@rwgk this is ready for review again. |
Description
The stubs generated by pybind11 do not contain any type hints for
*args
or**kwargs
which makes static type checkers like mypy sad.This pull requests
typing.Any
to*args
and**kwargs
(*args: typing.Any, **kwargs: typing.Any
)py::Args
andpy::KWArgs
which can be used in place ofpy::args
andpy::kwargs
to add custom type hints.Examples
Currently this will generate stub files that look like this
If this is used with mypy it will generate this error.
error: Function is missing a type annotation for one or more arguments
The changes proposed will generate stub files that look like this which resolves the mypy error.
Alternatively the
py::Args
andpy::KWArgs
classes can be used to add better type hints in the same vein as pybind11 does typing.This generates the following stub.
This can also be used with anything pybind11 supports including custom classes added through pybind11.
Alternatives
The only alternative solution I am currently aware of is to manually write the definition in a doc like this.
Suggested changelog entry: