-
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
Allow subclasses of py::args and py::kwargs #5381
Allow subclasses of py::args and py::kwargs #5381
Conversation
The current implementation does not allow subclasses of args or kwargs. This change allows subclasses to be used.
That seems fine to me. Could you please add a test? (That's the only way to ensure this feature change doesn't get lost.) |
@rwgk is this test sufficient? |
Looks good to me. Not too sure myself, but I'd make the change below, although it's tangential. WDYT? (Please post a comment to respond. I don't get notifications for git pushes.) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp
index afa9956c..09036ccd 100644
--- a/tests/test_kwargs_and_defaults.cpp
+++ b/tests/test_kwargs_and_defaults.cpp
@@ -25,11 +25,11 @@ namespace pybind11 {
namespace detail {
template <>
struct handle_type_name<ArgsSubclass> {
- static constexpr auto name = const_name("*args");
+ static constexpr auto name = const_name("*Args");
};
template <>
struct handle_type_name<KWArgsSubclass> {
- static constexpr auto name = const_name("**kwargs");
+ static constexpr auto name = const_name("**KWArgs");
};
} // namespace detail
} // namespace pybind11
diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py
index ac1f71a9..d7dfcb4e 100644
--- a/tests/test_kwargs_and_defaults.py
+++ b/tests/test_kwargs_and_defaults.py
@@ -431,3 +431,7 @@ def test_args_refcount():
(7, 8, myval),
{"a": 1, "b": myval},
)
+ assert (
+ m.args_kwargs_subclass_function.__doc__
+ == "args_kwargs_subclass_function(*Args, **KWArgs) -> tuple\n"
+ ) |
Added more tests and moved tests to more appropriate locations.
I have made the changes you suggested and added some more tests. |
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.
Nice! Thanks a lot for the great work on the tests.
Hi @rwgk @gentlegiantJGC,
where See wjakob/nanobind#605 for more info when we recently had a similar issue within Is there any way you can implement this new functionality, but still retaining the old behavior? Snippet of the compilation error
Thanks, ps: in case you need to have a look at the custom casters, do not follow the link in the above discussion, since it will lead you to a |
@francesco-ballarin I am a beginner C++ user so I don't fully understand the issue. The fastest solution is probably to submit a pull request with those changes and some test cases to make sure they don't get broken in the future. I thought |
Standard procedure would be to roll back #5381, and then to roll forward with fix plus additional test. Sorry I don't have spare time to help here. Please try to forward-fix in the next couple days. If there is no fix, I'll roll back so that you have more time. |
That's what I was thinking, too, tbh. Without understanding the issue:
|
Technically, you're actually in a best position to work on this, because you are setup up already to test with the "use case in the wild". For you, it could work like this:
For background:
Trying to be helpful, but untested: I'd maybe start with But I wouldn't stop there without thinking, at least a little bit. I'd want to understand why |
I think it boils down to the following, but that's just my intution, not a test:
Coming up with a test case that isn't
and does
it's not going to be obvious to me. Simplest solution would be to update the other PR to simply use |
Oh. Yes, I can believe that explanation.
Sigh. But OK, this isn't ideal, but the fix is so simple, I don't want to make this artificially difficult. Could you please update your PR, to implement just the simple fix? Please add
somewhere near your fix. Then in the description of 5396, add links to your comment above, and this comment. This is so that people working on the code in the future can easily find out why both |
Sure, will do that tomorrow. |
* Incomplete attempt to address regression introduced in #5381 * style: pre-commit fixes * Revert "style: pre-commit fixes" This reverts commit 9d107d2. * Revert "Incomplete attempt to address regression introduced in #5381" This reverts commit 8cf1cdb. * Simpler fix for the regression introduced in #5381 * style: pre-commit fixes * Added if constexpr workaround This can probably be done better but at least this is a start. * style: pre-commit fixes * Replace if constexpr with template struct if constexpr was not added until C++ 17. I think this should do the same thing as before. * style: pre-commit fixes * Made comment clearer * Added test cases * style: pre-commit fixes * Fixed is_same_or_base_of reference * style: pre-commit fixes * Added static assert messages * style: pre-commit fixes * Replaced typedef with using * style: pre-commit fixes * Back out `ForwardClassPtr` (to be discussed separately). Tested locally with clang-tidy. * Shuffle new `static_assert()` and leave error messages blank (they are more distracting than helpful here). --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: gentlegiantJGC <[email protected]> Co-authored-by: Ralf W. Grosse-Kunstleve <[email protected]>
Modified the tests from pybind#5381 to use the real Args and KWArgs classes
* Allow subclasses of args and kwargs The current implementation disallows subclasses of args and kwargs * Added object type hint to args and kwargs * Added type hinted args and kwargs classes * Changed default type hint to typing.Any * Removed args and kwargs type hint * Updated tests Modified the tests from #5381 to use the real Args and KWArgs classes * Added comment
Description
The current implementation does not allow subclasses of args or kwargs.
This change allows subclasses to be used.
This was originally submitted as part of #5357 but more discussion is required to solve it.
This is a very minor change which would allow me to implement my own workaround until it is solved.
Suggested changelog entry: