Skip to content

Question: can I do anything about docstrings for attributes being picked up from last location #128

@rodrigogiraoserrao

Description

@rodrigogiraoserrao

Hey there,

I have been using mkdocstrings with mkdocs and I've hit an interesting predicament.
When documenting attributes in Python code, griffe is able to pick up docstrings from within the class body and from within the dunder method __init__. I.e., in the code below, both foo and bar would be documented:

class C:
    foo = 42
    """A number I enjoy."""

    def __init__(self):
        self.bar = 73
        """Another number I enjoy."""

However, in some cases I'll set a default value in the class body and then I'll do some initialisation inside __init__ and, in that case, griffe will pick the docstring that is used last... Even if one of the cases does not include a docstring.
E.g., in the situation below foo would not have a docstring:

class C:
    foo = 42
    """A number I enjoy."""

    def __init__(self, number):
        self.foo = number

I would expect foo to be documented with the docstring “A number I enjoy.”.
However, if I move the docstring to inside __init__ or the assignment to be after the method __init__, then it'll work.
Is this a bug? Is this an intentional design decision? Can I do anything about it?

Thanks a lot for your time!

P.S. for reference, the original exploration that led to me opening this issue is Textualize/textual#1572.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions