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

Python Enum values (used to show default values in function signatures) are rendered ugly. #9272

Closed
sidneycadot opened this issue May 26, 2021 · 4 comments
Labels
Milestone

Comments

@sidneycadot
Copy link

Python Enum values (used to show default values in function signatures) are rendered ugly.

To Reproduce

I made a minimal example to show the issue:

https://github.com/sidneycadot/sphinx_issue_ugly_enum

$ git clone git@github.com:sidneycadot/sphinx_issue_ugly_enum.git
$ cd sphinx_issue_ugly_enum/
$ make html
$ firefox build/html/index.html 

Expected behavior

I would hope the signature rendered as:

ugly_enum_func(e: ugly_enum.MyEnum = MyEnum.ValueA) → None

Unfortunately, it renders as:

ugly_enum_func(e: ugly_enum.MyEnum = <MyEnum.ValueA: 10>) → None

Environment info

  • Python version: 3.9.5
  • Sphinx version: 4.0.2
  • Sphinx extensions: autodoc
@sidneycadot
Copy link
Author

Probably the repr() representation of default arguments is used, and it should be; that is supposed to give a string that, when evaluated, yields the value.

Unfortunately, the enum.Enum implementation in Python does not honor this convention; their repr() includes the Enum value and the "<>" brackets.

In an ideal world we could ask the enum.Enum people to fix that behavior; but I am afraid that would break quite a lot of code in the wild.

The best course of action may be to special-case Enum types in autodoc.

@sidneycadot
Copy link
Author

Searched the Python bug-tracker. There is some active discussion going on there to see if/how they should change repr for Enum classes:

https://bugs.python.org/issue40066

@sidneycadot
Copy link
Author

sidneycadot commented May 26, 2021

A workaround for the issue is to provide a __repr__ emplementation with Enum types, which may be a good idea anyway until the Python folks sort this out:

class MyEnum(enum.Enum):
    ValueA = 10
    ValueB = 20

    def __repr__(self):
        return "MyEnum." + self.name

@tk0miya
Copy link
Member

tk0miya commented May 29, 2021

+1 Reasonable.

@tk0miya tk0miya added extensions:autodoc type:proposal a feature suggestion and removed type:bug labels May 29, 2021
@tk0miya tk0miya added this to the 4.1.0 milestone May 29, 2021
tk0miya added a commit that referenced this issue May 30, 2021
Close #9272: autodoc: Render enum values for the default argument value better
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants