Skip to content

How to render my attribute values as hex #255

Answered by pawamoy
mrossington asked this question in Q&A
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