-
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
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I finally found a few minutes to look.
Could you please help me understand?
From the PR description:
The changes proposed will generate stub files that look like this which resolves the mypy error.
def test(*args: typing.Any, **kwargs: typing.Any) -> None: ...
Is this still relevant? (If not, could you please update the PR description?)
Also from the PR description:
def test(*args: int, **kwargs: float) -> None: ...
What do the int
and float
type hints mean here? What are type checkers meant to do here? Enforce that all positional arguments have type int
, and all keyword arguments have type float
?
I have removed this.
Yes. I have edited the OP to include a link to the PEP where this is defined. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I have edited the OP to include a link to the PEP where this is defined.
I see you also added this as a comment, perfect, thanks!
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 request adds templated classes
py::Args
andpy::KWArgs
which can be used in place ofpy::args
andpy::kwargs
to add custom type hints for*args
and**kwargs
as described in PEP 484Examples
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
py::Args
andpy::KWArgs
classes can be used instead to add 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: