Registry / serialization / immutabledict

immutabledict

JSON →
library4.3.1pypypi✓ verified 24d ago

immutabledict is a Python library providing an immutable wrapper around dictionaries. It functions as a drop-in replacement for standard dictionaries where immutability is desired, implementing the complete mapping interface. Forked from `frozendict`, `immutabledict` offers an MIT-licensed alternative to the LGPL-3.0 licensed original. It also includes `ImmutableOrderedDict` for order-preserving immutable mappings. The library is actively maintained with frequent minor and patch releases.

pip install immutabledict
INSTALL
IMPORT
SIG · IMMUTABLEDICT
I
immutabledict
serializationpythonv4.3.1
Install
1.5s avg
Import
11ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.3.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

immutabledict
from immutabledict import immutabledict
ImmutableOrderedDict
from immutabledict import ImmutableOrderedDict
For an order-preserving immutable dictionary.

Demonstrates how to create an immutabledict and shows that direct modification attempts will result in a TypeError. It also illustrates how to create a new immutabledict based on an existing one with updates.

from immutabledict import immutabledict # Create an immutable dictionary my_item = immutabledict({"a": "value", "b": "other_value"}) print(f"Value for 'a': {my_item['a']}") # Attempting to modify an immutabledict will raise a TypeError try: my_item["c"] = "new_value" except TypeError as e: print(f"Attempted modification failed: {e}") # You can create a new immutabledict with changes using dictionary union operator (PEP 584) new_item = my_item | {"c": "another_value"} print(f"New item (original unchanged): {new_item}") print(f"Original item: {my_item}")
Debug
Known issues
breakingThe `copy()` method signature was changed to no longer accept keyword arguments, aligning its behavior with the standard `dict.copy()` method. Previously, `copy(key='value')` would add `key: 'value'` to the copied dictionary.
fix
Instead of `my_immutabledict.copy(key='value')`, use `my_immutabledict | {'key': 'value'}` for simple additions, or `immutabledict({**my_immutabledict, 'key': 'value'})` for more complex updates or Python versions older than 3.9 where the `|` operator is not available for dicts.
affects: 3.0.0 and later
breakingSupport for Python 3.7 was officially dropped.
fix
Upgrade your Python environment to version 3.8 or newer.
affects: 3.0.0 and later
gotchaWhile `immutabledict` ensures the dictionary's structure (keys and their assigned values) is immutable, it does *not* recursively make the *values* themselves immutable. If you store mutable objects (e.g., lists, other dictionaries) as values, those objects can still be modified in place through their references.
fix
To achieve deep immutability, ensure that all values stored within an `immutabledict` are themselves immutable types (e.g., numbers, strings, tuples, or other `immutabledict` instances).
affects: All versions
gotchaOperations that appear to 'modify' an `immutabledict` (like `set()`, `delete()`, `discard()`, or using the `|` operator) actually return a *new* `immutabledict` instance with the changes, rather than modifying the original in-place. This can incur performance overhead due to repeated object creation and copying if used in tight loops or scenarios with very frequent updates.
fix
Consider the performance implications for high-frequency update patterns. For scenarios requiring frequent mutable operations before a final 'freeze', a standard `dict` might be more appropriate, converted to `immutabledict` only when immutability is needed for sharing or hashing.
affects: All versions
gotchaStarting from v4.0.0, the internal implementation of `immutabledict` switched from overriding `__init__` to overriding `__new__` for constructor calls. While this is primarily an internal refactoring, it's a significant change that could affect advanced use cases such as highly specialized subclassing or detailed introspection that relied on the previous `__init__` behavior.
fix
If you maintain custom subclasses that override `__init__` or `__new__`, review their implementation to ensure they correctly chain to `super().__new__` in the base `immutabledict` class for proper initialization.
affects: 4.0.0 and later
Errors
Common errors & fixes
TypeError: 'immutabledict' object does not support item assignment
You are attempting to modify an immutabledict instance after its creation, which is not allowed as immutabledict objects are designed to be unchangeable.
fix
Instead of modifying in-place, create a new immutabledict with the desired changes. You can use dictionary union (`|`) or recreate the object.
AttributeError: 'immutabledict' object has no attribute 'update'
You are trying to call a mutable dictionary method (like `update`, `setdefault`, `pop`, `clear`) on an immutabledict instance, which does not support these in-place modification methods.
fix
To achieve a similar effect, construct a new immutabledict from the existing one with the desired additions or modifications. For merging, use the dictionary union operator (`|`) or dictionary unpacking.
KeyError: 'some_key'
You are trying to access a key in the immutabledict that does not exist.
fix
Before accessing a key, check for its existence using `in` or use the `.get()` method to provide a default value if the key is not found.
ModuleNotFoundError: No module named 'frozendict'
The project or a dependency is trying to import `frozendict`, but `immutabledict` is installed, or you are migrating from `frozendict` to `immutabledict` and haven't updated the import statements.
fix
Change the import statement to `from immutabledict import immutabledict` or install `frozendict` if that is indeed the intended library.
Upgrade
Version history
4.3.1latest on PyPI · released Feb 15, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources