[BUG] Unbound methods should just be functions instead of instancemethod
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Issue description
Objects of classes created with pybind11 can't be pretty printed with the pprint module if the result is larger than the given width.
This only happens with python3.
It used to happen with cython too, but cython stopped using 'instancemethod', which solved the issue. The pprint bug was discarded because "Functions/methods should be immutable, so yes, I think it is a safe assumption that they should be hashable":
https://bugs.python.org/issue33395
Reproducible example code
example.cpp:
#include <pybind11/pybind11.h>
#include <string>
namespace py = pybind11;
struct Dummy
{
std::string text;
};
PYBIND11_MODULE(example, m) {
using namespace pybind11::literals;
py::class_<Dummy>(m, "Dummy")
.def(py::init<std::string>(), "text"_a)
.def_readonly("text", &Dummy::text)
.def("__str__", [](const Dummy& w) { return w.text; })
.def("__repr__", [](const Dummy& w) { return w.text; });
}
bug.py:
import example
import pprint
pprint.pprint(example.Dummy('abc'), width=3)
Result:
Traceback (most recent call last):
File "bug.py", line 4, in <module>
pprint.pprint(example.Dummy('abc'), width=2)
File "/usr/lib/python3.8/pprint.py", line 53, in pprint
printer.pprint(object)
File "/usr/lib/python3.8/pprint.py", line 148, in pprint
self._format(object, self._stream, 0, 0, {}, 0)
File "/usr/lib/python3.8/pprint.py", line 173, in _format
p = self._dispatch.get(type(object).__repr__, None)
TypeError: unhashable type: 'instancemethod'
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 by building the binding shown in example.cpp and running bug.py with Python 3 to reproduce the pprint TypeError. Read the pybind11 class and method-binding entry points related to unbound methods, then verify that pprint.pprint handles the resulting object at a narrow width without raising the unhashable instancemethod error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100