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

Class data members are included in the documentation when the type is explicitly defined. #280

Closed
ravenexp opened this issue Jul 7, 2021 · 5 comments
Labels

Comments

@ravenexp
Copy link

ravenexp commented Jul 7, 2021

Problem Description

As I understand it, pdoc does not document the class data members by default.
But, when the data member type is specified explicitly in __init__(), pdoc documents it by default.

Steps to reproduce the behavior:

class Foo:
    def __init__(self) -> None:
        """
        Foo() constructor
        """

        self.a = 0
        self.b: int = 0

Data member a is not included in the Foo class documentation, but data member b is.

System Information

pdoc: 7.1.1
Python: 3.9.6
Platform: Linux-5.12.14-arch1-1-x86_64-with-glibc2.33
@ravenexp ravenexp added the bug label Jul 7, 2021
@mhils
Copy link
Member

mhils commented Jul 15, 2021

Hi there!

This is more or less expected behavior. pdoc documents all attributes that have either a docstring or a type annotation. We don't document members for which neither is present, mostly because there's really not much to document. If you want members to not be public, I'd recommend you prefix them with an underscore, which is a more general Python convention. Does that make sense?

@ravenexp
Copy link
Author

Probably. I wasn't prefixing the data members with underscores (as I should have been), but I also use type annotations everywhere. It didn't bother me at all because the type of the data members was correctly inferred in 99% of cases. But then I needed something like foo: Optional[Foo] = None and it stood out like a sore thumb...

@mhils
Copy link
Member

mhils commented Jul 15, 2021

By "I also use type annotations everywhere", you mean everywhere but for attributes/data members?

I guess I do have some sympathy for your use case. A reasonable change here would be to only add variables with docstrings instead of adding variables that have a docstring, an annotation, or both. I think it should be enough to remove those four lines:

pdoc/pdoc/doc.py

Lines 475 to 476 in fdb6fde

for name in self._var_annotations:
members.setdefault(name, empty)

pdoc/pdoc/doc.py

Lines 583 to 584 in fdb6fde

for name in self._var_annotations:
unsorted.setdefault(name, empty)

I feel both approaches are valid, maybe other folks want to chime in. :)

@ravenexp
Copy link
Author

By "I also use type annotations everywhere", you mean everywhere but for attributes/data members?

There's no need, because the data members are typically defined like

def __init__(self):
    ...
    self.foo = Foo()
    ...

where the foo data type is automatically inferred from the RHS of the assignment.

@mhils
Copy link
Member

mhils commented Jul 28, 2021

You've convinced me here, this behavior will be changed with the next release!

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

No branches or pull requests

2 participants