python-attrs / attrs

Python Classes Without Boilerplate
https://www.attrs.org/
MIT License
5.3k stars 374 forks source link

Adding docstrings to classes created with `make_class` #1309

Open znichollscr opened 4 months ago

znichollscr commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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

hynek commented 4 months ago

also #1294 seems to be tangentially related