Code Monkey home page Code Monkey logo

Comments (12)

bwoodsend avatar bwoodsend commented on May 9, 2024 4

This is semi-possible now because of the auto_attribs option. With it you can just put the docstring beneath the attribute declaration:

@attr.s(auto_attribs=True)
class SomeClass:
    a_number: int = 42
    """Something descriptive about ``a_number``."""

It's still ignored by Python so help(SomeClass.a_number) doesn't work (in this case it'll just run help(42)) but Sphinx picks it up happily enough which was my main motivation for landing on this page.

from attrs.

radix avatar radix commented on May 9, 2024 1

I've actually just started documenting the attributes as :param: in the class docstring, which sphinx renders pretty nicely: http://effect.readthedocs.org/en/latest/apidocs.html#effect.Effect

This seems like probably the best solution, since if attrs were to modify the docstring, it'd have to assume a documentation format, which may be wrong (e.g. older projects still use epytext).

from attrs.

hynek avatar hynek commented on May 9, 2024 1

There is none; the community way is to put all your docs into the class docstring by hand. Adding templating magic would list likely break along the various consumers of it. 😐

To be fair, there's no boilerplate of repetition. It's just about the position where you put your docs.

from attrs.

hynek avatar hynek commented on May 9, 2024

I’ve played with it a bit but I’m not sure it’s possible when not using properties that are pretty special. :-/ Gotta dig deeper…

from attrs.

hynek avatar hynek commented on May 9, 2024

Reading the answers of http://stackoverflow.com/questions/5793306/python-get-doc-documentation-for-instance-variable#5793396 it seems to me that the only reason it work with your properties is they’re methods underneath.

So unless you find a way to add doc strings to data attributes without wrapping them using properties, I’m afraid this is impossible.

from attrs.

radix avatar radix commented on May 9, 2024

Have you considered appending documentation for attributes to the class's docstring? Or the __init__ docstring?

One other thing I noticed is that if you set default to an attr.Factory, sphinx shows the default as NOTHING which is slightly misleading (the reader would have to look down to the class attribute showing the Attribute instance, which will be unfamiliar to most people).

from attrs.

hynek avatar hynek commented on May 9, 2024

does that work? care to provide a PR/PoC? 😇

from attrs.

hynek avatar hynek commented on May 9, 2024

Yes, that’s why my init has no docstring. I removed it on Lynn’s request for this very reason. :)

from attrs.

Delengowski avatar Delengowski commented on May 9, 2024

Reading the answers of http://stackoverflow.com/questions/5793306/python-get-doc-documentation-for-instance-variable#5793396 it seems to me that the only reason it work with your properties is they’re methods underneath.

So unless you find a way to add doc strings to data attributes without wrapping them using properties, I’m afraid this is impossible.

You can add docstrings using descriptors.

I suppose that's not different enough from properties though?

class Descriptor:

   def __init__(self, doc_str):
       self.__doc__ = doc_str

   def __set_name__(self, owner, attr):
       self.attr = attr

   def __get__(self, instance, inst_type):
       return getattr(instance, f"_{self.attr}")

   def __set__(self, instance, value):
        setattr(instance, f"_{self.attr}", value)

class Foo:
   bar = Descriptor("Example Docstring")

foo = Foo()

help(foo)

You'll have to excuse me as I just discovered your library after trying out dataclasses for the first time. I was adding my own validators and docstrings to dataclass fields using descriptors. Then someone told me about attrs :)

Edit: I suppose what I did is not exactly what OP is doing.

from attrs.

relativistic avatar relativistic commented on May 9, 2024

Somehow param does this. See the "doc" keyword: https://param.holoviz.org/user_guide/Parameters.html#parameter-metadata

from attrs.

hynek avatar hynek commented on May 9, 2024

Somehow param does this. See the "doc" keyword: https://param.holoviz.org/user_guide/Parameters.html#parameter-metadata

If I read the docs correctly, it just appends a doc attribute to the params. It doesn't sound like it would work with help etc.


And since it came up the other day on StackOverflow to reinforce: the canonical way of documenting attributes of a class in Python is currently in the __init__ docstring. Until a new standard emerges, attrs is following 100% the community ways.

from attrs.

cjw296 avatar cjw296 commented on May 9, 2024

@hynek - what's the best way to specify the doc/help for a particular attribute when using @define and new-style type annotation/etc interface that ends up being in the __init__ docstring in the way you describe?

My specific use case is trying to get this somewhere better:

    time_limit: int = None  # in minutes

from attrs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.