tox-dev / sphinx-autodoc-typehints

Type hints support for the Sphinx autodoc extension
MIT License
558 stars 106 forks source link

Support class variables #32

Closed Codingboy closed 1 year ago

Codingboy commented 6 years ago

The Code below produces the output in the image. The type of pw (List[int]) is correct but the types of encodeMap and decodeMap are missing.

class SBox:
    """
    SBox is a substitution cipher.

    Attributes:
        encodeMap: lookuptable used to encode data
        decodeMap: lookuptable used to decode data

    Parameters:
        pw: password

    | **Pre:**
    |   len(pw) == 256
    |   pw[i] >= 0
    |   pw[i] < 256

    | **Post:**
    |   len(self.encodeMap) == 256
    |   self.encodeMap[i] >= 0
    |   self.encodeMap[i] < 256
    |   len(self.decodeMap) == 256
    |   self.decodeMap[i] >= 0
    |   self.decodeMap[i] < 256
    """

    def __init__(self, pw:List[int]):
        self.encodeMap:List[int] = [-1]*256
        self.decodeMap:List[int] = [-1]*256

sbox

agronholm commented 6 years ago

This library was created prior to the introduction of class variables. The fact that there is no support is not a bug, but it might be nice to add it at some point.

gpkc commented 6 years ago

These are not class variables, but instance variables. Anyway, this is a feature I was looking for too.

pleonex commented 6 years ago

It would be possible to implement for class variables because the type hints are stored in class.__annotations__ and get_type_hints(Foo) returns them.

This is not the case for instance variables. Since the type hints are analyzed in the current context and Sphinx doesn't create actual instances of the classes, we can't retrieve which variables were defined in the __init__ or any other method without running it. I think it would require source code parsing.

ghost commented 5 years ago

Is there any update for this? There does not appear to be any plugin that can do this for Sphinx, which is overly frustrating for documentation of dataclass fields within classes, since it partially defeats the object of being able to use type hints if you then also need to add them to the documentation manually.

An example from a project I am working on is as follows:

@dataclasses.dataclass()
class Guild(base.Snowflake):
    #: The application ID of the creator of the guild. This is always `None` unless the guild was made by a bot.
    creator_application_id: typing.Optional[int]
    #: The name of the guild.
    name: str
    #: The hash of the icon of the guild.
    icon_hash: str
    #: The hash of the splash for the guild.
    splash_hash: str
    #: Permissions for our user in the guild, minus channel overrides, if the user is in the guild.
    my_permissions: typing.Optional[permission.Permission]
    #: Timeout before a user is classed as being AFK in seconds.
    afk_timeout: int
    #: Verification level for this guild.
    verification_level: VerificationLevel
    #: The preferred locale of the guild
    preferred_locale: typing.Optional[str]
    #: Default level for message notifications in this guild.
    message_notification_level: MessageNotificationLevel
    #: Explicit content filtering level.
    explicit_content_filter_level: ExplicitContentFilterLevel
    #: Roles in this guild.
    roles: typing.Dict[int, "role.Role"]
    #: Emojis in this guild.
    emojis: typing.Dict[int, "emoji.Emoji"]
    #: Enabled features in this guild.
    features: typing.List[GuildFeature]
    #: Number of members.
    member_count: typing.Optional[int]
    #: MFA level for this guild.
    mfa_level: MFALevel
    #: The date/time the bot user joined this guild, if it is in the guild.
    joined_at: typing.Optional[datetime.datetime]
    ...

which produces the following format in our documentation: image

Unfortunately this makes the documentation not overly useful to us without having to add the types in the comments as well, which is error prone and easy to miss when adjusting types used within models.

If there are any ways around this, they would be greatly appreciated.

agronholm commented 5 years ago

Unfortunately I have been busy with other projects since the last release of sphinx-autodoc-typehints 5 weeks ago. I will get to this eventually but a quality PR would surely expedite things.

sdementen commented 4 years ago

+1

felix-hilden commented 4 years ago

This issue looks a lot like #44, for which I just posted at least a partial solution from Sphinx 3 (dataclasses). Though I've not tested it with variables defined in init.

gaborbernat commented 2 years ago

A PR for this would we welcome.

hoodmane commented 1 year ago

I think I resolved this in #299.