Base class of non exposed derived class in Python side?
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Reproduce the example using the shown Base, Derived, DerivedTwo, factory, and factoryTwo definitions. Start by reading the Boost.Python class_ registrations, bases declaration, and shared_ptr conversion behavior; compare the returned Python types with and without registering DerivedTwo. Done means the type-selection behavior is explained and the expected handling or scope of a fix is documented.
Written by the indexing model from the issue text.
Description
Hello, I have a use case where a polymorphism factory can instantiate object of a non exposed class. I have a multi-level inheritance scheme and the last level is not exposed to Python. What I try to understand is why the Python object of the last level takes the type of the top-most class in the inheritance hierarchy and not the one just above?
Here is a minimal example to reproduce the behavior:
C++ side
class Base
{
public:
Base() {}
virtual ~Base() = default;
};
class Derived: public Base
{
public:
Derived(int value) { m_value = value; }
~Derived() = default;
int getValue() { return m_value; }
private:
int m_value;
};
class DerivedTwo: public Derived
{
public:
DerivedTwo(int value): Derived(value) {}
~DerivedTwo() = default;
};
std::shared_ptr<Base> factory()
{
return std::make_shared<Derived>(9);
}
std::shared_ptr<Base> factoryTwo()
{
return std::make_shared<DerivedTwo>(99);
}
BOOST_PYTHON_MODULE(mymodule)
{
using namespace boost::python;
class_<Base, std::shared_ptr<Base>, boost::noncopyable>("Base", init<>());
class_<Derived, bases<Base>, std::shared_ptr<Derived>, boost::noncopyable>("Derived", init<int>())
.def("get_value", &Derived::getValue)
;
def("factory", &factory);
def("factory_two", &factoryTwo);
}
Python side
import mymodule
obj = mymodule.factory()
print(type(obj))
obj2 = mymodule.factory_two()
print(type(obj2))
Output
<class 'mymodule.Derived'>
<class 'mymodule.Base'> <-- WHY?
Obviously, if I expose DerivedTwo class, all is OK.
Thanks for your help.
- Dominant language
- C++
- Stars
- 537
- Forks
- 223
- Avg merge
- 11h 22m
- Merged PRs (30d)
- 2
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/python
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
BoostDetectToolset-1.90.0.cmake file not found in an include() call in boost_python-config.cmake Open
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
games-on-whales/wolf#509 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
bug-unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 76/100