Skip to main content

Slightly improved dataclasses

Project description

The Python built-in datacls is almost perfect, and this module just adds a little bit on top of it to smooth the rough edges a bit.


@datacls.mutable is @dataclasses.dataclass() , except:


@datacls.immutable or just @datacls is like datacls.mutable except frozen=True by default.


datacls.field() just like dataclasses.field() except default_factory is now (also) a positional parameter.

Usage examples

import datacls
from typing import Dict

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

#
# Three new instance methods
#
o = One()
assert o.asdict() == {'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})

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

#
# Immutable by default
#
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 dataclses:
#
@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

# In this case, you can access them as functions on `datacls`:
assert (
    datacls.asdict(ov) ==
    {'asdict': 1, 'astuple': 1, 'fields': 1, 'one': 'one', 'replace': 1}
)

assert datacls.astuple(ov) == ('one', 1, 1, 1, 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-2.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

datacls-2.0.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: datacls-2.0.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.0

File hashes

Hashes for datacls-2.0.0.tar.gz
Algorithm Hash digest
SHA256 8cadcc654830e1f337ee4b94ea2ae501441756cf018a43efdaad2372e442c63f
MD5 26b62f66e0d75cd4d2a83e5777b4224a
BLAKE2b-256 c66810772cb611c12f8fced85990b6f349f6d5f53dd9f1f74a22afcced5a9e9b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: datacls-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.0

File hashes

Hashes for datacls-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6ee8f95e8bcc6fb9a12bbf70d9b01df48bc616ddcca90aafbc2f7a3048314ac5
MD5 cf38ce5eaa95c6d7d5a38cbd0aec375a
BLAKE2b-256 602ac9c95c2e6e8d0aa73434db4fa6b838aa7cb70eb4c260622c4e9c737e4d60

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