Skip to main content

🗂 datacls: take the edge off dataclass 🗂

dataclasses is almost perfect.

datacls is a tiny, thin wrapper around dataclass.dataclasses making it a bit more self-contained, reflective, and saving a bit of typing.

datacls is exactly like dataclass, except:

  • Adds three new instance methods: asdict(), astuple(), replace(), and one new class method, fields(), all taken from the dataclasses module

  • xmod-ed for less cruft (so datacls is the same as datacls.dataclass)

  • The default class is datacls.immutable where frozen=True.

Example

import datacls

@datacls
class One:
    one: str = 'one'
    two: int = 2
    three: dict = datacls.field(dict)

# `One` has three instance methods: asdict(), astuple(), replace()

o = One()
assert o.asdict() == {'one': 'one', 'two': 2, 'three': {}}

import dataclasses
assert dataclasses.asdict(o) == o.asdict()

assert o.astuple() == ('one', 2, {})

o2 = o.replace(one='seven', three={'nine': 9})
assert o2 == One('seven', 2, {'nine': 9})

# `One` has one new class method: fields()

assert [f.name for f in One.fields()] == ['one', 'two', 'three']

# @datacls is immutable.

try:
    o.one = 'three'
except AttributeError:
    pass
else:
    raise AttributeError('Was mutable!')

# Usec @datacls.mutable or @datacls(frozen=False)
# for mutable classes

@datacls.mutable
class OneMutable:
    one: str = 'one'
    two: int = 2
    three: Dict = datacls.field(dict)

om = OneMutable()
om.one = 'three'
assert str(om) == "OneMutable(one='three', two=2, three={})"

# These four new methods won't break your old dataclass by mistake:
@datacls
class Overloads:
    one: str = 'one'
    asdict: int = 1
    astuple: int = 1
    fields: int = 1
    replace: int = 1

o = Overloads()

assert ov.one == 'one'
assert ov.asdict == 1
assert ov.astuple == 1
assert ov.fields == 1
assert ov.replace == 1

# You can still access the methods as functions on `datacls`:
assert (
    datacls.asdict(ov) ==
    {'asdict': 1, 'astuple': 1, 'fields': 1, 'one': 'one', 'replace': 1}
)

API Documentation

Metadata

Release files for datacls 4.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datacls 4.10.0
File Size Uploaded
datacls-4.10.0.tar.gz 17.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for datacls 4.10.0
File Interpreter ABI Platform
datacls-4.10.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.9 kB

Release files / datacls-4.10.0.tar.gz

Download URL datacls-4.10.0.tar.gz
Size 17.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f9a574d1e7ec03a1c55ef9dadb3e333f7525c9e7758233d3f5ac3cad3cbe8f20
BLAKE2b-256 checksum
How to use checksums
eaa5a54fe182239f8ce9ad1e17aa4c947828c95ccef0e1c1982a73d66fbb3e8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / datacls-4.10.0-py3-none-any.whl

Download URL datacls-4.10.0-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b5ea17f90bedd6d3bbb8b4184b917ed29990667b11dc105a0886bdd9b2823411
BLAKE2b-256 checksum
How to use checksums
a76b830505ae2b9033f3a672774836abeda88c813202d395739d97288c5389e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

4.10.0 This release

2 release files

4.9.0

2 release files

4.8.0

2 release files

4.7.1

2 release files

4.7.0

2 release files

4.6.0

2 release files

4.5.0

2 release files

4.4.2

2 release files

4.4.1

2 release files

4.3.0

2 release files

4.2.1

2 release files

4.2.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.1.0

2 release files

1.0.0

2 release 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