Skip to content

python/record-without-namedtuple

Rule record-without-namedtuple
Pack Python
Severity warning
Fix none, edit by hand
Library versions every version

A frozen dataclass or attrs class that only holds fields can be a NamedTuple, which is cheaper to define and build. Write class Name(NamedTuple).

from dataclasses import dataclass
from typing import NamedTuple
@dataclass(frozen=True, slots=True)
class Order:
class Order(NamedTuple):
price: float
quantity: int = 1

The rule reports a class with @dataclass(frozen=True) and a frozen attrs class. It leaves alone a class that a NamedTuple cannot replace: a dataclass that is not frozen, a class that inherits from another class, a class that calls field(), and a class with __post_init__.

xpd has no automatic fix, so make the change by hand.

A NamedTuple comes from the standard library, holds the same fields, cannot be changed, and keeps methods, class methods and properties. A JP Hutchins measurement on one machine found that Python defined a NamedTuple in 76.2 microseconds and a frozen dataclass in 373.4, and built an instance in 138 nanoseconds and 224. An instance takes 88 bytes, and an instance of a dataclass with slots takes 64. The table gives the NamedTuple name for each part of a dataclass.

dataclass NamedTuple
@dataclass(frozen=True, slots=True) class Order(NamedTuple)
dataclasses.replace(order, price=3.0) order._replace(price=3.0)
dataclasses.asdict(order) order._asdict()
dataclasses.fields(Order) Order._fields
Order(*row) for a row from a file Order._make(row)

A NamedTuple is also a tuple. It iterates over its fields, has a length, unpacks, and is equal to a plain tuple with the same values. Check each place that tests a record with isinstance(value, tuple), formats it with %, or passes a list of records to np.asarray, which turns the fields into columns. A field name cannot start with an underscore.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This frozen record can be a NamedTuple. Write class Name(NamedTuple), which holds the same fields, is cheaper to define and to build, and needs no decorator.

Browse the other rules of the Python pack.