Why This Happens
Frozen dataclasses (frozen=True) are immutable. All fields are set in __init__ and cannot be changed. Use replace() to create new instances with modified values.
The Problem
from dataclasses import dataclass
@dataclass(frozen=True)
class Point:
x: float
y: float
p = Point(1.0, 2.0)
p.x = 3.0The Fix
from dataclasses import dataclass, replace
@dataclass(frozen=True)
class Point:
x: float
y: float
p = Point(1.0, 2.0)
p2 = replace(p, x=3.0)Step-by-Step Fix
- 1
Use replace()
Create new instances: replace(old, field=value).
- 2
Remove frozen if not needed
Use @dataclass without frozen=True for mutable instances.
- 3
Keep frozen for immutability
Use frozen=True for hashable, thread-safe objects.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free