Skip to content

--check-arg-defaults cannot be scoped to function arguments only #299

Description

@KelvinChung2000

Since 0.9.1, indent : int, default=0 fails under the default config; it passed in 0.9.0. Turning on --check-arg-defaults=True to restore it also switches on default-checking for class attributes, where some attributes cannot be made to pass at all. The only way out disables class-attribute checking entirely.

There is no configuration that gives us both argument-default checking and class-attribute checking.

Reproduction

from dataclasses import dataclass, field
from enum import StrEnum


class Colour(StrEnum):
    """A colour.

    Attributes
    ----------
    RED
        The colour red.
    """

    RED = "RED"


@dataclass
class Config:
    """Rendering configuration.

    Attributes
    ----------
    indent : int
        How far to indent.
    tags : list[str]
        Tags to emit.
    """

    indent: int = 0
    tags: list[str] = field(default_factory=list)


def render(colour: Colour, indent: int = 0) -> str:
    """Render a colour.

    Parameters
    ----------
    colour : Colour
        The colour to render.
    indent : int, default=0
        How far to indent.

    Returns
    -------
    str
        The rendered colour.
    """
    return " " * indent + colour
  1. Clean on 0.9.0.
$ uvx pydoclint@0.9.0 --style=numpy --quiet repro.py
$ echo $?
0
  1. On 0.9.1 the argument default is now rejected.
$ uvx pydoclint@0.9.1 --style=numpy --quiet repro.py
repro.py
    33: DOC105: Function `render`: Argument names match, but type hints in these args do not match: indent
  1. Enabling --check-arg-defaults fixes the function, but breaks both classes.
$ uvx pydoclint@0.9.1 --style=numpy --quiet --check-arg-defaults=True repro.py
repro.py
    5: DOC605: Class `Colour`: Attribute names match, but type hints in these attributes do not match: RED
   18: DOC605: Class `Config`: Attribute names match, but type hints in these attributes do not match: indent, tags
  1. The class docstrings cannot be fixed.

If we update to the following example:

from dataclasses import dataclass, field
from enum import StrEnum


class Colour(StrEnum):
    """A colour.

    Attributes
    ----------
    RED : , default='RED'
        The colour red.
    """

    RED = "RED"


@dataclass
class Config:
    """Rendering configuration.

    Attributes
    ----------
    indent : int, default=0
        How far to indent.
    tags : list[str], default=field(default_factory=list)
        Tags to emit.
    """

    indent: int = 0
    tags: list[str] = field(default_factory=list)


def render(colour: Colour, indent: int = 0) -> str:
    """Render a colour.

    Parameters
    ----------
    colour : Colour
        The colour to render.
    indent : int, default=0
        How far to indent.

    Returns
    -------
    str
        The rendered colour.
    """
    return " " * indent + colour

Everything passes under uvx pydoclint@0.9.1 --style=numpy --quiet --check-arg-defaults=True repro_fixed.py but

  • Config will need to have tags : list[str], default=field(default_factory=list), not with the default value.
  • Colour.RED needs to be type-hinted as , default='RED'. RED, RED : str, and RED : str, default='RED' all fails.
  1. So the only passing configuration is to turn class attributes off.
$ uvx pydoclint@0.9.1 --style=numpy --quiet \
      --check-arg-defaults=True --check-class-attributes=False repro.py
$ echo $?
0

That disables all DOC6xx checks, including attribute type-drift, which is the check we value most. Losing it to regain argument-default checking is a poor trade. How should I resovle this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions