Skip to main content

Drop-in replacement for Python's built-in `dataclasses` module, allowing for certain fields to be mutable while others remain immutable.

Project description

Semimutable

Tests codecov

A dataclass drop-in that allows you to define individual fields as immutable.

Usage

Simply replace all your dataclasses imports with semimutable, and use field(frozen=True) for the fields you want to be immutable.

field takes in the same parameters as dataclasses.field, with an extra frozen boolean flag that defaults to False. You can specify default values, default factories, and other options just like you would with a regular dataclass field.

Here is one example from our tests.

from semimutable import dataclass, field

@dataclass
class Simple:
    x: int = field(frozen=True)
    y: int = 0 # normal, mutable field

def test_frozen_field_is_immutable():
    obj = Simple(x=1, y=2)
    with pytest.raises(TypeError):
        obj.x = 99

def test_non_frozen_field_is_mutable():
    obj = Simple(x=1, y=2)
    obj.y = 42
    assert obj.y == 42

Credits

Parts of this library are derived from Python's standard library dataclasses module. The original implementation is distributed under the Python Software Foundation License. See LICENSE.PSF for the full license text.

Project details


Download files

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

Source Distribution

semimutable-0.1.0.tar.gz (43.9 kB view details)

Uploaded Source

Built Distribution

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

semimutable-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file semimutable-0.1.0.tar.gz.

File metadata

  • Download URL: semimutable-0.1.0.tar.gz
  • Upload date:
  • Size: 43.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.12

File hashes

Hashes for semimutable-0.1.0.tar.gz
Algorithm Hash digest
SHA256 37e35ac442d103c367c87e1e219ef1afcc366729319a990324363ddb718cf31c
MD5 164a720bf828160b9ba2a1915f2a8a3e
BLAKE2b-256 ab007ce6af378095896fa39feeeeb22a3f5c4e5ed11051000f3803541c4a6945

See more details on using hashes here.

File details

Details for the file semimutable-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for semimutable-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 38b253ca963c74d2c1b2d5ebe2cce10a87ff621d22d842eb4b5444a418bb4c43
MD5 61d51d226595fde1a632dc6e2c986a86
BLAKE2b-256 0245abc6f3606444f362d43a37a8c6cd8ac6af9b810b96507ed3f473e3d84359

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page