Skip to content
\n

The generated docs converts these to decimal values as so:

\n\"image\"\n

I'd like to maintain the source code representation. Is this possible?

\n

Additionally I'd love the table to also say what the value is too.

","upvoteCount":2,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

OK so, it won't be possible to fix this while we keep using ast, but I can imagine a few solutions:

\n\n
import griffe\n\nclass ToHex(griffe.Extension):\n    def __init__(self, objects: list[str]) -> None:\n         self._objects = set(objects)\n\n    def on_attribute(self, *, attr: griffe.Attribute, **kwargs) -> None:\n        if attr.path in self._objects:\n            if isinstance(attr.value, griffe.ExprConstant):  # just to be sure\n                attr.value.value = hex(int(attr.value.value))
\n

Maybe make it accept paths to enums instead of enum values:

\n
import griffe\n\nclass ToHex(griffe.Extension):\n    def __init__(self, objects: list[str]) -> None:\n         self._objects = set(objects)\n\n    def on_class_members(self, *, cls: griffe.Class, **kwargs) -> None:\n        if cls.path in self._objects:\n            for attr in cls.attributes.values():\n                if isinstance(attr.value, griffe.ExprConstant):  # just to be sure\n                    attr.value.value = hex(int(attr.value.value))
\n

In mkdocs.yml:

\n
plugins:\n- mkdocstrings:\n    handlers:\n      python:\n        options:\n          extensions:\n          - scripts/griffe_extensions.py:  # or wherever you wrote your extension\n              objects:\n              - path.to.Enum1\n              - path.to.Enum2
","upvoteCount":0,"url":"https://github.com/mkdocstrings/python/discussions/255#discussioncomment-12338675"}}}
Discussion options

You must be logged in to vote

OK so, it won't be possible to fix this while we keep using ast, but I can imagine a few solutions:

  • we move to a CST parser (Concrete rather than Abstract Syntax Tree), but this is a drastic change so won't happen soon
  • you write a small, custom Griffe extension that converts back values of specific objects to hex
import griffe

class ToHex(griffe.Extension):
    def __init__(self, objects: list[str]) -> None:
         self._objects = set(objects)

    def on_attribute(self, *, attr: griffe.Attribute, **kwargs) -> None:
        if attr.path in self._objects:
            if isinstance(attr.value, griffe.ExprConstant):  # just to be sure
                attr.value.value = hex(int(attr.value.

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@pawamoy
Comment options

@mrossington
Comment options

@mrossington
Comment options

@pawamoy
Comment options

Answer selected by mrossington
@mrossington
Comment options

@pawamoy
Comment options

@pawamoy
Comment options

@pawamoy
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
docs Improvements or additions to documentation griffe extension Can be solved with a Griffe extension
2 participants