mitmproxy / mitmproxy/pdoc

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

Open
#790 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
2.5k
Forks
228
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no implementation files or tests. Reproduce the documented Dog and Cat example in pdoc, then trace how dataclasses members are collected and rendered; done means the InitVar id is absent from Cat's class members while the constructor still shows it and other members remain.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.