pdoc icon indicating copy to clipboard operation
pdoc copied to clipboard

Don't show `dataclasses.InitVar` as a class member

Open elliot-100 opened this issue 9 months ago • 1 comments

Problem Description

Given these definitions which are intended to do very similar things:

from dataclasses import dataclass, field, InitVar


class Dog:
    name: str

    def __init__(self, name: str, id: int):
        self.name = name
        self.vet_code: str = f"{self.name}-{id}"

@dataclass
class Cat:
    name: str
    id: InitVar[int]
    vet_code: str = field(init=False)

    def __post_init__(self, id):
        self.vet_code = f"{self.name}-{id}"

pdoc will produce output like:

class Dog:
    Dog(name: str, id: int)
    name: str
    vet_code: str

@dataclass
class Cat:
    Cat(name: str, id: dataclasses.InitVar[int])
    name: str
    id: dataclasses.InitVar[int]
    vet_code: str

i.e. Cat.id is included as a class member, even though it is type-annotated as an init-only variable.

Proposal

Suppress dataclasses.InitVar class members from output.

Alternatives

We can prefix the InitVar name with _, which will hide it in pdoc output by default...

Additional context

...but it would be nice to not have to do that.

elliot-100 avatar Mar 22 '25 20:03 elliot-100

Sounds fair to me, contributions welcome!

mhils avatar Apr 09 '25 07:04 mhils