pybind/pybind11

[BUG]: implementing __str__ on an enum turn the name attribute into a method

Fermée

#4 585 ouverte le 23 mars 2023

 (3 commentaires) (1 réaction) (0 personne assignée)C++ (2 005 forks)batch import
bughelp wanted

Métriques du dépôt

Stars
 (14 677 étoiles)
Métriques de merge PR
 (Merge moyen 5j 2h) (10 PRs mergées en 30 j)

Description

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.6.2

Problem description

When adding a .def(__str__, ...) on an instance of py::enum_ the name method of the enum members turns from an attribute into a method (i.e. you need parentheses to call it). Seems to happen both with and without a py::prepend(). Also this creates warnings when generating documentation with sphinx, because the docstring for name will show all the overloads for __str__. Those will contain *args and **kwargs which will be considered unclosed emphasis and strong emphasis characters.

Reproducible example code

enum class TestType
{
	Unknown = -1,
	RandomForest,
	NeuralNetworkLegacy,
	NeuralNetwork
};

py::enum_<TestType>(mod, "TestType")
  .value("NeuralNetwork", TestType::NeuralNetwork)
  .value("NeuralNetworkLegacy", TestType::NeuralNetworkLegacy)
  .value("RandomForest", TestType::RandomForest)
  .def("__str__", [](const TestType& t) { return "Hello"; }, py::prepend());

Then in Python:

TestType.NeuralNetwork.name
Out[1]: <bound method PyCapsule.name of <TestType.NeuralNetwork: 2>>

Is this a regression? Put the last known working version here if it is.

2.5

Guide contributeur