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

Support type annotations for class attributes #44

Open
koliyo opened this issue Jun 13, 2018 · 13 comments
Open

Support type annotations for class attributes #44

koliyo opened this issue Jun 13, 2018 · 13 comments

Comments

@koliyo
Copy link

koliyo commented Jun 13, 2018

For example

class Foo(object):
    myAttr: int
    "This is a type annotated attribute"

Does not currently generate type information when using sphinx + sphinx-autodoc-typehints

@JonnyWaffles
Copy link

I too would like to see this support. I need to specify the expected types for a bunch of data-descriptors, but inline type hinting per the above example does not currently work.

@pmav99
Copy link

pmav99 commented Jan 30, 2019

To be pendantic, the annotations on the initial post are instance attributes annotations. For class attributes you need to use ClassVar.

class MyClass:
    # You can optionally declare instance variables in the class body
    attr: int
    # This is an instance variable with a default value
    charge_percent: int = 100

    # You can use the ClassVar annotation to declare a class variable
    seats: ClassVar[int] = 4
    passengers: ClassVar[List[str]]

https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html#classes

@felix-hilden
Copy link

If I've understood the documentation machinery correctly this would be huge for dataclasses. Big plus.

@Bibo-Joshi
Copy link

I'd also be interested in this feature :)

@JureSencar
Copy link

+1

@felix-hilden
Copy link

felix-hilden commented Jun 6, 2020

In Sphinx 3.0 functionality similar to this was added to autodoc. I know it's not a fix to this library, but I thought I'll leave it here too. It's nowhere near the whole thing and has some quirks that I'd like sorted out, but it looks promising for the dataclass use case at least. Sure beats having nothing.

# conf.py
extensions = [
   ...,
   'sphinx.ext.autodoc',
]
autodoc_typehints = 'description'  # show type hints in doc body instead of signature
autoclass_content = 'both'  # get docstring from class level and init simultaneously

# documentation
@dataclass
class C:
    """Docstring."""

    attr: dict
    value: int

Some oddities and inconveniences I've found:

  • autoclass_content is needed to have init typehints in documentation, even when the docstring is at the class level with parameter definitions
  • Doc comments and attribute docstrings don't work
  • For dataclasses the attribute descriptions naturally don't pass on when inheriting
  • When no descriptions are provided like above, the documentation includes a dash as if it would be followed by a description, but that's quite minor

I'm sorry if it's in bad taste to essentially advertise other approaches on a library forum, but at least for me a solution is more important. It would be nice to know what does @agronholm think about the native implementation and this library's role?

@mitar
Copy link

mitar commented Jun 6, 2020

So there is now: https://github.com/agronholm/sphinx-autodoc-typehints/pull/143

@altendky
Copy link

After a little tweak for some bad classes I deal with (david-yan#1) but otherwise it runs on my project.

It looks like presently #147 only looks at the Attributes: in the class docstring while autodoc checks the string literals immediately after the attribute/hint. Should the collection of the documentation be left to autodoc and this change just pulls the content from there? (I'm just starting to get into Sphinx so I may be misassuming some structure here) I also kind of prefer the autodoc formatting that places the type hint adjacent to the attribute name. I think this could be consistent with how sphinx-autodoc-typehints puts parameter hints adjacent to the parameter name. Also, this doesn't have the behavior of stripping out the output generated by autodoc.

All that said, thanks @david-yan for getting something started! Maybe I can find some time to build in some extra pieces.

@attr.s(auto_attribs=True, frozen=True, slots=True, eq=False)
class Emission:
    """Stores the emission of a signal including the emitted arguments.  Can be
    compared against a signal instance to check the source.  Do not construct this class
    directly.  Instead, instances will be received through a channel created by
    :func:`qtrio.enter_emissions_channel`.

    Note:
        Each time you access a signal such as ``a_qobject.some_signal`` you get a
        different signal instance object so the ``signal`` attribute generally will not
        be the same object.  A signal instance is a ``QtCore.SignalInstance`` in
        PySide2 or ``QtCore.pyqtBoundSignal`` in PyQt5.

    Attributes:
        signal: abc def
    """

    signal: qtrio._util.SignalInstance
    """An instance of the original signal."""

image

@agronholm
Copy link
Collaborator

I need this myself, but I have had very little time to spare for this project. I will look at the PR when time permits.

@altendky
Copy link

I can't really say I made any progress but I did poke around a bit to become minimally more familiar. The top two attributes in the image below (with zzz in their descriptions) are being produced by sphinx-autodoc-typehints while the bottom two are from autodoc. The thing I feel like I need to know next is how to put regular old ReST in the :type: and have it get processed rather than shown literally. I am guessing though that I am missing something pretty important about how this all works...

image

@gaborbernat
Copy link
Member

A PR for this would we welcome.

@nvaytet
Copy link

nvaytet commented Mar 16, 2023

Any progress on this? Thanks!

@gaborbernat
Copy link
Member

Please check the comment I made right above yours. That's the latest progress.

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