pybind / pybind/pybind11

Disable custom type conversion of self when calling class method

Open
#1,485 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Issue description

I have implemented a class in C++ which I would like to both expose to Python (py::class_) as well as allow to be converted (py::detail::type_caster).
(The main reason for this is, that I want to convert existing Python classes to my specialized C++ class while also writing Python unit tests for my C++ class)

I now run into the problem that, when calling methods of the exposed C++ class, the self argument tries to convert itself.

In the attached code, the output will be "converted" because py::detail::type_caster::load is being called.
However, I would like it to be "original", i.e. that type converter should not be applied to the self argument.
Furthermore, when uncommenting return false;, the program crashes with

Traceback (most recent call last):
  File "bug_dummy.py", line 6, in <module>
    bug_dummy.Foo().bar()
TypeError: bar(): incompatible function arguments. The following argument types are supported:
    1. (self: Foo) -> None

Invoked with: <bug_dummy.Foo object at 0x109ec34c8>

I would expect it to simply executeFoo::bar.

Is there a way of circumventing this problem or do I need to consider a different approach?
I looked at .noconvert(), but this does not seem to be relevant.
Thank you very much for your help.

Reproducible example code

bug_dummy.cpp:

/*<%
cfg['compiler_args'] = ['-std=c++11']
%>*/
#include <pybind11/pybind11.h>

namespace py = pybind11;


class Foo {
public:
    Foo() {}
    std::string data = "original";

    void bar() {
        py::print(data);
    }
};


template<> struct py::detail::type_caster<Foo> {
public:
    PYBIND11_TYPE_CASTER(Foo, _("Foo"));
    bool load(py::handle src, bool) {
        //return false;
        value = Foo();
        value.data = "converted";
        return true;
    }
    static py::handle cast(Foo src, py::return_value_policy /* policy */, py::handle /* parent */) {
        py::print("-> Python");
        return py::object();
    }
};


PYBIND11_MODULE(bug_dummy, m) {
    py::class_<Foo>(m, "Foo")
        .def(py::init<>())
        .def("bar", &Foo::bar)
    ;
}

bug_dummy.py:

import cppimport

cppimport.force_rebuild()
bug_dummy = cppimport.imp('bug_dummy')

bug_dummy.Foo().bar()

(sorry for the external dependency)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running bug_dummy.py with the binding and custom type_caster defined in bug_dummy.cpp, then trace type_caster::load during the .def("bar", &Foo::bar) call. Done means Foo().bar() uses the original exposed Foo and prints "original" without attempting custom self conversion, while preserving the expected behavior when loading fails.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.