Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
IljaManakov opened this issue Mar 23, 2023 · 3 comments
Open
3 tasks done

Comments

@IljaManakov
Copy link

IljaManakov commented Mar 23, 2023

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

@IljaManakov IljaManakov added the triage New bug, unverified label Mar 23, 2023
@IljaManakov
Copy link
Author

IljaManakov commented Mar 23, 2023

Also, I found this weird piece. Notice how both methods are called name. They are not identical, though when comparing them with is
image

@Skylion007 Skylion007 added bug help wanted and removed triage New bug, unverified labels Mar 27, 2023
@Skylion007
Copy link
Collaborator

The way we have enums implemented currently is a major source of bugs / odd behavior. Open to PRs that improve the situation.

@varshneydevansh
Copy link

Are there any beginner-friendly bugs from where I can start with?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants