# Python Dataclasses: a class that stores values

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-dataclasses-full-en
`@dataclass` writes `__init__`, `__repr__` and `__eq__` for a class from its annotated fields. The run used Python 3.10.12.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-85-A, TE-85-B and TE-85-C. The expected non-zero exit is TE-85-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-85-A: dataclass gives a constructor, a readable repr and equality

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
from dataclasses import dataclass
@dataclass
class P:
    x: int
    y: int
print(P(1, 2))
print(P(1, 2) == P(1, 2))
print(P(1, 2) == P(2, 1))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
P(x=1, y=2)
True
False
```

Exit status: 0. The output matches the expected text written before the run.

### TE-85-B: a field with a default value can be left out, and equality compares the fields

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
from dataclasses import dataclass
@dataclass
class P:
    x: int
    y: int = 5
print(P(1))
print(P(1) == P(1, 5))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
P(x=1, y=5)
True
```

Exit status: 0. This status and the output match what was written before the run.

### TE-85-C: expected non-zero exit: creating the dataclass without its required fields raises TypeError

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
from dataclasses import dataclass
@dataclass
class P:
    x: int
    y: int
P()
EOS
```

Command:

```bash
python3 s.py
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
Traceback (most recent call last):
  File "/tmp/b22-58av6b37/work/s.py", line 6, in <module>
    P()
TypeError: P.__init__() missing 2 required positional arguments: 'x' and 'y'
```

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-85-B: P(1) used the default y=5, printed P(x=1, y=5), and compared equal to P(1, 5). TE-85-C: P() with no arguments stopped with exit status 1. The last line of standard error is TypeError: P.__init__() missing 2 required positional arguments: 'x' and 'y'.

## Limits

The run used Python 3.10.12. TE-85-B runs one default value. `field()`, `frozen=True` and ordering were not tested.

## Source

- https://docs.python.org/3/library/dataclasses.html

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-85-A` | dataclass gives a constructor, a readable repr and equality | 0 | PASS | `aa3ef0defaf0` |
| `TE-85-B` | a field with a default value can be left out, and equality compares the fields | 0 | PASS | `76b73ccb9290` |
| `TE-85-C` | expected non-zero exit: creating the dataclass without its required fields raises TypeError | 1 | PASS | `b5b021d0eaab` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python sys.exit: set the exit status](https://kairos-2z6s.onrender.com/res/python-sys-exit-full-en)
- [Python Defaultdict: a dict that fills missing keys](https://kairos-2z6s.onrender.com/res/python-defaultdict-full-en)
- [Python dates: parse and compare](https://kairos-2z6s.onrender.com/res/python-dates-full-en)
- [Python csv module: read and write rows with quoting rules](https://kairos-2z6s.onrender.com/res/python-csv-full-en)
- [Python: count with a dict or Counter, and meet sets and tuples](https://kairos-2z6s.onrender.com/res/python-counting-sets-tuples-full-en)
