Skip to content

Adding docstrings to classes created with make_class #1309

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

Open
znichollscr opened this issue Jul 19, 2024 · 7 comments
Open

Adding docstrings to classes created with make_class #1309

znichollscr opened this issue Jul 19, 2024 · 7 comments
Labels

Comments

@znichollscr
Copy link

Hi there, I might have missed something. If I have my apologies.

My question is pretty basic: is there a way to add a docstring to class created with make_class? For example

from attrs import define

@define
class D:
    """
    Docstring
    """

# Prints out the docstring above
print(D.__doc__)

C = make_class("C", ["a", "b"])

# Prints None, which makes it seem like the docstring is unset.
# Is there a way to have the docstring be set when a class is created
# via `make_class`?
print(C.__doc__)

Thanks for any help

@znichollscr
Copy link
Author

Hmm ok, having read #8, I suspect the answer to this is that it is impossible. If any maintainers/experts think otherwise though, I'd be interested to hear their thoughts.

@Tinche
Copy link
Member

Tinche commented Jul 19, 2024

You can wrap make_class with your own function and set the docstring yourself. It's only a matter of C.__doc__ = "docstring", right?

@znichollscr
Copy link
Author

You can wrap make_class with your own function and set the docstring yourself. It's only a matter of C.doc = "docstring", right?

I'm not sure that works in all cases (at least #8 discusses lots of cases where it appears more complex), but that is certainly a possible workaround.

@hynek
Copy link
Member

hynek commented Jul 24, 2024

If I remember correctly, just attaching __doc__ to something doesn't work. But I'm open to be proven wrong here.

@hynek
Copy link
Member

hynek commented Jul 24, 2024

also #1294 seems to be tangentially related

@hynek hynek added the Feature label Jul 24, 2024
@thanegill
Copy link

Passing class_body= {"__doc__": "doc string"} to make_class seems to set the doc string correctly.

@hynek
Copy link
Member

hynek commented May 1, 2025

oh nice, assigning works too, btw. I tried both and then called help() on them.

looks like this behavior changed since Python 2 because there it doesn't:

>>> C.__doc__ = "foo"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: attribute '__doc__' of 'type' objects is not writable

this means we could move a nontrivial number of issues forward if people care enough about these topics?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants