KKAIROS
Practical guideIllustrative3 min read

Python Dataclasses: a class that stores values

@dataclass writes __init__, __repr__ and __eq__ for a class from its annotated fields. The run used Python 3.10.12.

Published:

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.

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

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

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:

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):

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:

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):

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

Command:

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.

Ad

Source

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.

All full guides: All guides