joel
joel

Reputation: 7867

How can I document type parameters in Python?

I'd like to document what T means in this class

from typing import Generic, TypeVar

T = TypeVar('T')

class A(Generic[T]):
    pass

I could document T with T.__doc__ = "Represents ..." but I might want to use T it in several unrelated classes.

Is there any standard way to document it for a given class/function etc? Ideally using sphinx & reST.

I'm thinking something at the same level as sphinx's :param:, but for types, along the lines of scaladoc's @tparam

Upvotes: 4

Views: 1864

Answers (1)

Matteo Peluso
Matteo Peluso

Reputation: 452

You could document it through the multiline string as suggested in this way:

from typing import Generic, TypeVar

T = TypeVar('T')

class A(Generic[T]):
"""
Description of the class and his input T

Inputs:
    T : what is T? the type of T

Returns:
    res 

"""

And you could call the documentation as:

A.__doc__

or

help(A)

Upvotes: 2

Related Questions