question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dataclass member documentation issues

See original GitHub issue

Problem Description

When using the @dataclass decorator from the dataclasses module, pdoc seems to think that member (instance) variables with default values are actually class variables. Furthermore, when using the typing.ClassVar hint, the class variable is not picked up by the parser (unless it has a default, but apparently specifying a default isn’t a requirement for the code to run).

Steps to reproduce the behavior:

A minimal example is provided below:

from dataclasses import dataclass
from typing import ClassVar


@dataclass
class Example:
    value: float
    n: int = 1
    m: int = 2
    asdf: ClassVar


class OldExample:
    n: int = 2
    m: int = 2
    asdf: ClassVar

    def __init__(self):
        pass

Generating the documentation gives: image Note that neither n nor m should be shown in the docs for Example (unless they have docstrings attached, as per the docs), while asdf should always be shown in the docs as it is a class variable (as specified by its type hint), even if it has no value assigned (the code runs fine as long as one doesn’t try to access asdf).

System Information

pdoc: 12.0.2
Python: 3.9.13
Platform: Linux-5.16.0-1-amd64-x86_64-with-glibc2.33

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mhilscommented, Sep 10, 2022

Fixed in #436!

0reactions
JCGorancommented, Aug 17, 2022

Any updates on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

dataclasses — Data Classes — Python 3.11.1 documentation
The dataclass() decorator examines the class to find field s. A field is defined as a class variable that has a type annotation....
Read more >
dataclass member variables are documented out of order #9853
Describe the bug class members documented via autoclass or similar will be reordered during documentation (apparently alphabetically).
Read more >
Support documentation strings for Data Classes (PEP 557)
PyCharm inspection complains about unsolved references in the dataclass class docstring. For example: from dataclasses import dataclass @dataclass class ...
Read more >
How do I document a constructor for a class using Python ...
I have __init__ methods with nice docstring documentation, specifying the attributes the constructors take and their types. However, if I change ...
Read more >
Data Classes in Python 3.7+ (Guide)
To avoid this, make sure all fields of an immutable data class use immutable types (but remember that types are not enforced at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found