Skip to content

Commit 4c356d2

Browse files
authored
Fix __new__ overloads with variadic args but not keyword args. (#1172)
We had confused has_var_kwargs with has_var_args, and this made us think that such a __new__ overload was not compatible with a nullary call.
1 parent aeae256 commit 4c356d2

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

src/nb_func.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ PyObject *nb_func_new(const func_data_prelim_base *f) noexcept {
201201
bool has_scope = f->flags & (uint32_t) func_flags::has_scope,
202202
has_name = f->flags & (uint32_t) func_flags::has_name,
203203
has_args = f->flags & (uint32_t) func_flags::has_args,
204-
has_var_args = f->flags & (uint32_t) func_flags::has_var_kwargs,
205-
has_var_kwargs = f->flags & (uint32_t) func_flags::has_var_args,
204+
has_var_args = f->flags & (uint32_t) func_flags::has_var_args,
205+
has_var_kwargs = f->flags & (uint32_t) func_flags::has_var_kwargs,
206206
can_mutate_args = f->flags & (uint32_t) func_flags::can_mutate_args,
207207
has_doc = f->flags & (uint32_t) func_flags::has_doc,
208208
has_signature = f->flags & (uint32_t) func_flags::has_signature,

tests/test_classes.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,13 +680,20 @@ NB_MODULE(test_classes_ext, m) {
680680
// issue #786
681681
struct NewNone {};
682682
struct NewDflt { int value; };
683+
struct NewStarPosOnly { size_t value; };
683684
struct NewStar { size_t value; };
684685
nb::class_<NewNone>(m, "NewNone")
685686
.def(nb::new_([]() { return NewNone(); }));
686687
nb::class_<NewDflt>(m, "NewDflt")
687688
.def(nb::new_([](int value) { return NewDflt{value}; }),
688689
"value"_a = 42)
689690
.def_ro("value", &NewDflt::value);
691+
nb::class_<NewStarPosOnly>(m, "NewStarPosOnly")
692+
.def(nb::new_([](nb::args a, int value) {
693+
return NewStarPosOnly{nb::len(a) + value};
694+
}),
695+
"args"_a, "value"_a = 42)
696+
.def_ro("value", &NewStarPosOnly::value);
690697
nb::class_<NewStar>(m, "NewStar")
691698
.def(nb::new_([](nb::args a, int value, nb::kwargs k) {
692699
return NewStar{nb::len(a) + value + 10 * nb::len(k)};

tests/test_classes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,10 @@ def test46_custom_new():
895895
t.NewNone()
896896
assert t.NewDflt().value == 42
897897
assert t.NewDflt(10).value == 10
898+
assert t.NewStarPosOnly().value == 42
899+
assert t.NewStarPosOnly("hi").value == 43
900+
assert t.NewStarPosOnly(value=10).value == 10
901+
assert t.NewStarPosOnly("hi", "lo", value=10).value == 12
898902
assert t.NewStar().value == 42
899903
assert t.NewStar("hi").value == 43
900904
assert t.NewStar(value=10).value == 10

tests/test_classes_ext.pyi.ref

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ class NewDflt:
335335
@property
336336
def value(self) -> int: ...
337337

338+
class NewStarPosOnly:
339+
def __init__(self, *args, value: int = 42) -> None: ...
340+
341+
@property
342+
def value(self) -> int: ...
343+
338344
class NewStar:
339345
def __init__(self, *args, value: int = 42, **kwargs) -> None: ...
340346

0 commit comments

Comments
 (0)