Skip to content

clarify frozen class immutability behavior with factories#1565

Open
sahvx655-wq wants to merge 2 commits into
python-attrs:mainfrom
sahvx655-wq:frozen-factory-shallow-immutability
Open

clarify frozen class immutability behavior with factories#1565
sahvx655-wq wants to merge 2 commits into
python-attrs:mainfrom
sahvx655-wq:frozen-factory-shallow-immutability

Conversation

@sahvx655-wq
Copy link
Copy Markdown

This pull request clarifies the behavior of frozen=True classes when factory defaults return mutable objects that are shared.
In attrs, frozen=True provides shallow immutability (preventing attribute reassignment), but it does not make the contents of mutable objects (like lists or dicts) immutable. If a factory returns a shared mutable object, all instances of the frozen class share that object, and external mutations will affect all frozen instances.
This PR adds documentation and a doctest to clarify this behavior.
Changes
Added a new Factories and Frozen Classes subsection under the Defaults section in docs/init.md to explain shallow immutability and document this behavior with a clear doctest example.
Key Points
frozen=True does not prevent changes inside mutable objects.
Factory defaults that return shared mutable references lead to shared-state behavior across frozen instances.

@sahvx655-wq sahvx655-wq force-pushed the frozen-factory-shallow-immutability branch from 43ac362 to 17d7047 Compare May 28, 2026 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant