theislab / scanpydoc

Collection of Sphinx extensions similar to (but more flexible than) numpydoc
https://icb-scanpydoc.readthedocs-hosted.com/en/latest/
GNU General Public License v3.0
12 stars 6 forks source link

[Help request] Formatting of class attributes #34

Closed caenrigen closed 10 months ago

caenrigen commented 3 years ago

Hi!

I have been using this package for a while, I like a lot the the features of the scanpydoc.elegant_typehints. Nice work!

Recently I came across a case that seems to not be covered by the extension. Not sure if this is the right places to create the issue. If it is not I would appreciate if you point me in the right direction!

What I would like to document are class (or instance) attributes, which seems to be possible though the formatting does not seem to be applied.

from typing_extensions import Literal

class ClassA(object):
    """docstring"""
    my_attr: int = 5
    """My attribute."""
    my_attr_2: Literal["bla", "foo", None] = "bla"
    """Another attribute."""

Which renders as:

Screenshot 2021-06-30 at 19 26 42

Note that int is also not formatted nicely.

A second issue is that for the literal type sphinx also raises errors:

scanpydoc/scanpydoc/elegant_typehints/example.py:docstring of scanpydoc.elegant_typehints.ClassA.my_attr_2:: 
WARNING: py:class reference target not found: bla
scanpydoc/scanpydoc/elegant_typehints/example.py:docstring of scanpydoc.elegant_typehints.ClassA.my_attr_2:: 
WARNING: py:class reference target not found: foo

(There are a few more warnings when I built these docs inside the scanpydoc/docs but I think are unrelated)

Additionally in python 3.7 (above I used python 3.8), it will also throws:

scanpydoc/scanpydoc/elegant_typehints/example.py:docstring of scanpydoc.elegant_typehints.ClassA.my_attr_2:: WARNING: py:class reference target not found: typing_extensions.Literal

@flying-sheep could you have a look?

caenrigen commented 3 years ago

@flying-sheep could you find a few minutes to give some guidance on this issue? 🥺

flying-sheep commented 3 years ago

Hi! Sorry for not responding for so long. I had a look back then but I couldn’t find where Sphinx was formatting those.

I think it might be happening here: https://github.com/sphinx-doc/sphinx/blob/9e1b4a8f1678e26670d34765e74edf3a3be3c62c/sphinx/ext/autodoc/__init__.py#L1939

But I wonder why you still get links if that’s the case … I have to dive into that.

caenrigen commented 3 years ago

Hi! Sorry for not responding for so long. I had a look back then but I couldn’t find where Sphinx was formatting those.

I think it might be happening here: https://github.com/sphinx-doc/sphinx/blob/9e1b4a8f1678e26670d34765e74edf3a3be3c62c/sphinx/ext/autodoc/__init__.py#L1939

But I wonder why you still get links if that’s the case … I have to dive into that.

Hi! Thank you very much for the reply and efforts :)

Let me know if you dig up some solution!

flying-sheep commented 3 years ago

I will!

flying-sheep commented 10 months ago

Hi! Since sphinx-autodoc-typehints now covers much more, I reduced elegant_typehints to only the qualname_overrides and typed return tuples. Also Sphinx now has its own typehint formatting!

If those don’t satisfy your needs, I think you should report it in one of those projects.