Skip to content

Commit 57a3663

Browse files
crisluengorwgk
authored andcommitted
fix: enable py::implicitly_convertible<py::none, ...> for py::class_-wrapped types (#3059)
* Allow casting from None to a custom object, closes #2778 * ci.yml patch from the smart_holder branch for full CI coverage.
1 parent cfc0683 commit 57a3663

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

include/pybind11/detail/type_caster_base.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,6 @@ class type_caster_generic {
657657
PYBIND11_NOINLINE bool load_impl(handle src, bool convert) {
658658
if (!src) return false;
659659
if (!typeinfo) return try_load_foreign_module_local(src);
660-
if (src.is_none()) {
661-
// Defer accepting None to other overloads (if we aren't in convert mode):
662-
if (!convert) return false;
663-
value = nullptr;
664-
return true;
665-
}
666660

667661
auto &this_ = static_cast<ThisT &>(*this);
668662
this_.check_holder_compat();
@@ -731,7 +725,19 @@ class type_caster_generic {
731725
}
732726

733727
// Global typeinfo has precedence over foreign module_local
734-
return try_load_foreign_module_local(src);
728+
if (try_load_foreign_module_local(src)) {
729+
return true;
730+
}
731+
732+
// Custom converters didn't take None, now we convert None to nullptr.
733+
if (src.is_none()) {
734+
// Defer accepting None to other overloads (if we aren't in convert mode):
735+
if (!convert) return false;
736+
value = nullptr;
737+
return true;
738+
}
739+
740+
return false;
735741
}
736742

737743

tests/test_methods_and_attributes.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ int none3(const std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -
118118
int none4(std::shared_ptr<NoneTester> *obj) { return obj && *obj ? (*obj)->answer : -1; }
119119
int none5(const std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -1; }
120120

121+
// Issue #2778: implicit casting from None to object (not pointer)
122+
class NoneCastTester {
123+
public:
124+
int answer = -1;
125+
NoneCastTester() = default;
126+
NoneCastTester(int v) : answer(v) {};
127+
};
128+
121129
struct StrIssue {
122130
int val = -1;
123131

@@ -358,6 +366,16 @@ TEST_SUBMODULE(methods_and_attributes, m) {
358366
m.def("no_none_kwarg", &none2, "a"_a.none(false));
359367
m.def("no_none_kwarg_kw_only", &none2, py::kw_only(), "a"_a.none(false));
360368

369+
// test_casts_none
370+
// Issue #2778: implicit casting from None to object (not pointer)
371+
py::class_<NoneCastTester>(m, "NoneCastTester")
372+
.def(py::init<>())
373+
.def(py::init<int>())
374+
.def(py::init([](py::none const&) { return NoneCastTester{}; }));
375+
py::implicitly_convertible<py::none, NoneCastTester>();
376+
m.def("ok_obj_or_none", [](NoneCastTester const& foo) { return foo.answer; });
377+
378+
361379
// test_str_issue
362380
// Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
363381
py::class_<StrIssue>(m, "StrIssue")

tests/test_methods_and_attributes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,17 @@ def test_accepts_none(msg):
433433
assert m.ok_none4(None) == -1
434434

435435

436+
def test_casts_none(msg):
437+
"""#2778: implicit casting from None to object (not pointer)"""
438+
a = m.NoneCastTester()
439+
assert m.ok_obj_or_none(a) == -1
440+
a = m.NoneCastTester(4)
441+
assert m.ok_obj_or_none(a) == 4
442+
a = m.NoneCastTester(None)
443+
assert m.ok_obj_or_none(a) == -1
444+
assert m.ok_obj_or_none(None) == -1
445+
446+
436447
def test_str_issue(msg):
437448
"""#283: __str__ called on uninitialized instance when constructor arguments invalid"""
438449

0 commit comments

Comments
 (0)