Use frozen=True to make the dataclasses immutable and hashable.
With @dataclass(frozen=True) then assigning to fields after the object has been instantiated will raise a FrozenInstanceError. This emulates read-only frozen instances, and gives the advantages of immutability.
Frozen dataclass will also generate a __hash__() method on the class. This allows the instance to be used in set and dict objects. Attempting to add a non-hashable object to a dict will result in TypeError: unhashable type
An immutable object is an object that cannot be modified. Strings are an example of an immutable object. There are some advantages to immutability:
If our GitHub code review bot spots this issue in your pull request it gives this advice:
1 | + | from dataclasses import dataclass | |
2 | + | ||
3 | + | @dataclass |
Use frozen=True to make the dataclasses immutable and hashable.
4 | + | class FooBarClass: | |
5 | + | pass |



