Skip to main content

Slightly improved, backward compatible dataclasses

Project description

The Python built-in module dataclasses is almost perfect.

datacls is a thin wrapper around dataclasses, completely backward-compatible, that makes common use cases a little easier.


@datacls.mutable is exactly like @dataclasses.dataclass() except that the resulting dataclass has four new methods:

The new methods are only added if they do not exist on the target dataclass, so it should be impossible for datacls to override or shadow user methods or members by mistake.


@datacls.immutable, or just @datacls, is exactly like datacls.mutable except frozen=True by default, so members can’t be changed after construction (without deliberately subverting the immutability).


datacls.field() is just like dataclasses.field() except the very common default_factory argument is now also the first and only positional parameter.

datacls.make_dataclass() is just like dataclasses.make_dataclass() except that the class created also has the four new methods listed above.

datacls.cached_property is exactly like functools.cached_property from Python 3.8, except with a backport in Python 3.7.

datacls.dtyper is the dtyper.dataclass decorator, lazily loaded: https://github.com/rec/dtyper

Usage examples

import datacls
from typing import Dict

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

#
# Three new instance methods: asdict(), astuple(), replace()
#
o = One()
assert o.asdict() == {'one': 'one', 'two': 2, 'three': {}}

# Same as:
#
# import dataclasses
#
# assert dataclasses.asdict(o) == {'one': 'one', 'two': 2, 'three': {}}

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

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

#
# One new class method: fields()
#
assert [f.name for f in One.fields()] == ['one', 'two', 'three']

#
# @datacls is immutable: use @datacls.mutable for mutable classes
#
try:
    o.one = 'three'
except AttributeError:
    pass
else:
    raise AttributeError('Was mutable!')

@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 datacls.astuple(ov) == ('one', 1, 1, 1, 1)

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}
)

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

datacls-4.5.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

datacls-4.5.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file datacls-4.5.0.tar.gz.

File metadata

  • Download URL: datacls-4.5.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Darwin/21.6.0

File hashes

Hashes for datacls-4.5.0.tar.gz
Algorithm Hash digest
SHA256 d8f4694526a0938e643bf6a62b1d03eac3873df52f001d50982ce3682f38e474
MD5 57076a130e3b459015cf9250506227be
BLAKE2b-256 6a5630f7d609eeb9260d41bdaa28bc032646818645aec290d19237eff1b1016e

See more details on using hashes here.

File details

Details for the file datacls-4.5.0-py3-none-any.whl.

File metadata

  • Download URL: datacls-4.5.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Darwin/21.6.0

File hashes

Hashes for datacls-4.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e0762210fb532fe2a5cac6d08a046b686609ff9de86f5572f5cf44549a1a84f
MD5 978988af5629b5a22451256a2c4e8c77
BLAKE2b-256 608a39319f480f754571becd1206c1fb8e65dc3e77108122ca65d0cf70d77a3f

See more details on using hashes here.

Supported by

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