sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

Fully qualified types are generated with autodoc 'short' typehint format for class attributes

Open jzombi opened this issue 3 years ago • 5 comments

Describe the bug

When a python class attribute has a type annotation, the generated documentation will show the fully qualified class name, even with autodoc_typehints_format = 'short'.

Example:

class Bad:
    """
    :param d: This is my parameter.
    """

    d: D  #: My attribute

    def __init__(self, d: D):
        self.d = d

When napoleon is used (with default settings), the fully qualified class name appears even in the constructor (and supposedly any other method) parameters.

How to Reproduce

$ git clone https://github.com/jzombi/sphinx_typehint_attribute_bug
$ pip install sphinx
$ cd sphinx_typehint_attribute_bug/sphinx_typehint_attribute_bug/doc
$ make
$ # open html/mymodule.bad.html and see fully qualified attribute type
$ cd ../../sphinx_typehint_attribute_bug_napoleon/doc
$ make
$ # open html/mymodule.worse.html and see fully qualified attribute and parameter type

Expected behavior

With the autodoc_typehints_format = 'short' setting, all type names are kept short, and no fully qualified class names are shown.

Your project

https://github.com/jzombi/sphinx_typehint_attribute_bug

Screenshots

bad worse

OS

Linux

Python version

3.8.12

Sphinx version

4.5.0 - 40a8f2b54a048

Sphinx extensions

sphinx.ext.autodoc, sphinx.ext.napoleon

Extra tools

No response

Additional context

This quick&dirty patch solves the problem, but I'm not sure whether it has any bad side-effects, or whether it is the right way to do it.

bad_fixed worse_fixed

jzombi avatar Mar 24 '22 15:03 jzombi

Thank you for reporting.

At present, autodoc internally generates the following document:

.. py:class:: Bad(d)
   :module: mymodule.bad

   Bases: :py:class:`object`

   :param d: This is my parameter.


   .. py:attribute:: Bad.d
      :module: mymodule.bad
      :type: mymodule.a.b.c.d.D

      My attribute

It will be processed as a reST document. So it can be controled via python_use_unqualified_type_names setting. Could you try it again after setting python_use_unqualified_type_names = True? I hope it will get well. But I admit it's a workaround...

tk0miya avatar Mar 26 '22 17:03 tk0miya

Thank you for the hint, it indeed fixes the output. Now in both cases (without and with napoleon) I get the short type name for the attribute. This workaround is perfect for my use-case, I could not find any problematic side-effects. I'm leaving this ticket open, in case a fix without this config option is desired.

jzombi avatar Mar 28 '22 11:03 jzombi

I just ran into this issue myself as well, and the workaround worked for me too. To a sphinx newbie like me it is not clear at all what the difference is between autodoc_typehints_format and python_use_unqualified_type_names. I get different results with either and both enabled, but it's hard to see a pattern to it. (not asking for an explanation here - I'm happy to use the workaround and not look back - just raising it as a potential documentation gap)

ajyoon avatar Apr 12 '22 20:04 ajyoon

I had the same problem and the same workaround helped me, too. Thanks for the hint!

1kastner avatar Aug 03 '22 11:08 1kastner

A solution like proposed in the patch would be great though!

1kastner avatar Aug 05 '22 06:08 1kastner