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

dynamic loading of an instance should prioritize a statically written docstring (if it exists) #308

Open
machow opened this issue Nov 27, 2023 · 2 comments

Comments

@machow
Copy link
Owner

machow commented Nov 27, 2023

Generally, dynamic loading works well for modules, classes and functions, but can behave surprisingly for instances. This is because what it means to document an instance is ambiguous.

Consider below:

# Functions ----

def some_func():
    """I am a docstring"""

some_dynamic_func = some_func

some_other_dynamic_func = some_func
"""I am a function"""



# Instances ----

some_int = 1

some_other_int = 2
"""Another int"""

Note that dynamically loading produces these behaviors:

  • functions: when documenting any of the functions above (some_func, some_dynamic_fun, some_other_dynamic_func) the docstring some_func.__doc__ is used.
  • instances: when documenting some_int or some_other_int, the underlying docstring int.__doc__ is also used.
    • However, this can feel surprising and wrong, because the user took the time to use the special docstring format on some_other_int.

For cases where a variable has a docstring defined below it, we should favor that docstring, so it matches what happens statically.

@machow
Copy link
Owner Author

machow commented Nov 27, 2023

from #307

@has2k1
Copy link
Contributor

has2k1 commented Dec 5, 2023

What the "official" (PEP 257) sources say about documenting attributes.

String literals occurring elsewhere in Python code may also act as documentation. They are not recognized by the Python bytecode compiler and are not accessible as runtime object attributes (i.e. not assigned to doc), but two types of extra docstrings may be extracted by software tools:

String literals occurring immediately after a simple assignment at the top level of a module, class, or init method are called “attribute docstrings”.
String literals occurring immediately after another docstring are called “additional docstrings”.

What sphinx accepts as documenting attributes

For module data members and class attributes, documentation can either be put into a comment with special formatting (using a #: to start the comment instead of just #), or in a docstring after the definition. Comments need to be either on a line of their own before the definition, or immediately after the assignment on the same line. The latter form is restricted to one line only.

This means that in the following class definition, all attributes can be autodocumented:

class Foo:
    """Docstring for class Foo."""

    #: Doc comment for class attribute Foo.bar.
    #: It can have multiple lines.
    bar = 1

    flox = 1.5   #: Doc comment for Foo.flox. One line only.

    baz = 2
    """Docstring for class attribute Foo.baz."""

    def __init__(self):
        #: Doc comment for instance attribute qux.
        self.qux = 3

        self.spam = 4
        """Docstring for instance attribute spam."""

My takeaway is all """ strings that follow any type of attribute should be recognised as documenting the attribute as there isn't any other competing intent.

@machow machow added this to quartodoc Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

2 participants