Skip to main content

Less verbose dataclasses

Project description

Module dataclass_baseclass

DataClass - inheritable contagious base class.

Instead of (endless?) @dataclass decorating.

Usage

class A(DataClass):  # it's a dataclass

class B(A):  # it's a dataclass too

as opposed to:

@dataclass
class A(): ...  # it's a dataclass

class B(A): ...  # it's *not* a dataclass, needs decorating

@dataclass
class B(A): ...  # now it's a dataclass

Also:

class B(DataClass, A): ...  # all properties from A are dataclassed

as opposed to:

class A():  ...

@dataclass
class B(A): ...  # no properties from A are dataclassed

Instantiation

class C(DataClass):
    a: str
    b: str

defaults: Data = {"a": "A", "b": "B"}

c = C(defaults, a="a", b="b")

or just:

c = C(a="a", b="b")

Loaders

Tested with following dataclass loaders:

dataclasses-json

Works with DataClassJsonMixin and from_dict() (actually _decode_dataclass().

I could not get it to work with @dataclass_json decorator, probably did not try hard enough.

Documentation and examples

Documentation

Tests should give a good idea of how to use it.

Test report

Name                     Stmts   Miss  Cover
--------------------------------------------
dataclass_baseclass.py     113      0   100%
--------------------------------------------
TOTAL                      113      0   100%

Notes / FAQ

And Pydantic?

Obvious question. Pydantic is Ok if you want to enter that world and stay there. Like Microsoft or Apple or Google. Want to share between those worlds? Good luck with that. In particular, multiple inheritance:

Straight multiple inheritance

class A(BaseModel): ...

class B(BaseModel): ...

class C(A, B): ...

The official stance on this (at least what I could figure out at the time of writing) is:

It will probably work, but not quaranteed, not officially supported

Of course, it could be argued this is an anti-pattern and it is good that it is not supported. I have no strong opinion on that. But:

Protocols (or mix-ins, or whatever)

class A(BaseModel): ...

class P(Protocol): ...

class C(A, P): ...

This is a no-go.

With DataClass intention is to enable all of that.

Why not from scratch, why wrapping dataclasses?

Considering the effort that was put into dataclasses my conclusu=ion is that dataclasses is the recommended way to standardise directly accessible class/instance properties in the standard library.

Has this been tested in real life?

I am using it in my personal (hobby?) projects. But nothing of a decent size in business environment.

What there anything especially annoying?

Not that anyone wanted to ask this question. But I'll say it anyway.

Metaclasses. A quote from the official docs:

The potential uses for metaclasses are boundless. Some ideas that have been explored include enum, logging, interface checking, automatic delegation, automatic property creation, proxies, frameworks, and automatic resource locking/synchronization.

One could be easily forgiven to think that creating custom metaclasses is a valid thing to do, at least not discouraged. Is there any official example of how to c roll out your own metaclass, how do you subclass type? I could not find it. type (meta)class is implementyed in C, and it is not light reading. Quite frustrating if you are after "what methods are available for overriding and what are their footprints".

One is condemned to trawling the internets, which gives you the venerable "Let's make a singleton" example in 99.98% of the cases. That gives you a clue that you ned to override __new__(), which hase the same footprint as type(). Then you look in some corners of the internet, or much better ask ChatGPT, which will give you a hint that you could play with__call__() method.

To sum it up, a very laborious trial and error process. Why not documenting some examples and make life a tad easier - no idea.

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

dataclass_baseclass-0.0.1.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

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

dataclass_baseclass-0.0.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file dataclass_baseclass-0.0.1.tar.gz.

File metadata

  • Download URL: dataclass_baseclass-0.0.1.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for dataclass_baseclass-0.0.1.tar.gz
Algorithm Hash digest
SHA256 830fb35ba068930c1c5304281cae2471ee1d2fbcac1751dc9df0383d01b2f33c
MD5 869dfdc7048e74c5959cfb6bd7d8a1bd
BLAKE2b-256 5da3ba5d25bdf4452f2ba81e04f86fd86559be53f9aa525ab4cf282269ed429a

See more details on using hashes here.

File details

Details for the file dataclass_baseclass-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for dataclass_baseclass-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 403949032a523bb733ce6f1dc1710ecd9c868692c6d9582e7c91dcfa1d21c754
MD5 2f98c9b4c2a7532b5b9ecbf54c4ff1f8
BLAKE2b-256 c92b70c94cd3a16621fa1cc792052c71216a165776119c2e2d992937935a14ca

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