Skip to main content

PyPI version shields.io PyPI pyversions

arcticfreeze

Note: This is a fork of KerstenBreuer/arcticfreeze maintained by the German Human Genome-Phenome Archive (GHGA). It was created to bring in fixes and updates required by GHGA-related projects. Many thanks to Kersten Breuer for the initial implementation. The distribution is published as ghga-arcticfreeze, but the import path remains arcticfreeze, so it is a drop-in replacement for the upstream package.

Enjoy Python on the rocks with deeply (recursively) frozen data structures.

Description

Python's built-in immutable types only go one level deep: a tuple may still contain a list, and a frozenset cannot contain a dict at all. arcticfreeze closes that gap by recursively converting a nested data structure into an immutable counterpart.

It provides:

  • freeze – a function that deep freezes an arbitrary object. It walks the object tree bottom-up and replaces every mutable container with an immutable equivalent (list/dequetuple, dictFrozenDict, setfrozenset), leaving already-immutable values untouched.
  • FrozenDict – a hashable, immutable Mapping (built on immutabledict) with first-class type-hint support and out-of-the-box Pydantic v2 integration (validation, JSON schema, and serialization).
  • Converter – an extension point for teaching freeze how to handle your own types, including a priority mechanism to override the standard converters.

Freezing is useful wherever shared data must not be mutated by accident: configuration objects, cached values, dictionary keys, or any value that should be hashable and safe to pass around.

Installation

Requirements

  • Python 3.10+

Install from PyPI

pip install ghga-arcticfreeze

To use the FrozenDict type within Pydantic models, install the optional pydantic extra:

pip install "ghga-arcticfreeze[pydantic]"

Usage

Deep freezing

from arcticfreeze import FrozenDict, freeze

original = {"a": [1, 2, {"b": {"c", "d"}}]}
frozen = freeze(original)

# The nested list became a tuple, the nested dicts became FrozenDicts,
# and the nested set became a frozenset:
assert frozen == FrozenDict({"a": (1, 2, FrozenDict({"b": frozenset({"c", "d"})}))})

# The result is hashable and cannot be modified:
hash(frozen)

Custom types are supported by providing additional converters:

from arcticfreeze import Converter, freeze


class Point:
    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y


point_converter = Converter(
    input_type=Point,
    convert=lambda obj, freeze_child: (freeze_child(obj.x), freeze_child(obj.y)),
)

assert freeze(Point(1, 2), add_converters=[point_converter]) == (1, 2)

FrozenDict in Pydantic models

from arcticfreeze import FrozenDict
from pydantic import BaseModel


class Config(BaseModel):
    parameters: FrozenDict[str, int]


config = Config(parameters={"a": 1})

# The mapping is validated and converted into a FrozenDict:
assert isinstance(config.parameters, FrozenDict)

# It serializes to a plain dict in JSON mode and stays a FrozenDict in Python mode:
assert config.model_dump(mode="json") == {"parameters": {"a": 1}}
assert isinstance(config.model_dump()["parameters"], FrozenDict)

Development

This package is a member of the GHGA monorepo and is developed from the repository root rather than on its own. The repository ships a devcontainer with the whole toolchain: open it in VS Code and run Remote-Containers: Reopen in Container, or set the environment up directly with just sync.

The usual tasks, run from the repository root (see ADR-0015 for the full recipe list):

just sync                         # install every member plus the shared dev toolchain
just test libs/ghga-arcticfreeze  # this member's test suite
just lint                         # ruff check + format check across the workspace

License

This repository is free to use and modify according to the Apache 2.0 License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ghga_arcticfreeze-1.1.1.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ghga_arcticfreeze-1.1.1-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file ghga_arcticfreeze-1.1.1.tar.gz.

File metadata

  • Download URL: ghga_arcticfreeze-1.1.1.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.6 {"installer":{"name":"uv","version":"0.12.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for ghga_arcticfreeze-1.1.1.tar.gz
Algorithm Hash digest
SHA256 6a9737c5335d854c36d369a9469a37ecfae5765d4f9adc83ff8aa3923c27674e
MD5 bde64a05004fc8964307e01cb7d58279
BLAKE2b-256 a3133e4c001cbbfd54e6de9ef1f0da665bbae4794611f5aa373c9fa2f4582622

See more details on using hashes here.

File details

Details for the file ghga_arcticfreeze-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: ghga_arcticfreeze-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.6 {"installer":{"name":"uv","version":"0.12.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for ghga_arcticfreeze-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a1cb2c0b9c1af84461adc84cc97cb7ce683c86b4522d064055eec532ab1dd67e
MD5 33c6e7a1b8a78956184c545d9b57e2ee
BLAKE2b-256 12439da6f9ba0529eb74000a7b9c340dcd0ee94ee9e1463ea1f7c52bdf5f1836

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page