pdoc icon indicating copy to clipboard operation
pdoc copied to clipboard

Properties are shown as var

Open johann-petrak opened this issue 5 years ago • 4 comments

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.

johann-petrak avatar Oct 20 '20 21:10 johann-petrak

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. https://github.com/pdoc3/pdoc/blob/d776cbdf53f2364897647fd404b2d70626c31592/pdoc/init.py#L985-L993 It's a major simplification, which I kind of like, but otherwise agree with all your points. :confused:

kernc avatar Oct 20 '20 23:10 kernc

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.

johann-petrak avatar Oct 21 '20 06:10 johann-petrak

Unlike pdoc.Function.funcdef(): https://github.com/pdoc3/pdoc/blob/d776cbdf53f2364897647fd404b2d70626c31592/pdoc/init.py#L1235-L1240 the 'var' before variables comes from the template: https://github.com/pdoc3/pdoc/blob/d776cbdf53f2364897647fd404b2d70626c31592/pdoc/templates/html.mako#L235-L237 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.

kernc avatar Oct 21 '20 11:10 kernc

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 https://github.com/pdoc3/pdoc/pull/277

johann-petrak avatar Oct 21 '20 13:10 johann-petrak