pybind / pybind/pybind11

Unable to assign to python property

Open
#2,871 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

I am trying to export an enum and a property for a class which uses the enum. In my class I have this two methods

ForcesMap forcesMap() const; //getter
void setForcesMap(const VNCS::Sim2D::DeformationGradientMap::ForcesMap &forcesMap); //setter

the enum is defined in the scope of my class

enum class ForcesMap { Coarse, Fine };

I create bindings for both the enum and the class like this

::py::enum_<VNCS::Sim2D::DeformationGradientMap::ForcesMap>(m, "ForcesMap")
        .value("Coarse", VNCS::Sim2D::DeformationGradientMap::ForcesMap::Coarse)
        .value("Fine", VNCS::Sim2D::DeformationGradientMap::ForcesMap::Fine);

::py::class_<VNCS::Sim2D::DeformationGradientMap, Inherit, sofa::core::sptr<VNCS::Sim2D::DeformationGradientMap>>(
        m, "DeformationGradientMap")
        ...
        .def_property("forcesMap",
                      &VNCS::Sim2D::DeformationGradientMap::forcesMap,
                      &VNCS::Sim2D::DeformationGradientMap::setForcesMap);

The getter works fine. I am able to read the property from python and when debugging, it stop in a breakpoint inside the getter. However, the setter doesn't work, it is never called.

From python, I can do the following:

dir(PyVNCS.Sim2D.ForcesMap)
['Coarse', 'Fine', '__class__', '__delattr__', '__dir__', '__doc__', '__entries', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', ...]
phiFMap.forcesMap
<ForcesMap.Coarse: 0>
phiFMap.forcesMap = PyVNCS.Sim2D.ForcesMap.Fine
phiFMap.forcesMap
<ForcesMap.Coarse: 0>

However, it works fine if I change to a readonly property + a function which sets the value (.def_property_readonly + .def)

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 with the shown py::enum_, py::class_, and .def_property bindings, then reproduce the Python assignment sequence using ForcesMap.Fine. Trace why the getter returns the enum but the setter is not reached; done means assignment invokes setForcesMap and a subsequent read returns Fine, with coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.