[BUG]: `tp_name` field not initialized correctly for a nested class
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
2.11.1
Problem description
Consider the following bindings for a class Pet and some nested class Pet.Attributes, whichs is taken from the pybind11 docs:
py::class_<Pet> pet(m, "Pet");
py::class_<Pet::Attributes>(pet, "Attributes")
.def(py::init<>());
pybind11 seems to be computing a wrong tp_name field for the nested class' PyTypeObject in make_new_python_type(). The class ends up being named just mymodule.Attributes instead of the expected mymodule.Pet.Attributes.
This can be demonstrated: The Python interpreter uses the wrong tp_name in error messages, for example:
import mymodule
a = mymodule.Pet.Attributes()
print(a.nonexisting)
gives the following error string:
AttributeError: 'mymodule.Attributes' object has no attribute 'nonexisting'
Please let me know if you also think this needs to be fixed. If so, I would be willing to contribute a pull request.
Reproducible example code
No response
Is this a regression? Put the last known working version here if it is.
Not a regression
Contributor guide
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.
Research direction
Start in include/pybind11/detail/class.h at make_new_python_type(), then reproduce the nested Pet.Attributes case described in the issue and inspect the generated PyTypeObject name. Done means Python errors identify the object as mymodule.Pet.Attributes rather than mymodule.Attributes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100