python-sphinx

Documenting user created type declarations with Sphinx


I'm making a type declaration. For example:

from typing import Tuple
Type1 = Tuple[str, float]

I would like Sphinx to document this declaration like a function declaration. How can I do this? Thanks in advance.

If I bilded documentation and add usual description the result is that the declaration of these types is displayed as a plain text, not something special.


Solution

  • A canonical Python way is to use type keyword or TypeAlias from PEP-653:

    #
    # Python >= 3.12
    #
    
    #: Funny type that is mixing strings and floats - sounds like JavaScript programming!
    type NumberOrString = Tuple[str, float]
    
    
    #
    # 3.10 <= Python < 3.12
    #
    
    #: Funny type that is mixing strings and floats - sounds like JavaScript programming!
    NumberOrString: TypeAlias = Tuple[str, float]
    

    See type keyword in Python 3.12+.

    As the writing of this, Sphinx component that does autogenerated documentation, autodoc, does not support TypeAlias yet.