consider-using-namedtuple-or-dataclass / R6101#

Message emitted:

Consider using namedtuple or dataclass for dictionary values

Description:

Emitted when dictionary values can be replaced by namedtuples or dataclass instances.

Problematic code:

FELIDAES = {  # [consider-using-namedtuple-or-dataclass]
    "The queen's cymric, fragile furry friend": {
        "tail_length_cm": 1,
        "paws": 4,
        "eyes": 2,
        "Elizabethan collar": 1,
    },
    "Rackat the red, terror of the sea": {
        "tail_length_cm": 13,
        "paws": 3,
        "eyes": 1,
        "Red Hat": 1,
    },
}

Correct code:

from typing import NamedTuple


class FelidaeCharacteristics(NamedTuple):
    tail_length_cm: int
    paws: int
    eyes: int
    hat: str | None


FELIDAES = {
    "The queen's cymric, fragile furry friend": FelidaeCharacteristics(
        tail_length_cm=1, paws=4, eyes=2, hat="Elizabethan collar"
    ),
    "Rackat the red, terror of the sea": FelidaeCharacteristics(
        tail_length_cm=21, paws=3, eyes=1, hat="Red Hat"
    ),
}

Configuration file:

[MAIN]
load-plugins=pylint.extensions.code_style

Note

This message is emitted by the optional 'code_style' checker, which requires the pylint.extensions.code_style plugin to be loaded.

Created by the code_style checker.