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

Properties are shown as var #276

Open
johann-petrak opened this issue Oct 20, 2020 · 4 comments · May be fixed by #277
Open

Properties are shown as var #276

johann-petrak opened this issue Oct 20, 2020 · 4 comments · May be fixed by #277
Labels
enhancement New feature or request Hacktoberfest https://hacktoberfest.digitalocean.com

Comments

@johann-petrak
Copy link

johann-petrak commented Oct 20, 2020

The generated documentation for a property:

@property
def weight(self) -> int:
...

is shown as "var weight : int" but it would be more useful if the fact that this is a property would get shown, e.g.
"property weight: int" as the Sphinx generator does.

This is also shown under the header "Instance variables" which is not exactly right.

Also, if there is a separate setter for the property:

@weight.setter
def weight(self, weight):
...

this is not reflected in the documentation, so there is no way to see from the documentation if the property can only be read or set as well. Only the docstring of the getter method is included in the generated documentation.

@kernc
Copy link
Member

kernc commented Oct 20, 2020

This part came from the original pdoc and so far hasn't been touched (or, at least, revised into a different manner). We only know about not even a handful of objects: modules, classes, functions and variables.

pdoc/pdoc/__init__.py

Lines 985 to 993 in d776cbd

self.doc[name] = Variable(
name, self.module,
docstring=(
var_docstrings.get(name) or
(inspect.isclass(obj) or _is_descriptor(obj)) and inspect.getdoc(obj)),
cls=self,
obj=getattr(obj, 'fget', getattr(obj, '__get__', None)),
instance_var=(_is_descriptor(obj) or
name in getattr(self.obj, '__slots__', ())))

It's a major simplification, which I kind of like, but otherwise agree with all your points. 😕

@kernc kernc added the enhancement New feature or request label Oct 20, 2020
@johann-petrak
Copy link
Author

Hmm, I do not know much about Python internals but for a member of some class C, I think it is not too hard to find out of the
member is a variable or property:

class C1: 
     a = 1 
     def b(self): 
         return 2 
     @property 
     def c(self): 
         return 3 

def whatisit(clazz, membername): 
    return dict(inspect.getmembers(clazz))[membername]

assert isinstance(whatisit(C1,"c") property)

So this could still get listed under "Instance variables" but with a prefix "property" instead of "var".

It is probably harder to figure out of the property also has a setter or not. If this can be done, then it would be cool to have "read only property" in the docs if there is no setter and otherwise just "property", I think. Or some other way to indicate this.

@kernc
Copy link
Member

kernc commented Oct 21, 2020

Unlike pdoc.Function.funcdef():

pdoc/pdoc/__init__.py

Lines 1235 to 1240 in d776cbd

def funcdef(self) -> str:
"""
Generates the string of keywords used to define the function,
for example `def` or `async def`.
"""
return 'async def' if self._is_async else 'def'

the 'var' before variables comes from the template:
% for v in inst_vars:
<% return_type = get_annotation(v.type_annotation) %>
<dt id="${v.refname}"><code class="name">var ${ident(v.name)}${return_type}</code></dt>

And that's not the place for further complex logic. (Although you're welcome to suit it to your needs.)

It's not hard to discern properties, access setters too, but there's currently no extra supporting API in place; this would probably not be a minor/trivial change. But if you're willing to work on it, I'd certainly have a look!

The reasoning behind properties being instance variables is that they do, normally, depend on (some computation upon) self.

@johann-petrak
Copy link
Author

Thanks!

I have implemented something for this which works for me but I am not sure that I have not overlooked
some important cases or situations I am less familiar with.

The concrete way how to show this is obviously a matter of taste. I would very much like it if the documentation would indicate read-only properties (which only have a getter) vs. other properties which may have a setter and/or a getter.

In my fix i have called the former "ro-property" and the latter just "property".

See #277

@kernc kernc linked a pull request Oct 26, 2020 that will close this issue
@kernc kernc added the Hacktoberfest https://hacktoberfest.digitalocean.com label Oct 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Hacktoberfest https://hacktoberfest.digitalocean.com
Development

Successfully merging a pull request may close this issue.

2 participants