Sphinx: Support for `TypedDict` Classes

Created on 15 Apr 2020  ·  6Comments  ·  Source: sphinx-doc/sphinx

Is your feature request related to a problem? Please describe.
Python 3.8 added typing.TypedDict which you can subclass to define a dictionary type. This type associates the classes arguments as keys with the given types for their values like so:

class DictType(TypedDict):
    arg1: str
    arg2: int

d = {"arg1": "string", "arg2": 1}

When this Type is used its referenced in the build docs with the full path to the TypedDict subclass which is great. It even creates a hyperlink when you have the subclass also 'autodoced' which is even greater. BUT the 'autodoced' subclass is empty except its docstring. I cannot find a way to include the empty arguments to document the types.

Describe the solution you'd like
.. autoattribute:: does not work because the attributes must not have values and so the attributes do not actually exist. The type hints however are available with class.__annotations__.

So could one add a check to the .. autoattribute:: directive to check if the class is a subclass of TypedDict and if so extract from class.__annotations__ instead of class.arg?
Or just add another directive like .. autotype:: (or called whatever) to extract always from class.__annotations__ so it could be used for other classes as well whatever good this does.

Describe alternatives you've considered
Currently I only see the possibility to document the types in the docstring of the TypedDict subclass which is redundant work.

Additional context
Add any other context or screenshots about the feature request here.

  • [e.g. URL or Ticket]
autodoc enhancement

Most helpful comment

How about this?

.. module:: example

.. autoclass:: DictType
   :members:
   :undoc-members:

On my local, it works fine:
スクリーンショット 2020-04-16 22 24 39

All 6 comments

Please try latest release and give :undoc-members: option. I think autodoc has already supported it.

Unfortunately it does not help :(

This is what the part in my .rst file looks like:

.. module:: package.module

.. autoclass:: ClassName
    :undoc-members:

And in the HTML output there still is only class package.module.ClassName and the docstring beneath.

I think it does not work because the 'annotation arguments' are not assigned to the class object as arguments but only in __annotations__.

How about this?

.. module:: example

.. autoclass:: DictType
   :members:
   :undoc-members:

On my local, it works fine:
スクリーンショット 2020-04-16 22 24 39

Yeah 👍 it works like you posted ! Thanks !

I don't quiet like that there are = None behind because they don't have an assigned value at all, but that's minor and I don't know what processes are behind, that result in this, which may be important in other scenarios.

If you have an idea how to easily remove the = None in those TypedDict cases I would appreciate it if you could tell me else I think this issue can be closed.

Now I changed Sphinx to suppress = None in the development version (refs: #7515). So please wait a moment for the next release.

Thanks :)

Was this page helpful?
0 / 5 - 0 ratings