frigidΒΆ

Package Version PyPI - Status Tests Status Code Coverage Percentage Project License Python Versions

πŸ”’ A Python library package which provides immutable data structures - collections which cannot be modified after creation.

Key Features ⭐¢

  • πŸ“– Immutable Dictionary: Like a regular dict, but entries cannot be modified or removed. Also has variant for validation on initialization. And provides set operations not found on MappingProxyType.

  • πŸ“‡ Immutable Namespace: Similar to SimpleNamespace, but attributes are immutable from creation.

  • 🧱 Additional Types: Classes (including abstract base classes), modules, and objects with immutable behavior.

  • πŸ—οΈ Flexible Initialization: Support for unprotected attributes during initialization; useful for compatibility with class decorators, such as dataclasses.

  • πŸ”“ Flexible Mutability: Support for declaring specific attributes as mutable, enabling selective modification while maintaining immutability for other attributes.

Installation πŸ“¦ΒΆ

pip install frigid

Note on Immutability πŸ“’ΒΆ

Enforcement of immutability is quite difficult in Python. While this library enforces immutability by default, it can be circumvented by anyone who has intermediate knowledge of Python machinery and who is determined to circumvent the immutability. Use the library in the spirit of making programs safer, but understand that it cannot truly prevent unwanted state tampering.

Examples πŸ’‘ΒΆ

Immutable Namespace πŸ“‡ΒΆ

An immutable namespace, similar to types.SimpleNamespace, is available. This namespace is initialized from keyword arguments and becomes completely immutable. (Keyword arguments shown below; see documentation for additional forms of initialization.)

>>> from frigid import Namespace
>>> ns = Namespace( apples = 12, bananas = 6 )
>>> ns.cherries = 42    # ❌ Attempted assignment raises error.
Traceback (most recent call last):
...
frigid.exceptions.AttributeImmutabilityError: Cannot assign attribute 'cherries'.
>>> del ns.apples       # ❌ Attempted deletion raises error.
Traceback (most recent call last):
...
frigid.exceptions.AttributeImmutabilityError: Cannot delete attribute 'apples'.
>>> ns
frigid.namespaces.Namespace( apples = 12, bananas = 6 )

Immutable Dictionary πŸ“–ΒΆ

An immutable dictionary, similar to dict, is available. This dictionary is initialized from iterables and keyword arguments and becomes completely immutable. (Keyword arguments shown below; see documentation for additional forms of initialization.)

>>> from frigid import Dictionary
>>> dct = Dictionary( apples = 12, bananas = 6)
>>> dct['cherries'] = 42  # ❌ Attempted assignment raises error.
Traceback (most recent call last):
...
frigid.exceptions.EntryImmutabilityError: Cannot assign entry for 'cherries'.
>>> del dct['bananas']    # ❌ Attempted removal raises error.
Traceback (most recent call last):
...
frigid.exceptions.EntryImmutabilityError: Cannot delete entry for 'bananas'.
>>> dct
frigid.dictionaries.Dictionary( {'apples': 12, 'bananas': 6} )

Use Cases 🎯¢

  • πŸ”’ Configuration Objects: Objects which must maintain consistent state throughout program execution.

  • πŸ“Š Value Objects: Objects which represent values and should be immutable, like numbers or strings.

  • 🧱 Immutable Collections: Many scenarios requiring collections with complete immutability guarantees.

More FlairΒΆ

GitHub last commit Copier Hatch pre-commit Bandit Pylint Pyright Ruff Hypothesis PyPI - Implementation PyPI - Wheel

Table of ContentsΒΆ

IndicesΒΆ