FrozenInstanceError: Cannot Assign to Field

dataclasses.FrozenInstanceError: cannot assign to field 'name'

Quick Answer

You are modifying a frozen dataclass which is immutable. Use dataclasses.replace() to create a modified copy.

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.0

The 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. 1

    Use replace()

    Create new instances: replace(old, field=value).

  2. 2

    Remove frozen if not needed

    Use @dataclass without frozen=True for mutable instances.

  3. 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