Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support class variables #32

Closed
Codingboy opened this issue Jan 31, 2018 · 9 comments
Closed

Support class variables #32

Codingboy opened this issue Jan 31, 2018 · 9 comments

Comments

@Codingboy
Copy link

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
Copy link
Collaborator

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.

@agronholm agronholm changed the title typehint support not working for class variables Support class variables Jan 31, 2018
@gpkc
Copy link

gpkc commented Apr 9, 2018

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

@pleonex
Copy link
Contributor

pleonex commented Oct 2, 2018

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
Copy link

ghost commented Sep 1, 2019

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
Copy link
Collaborator

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
Copy link

+1

@felix-hilden
Copy link

felix-hilden commented Jun 6, 2020

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
Copy link
Member

A PR for this would we welcome.

@hoodmane
Copy link
Collaborator

I think I resolved this in #299.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants