baseclasses
Simple dataclasses alternative (beta)
Installation
baseclasses is available on PyPI:
pip install baseclasses
Quick Start
baseclasses is an alternative to dataclasses.
There are many such alternatives, and there are a lot of reasons
to use them (including the built-in
dataclasses), but if you want a library with:
- No monkey-patched methods generated from strings
- No special decorators, just regular inheritance
- Ability to order fields in subclasses as desired
- Auto-inheritance of parent properties, with ability to change per subclass
- Emphasis on keyword args over positional args
- Optional
__pre_init__ability to mutate kwargs - Optional ability to reference other kwargs in
default_factoryfunc - A shorthand helper to specify
InternalStateField - Ability to differentiate between
strandrepr
you can give baseclasses a try. E.g.:
from typing import Dict, Optional
import baseclasses
# No decorator
class Foo(baseclasses.FrozenBaseClass):
a: int
b: int
c: Optional[str] = baseclasses.Field(default="hello", hash=False)
_d: Dict = baseclasses.InternalStateField(default_factory=dict)
# Auto-inherits FrozenBaseClass properties
class ChildFoo(Foo):
# No problems with child class field ordering
x: float
# Dynamic defaults
y: int = baseclasses.Field(
default_factory=lambda **kwargs: kwargs["a"] * 2.0
)
# Override frozen state per child class
class MutableChildFoo(ChildFoo, frozen=False):
pass
Comparisons to Alternatives
Like dataclasses, but:
- No issues with adding new optional fields because you're forced to
use
kwargsin all but the simplest cases - Uses
metaclassand simple inheritance over monkey-patched generated code - Child classes automatically inherit state from parent, so no need to re-declare
- But you can also change
frozenstatus per child class - You can use
default_factory(lambda **kwargs: ...)to access init fields - Or you can mutate kwargs with an optional
__pre_init__ - More consistent with traditional OOP style
(e.g.,
obj.get_fields()instead ofdataclasses.get_fields(obj))
Like dataclassy but:
- Uses
metaclassand simple inheritance over monkey-patched generated code
There are others:
Note: there are perfectly valid reasons
for why the Python community
decided to use generated code over simple inheritance.
baseclasses is just an alternative that implements things differently.
TODO
- Add
initproperty to field (to mirrordataclasses) - Expose
obj.replace(**kwargs)(to mirrordataclasses) - Consider
serializeproperty to field - Take advantage of PEP 681 included in Python 3.11
Metadata
Release files for baseclasses 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| baseclasses-0.1.1.tar.gz | 18.1 kB | Without provenance | Details for baseclasses-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| baseclasses-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for baseclasses-0.1.1-py3-none-any.whl |
Total release size: 33.3 kB
Release files / baseclasses-0.1.1.tar.gz
| Download URL | baseclasses-0.1.1.tar.gz |
|---|---|
| Size | 18.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
851c7d32bff8733217a1c6ac4eff7696a270ad9822e6817264cce30c7ade27a1
|
|
BLAKE2b-256 checksum How to use checksums |
dbc811ed8eab6cf265bffbabaea9bb4af2c1bbb3f3f0141bdefde10bd9ba4512
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.3
|
Release files / baseclasses-0.1.1-py3-none-any.whl
| Download URL | baseclasses-0.1.1-py3-none-any.whl |
|---|---|
| Size | 15.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
567008c26e9616984888fccba2e371295fb65d46ddecf3428d7a26bc326fe951
|
|
BLAKE2b-256 checksum How to use checksums |
3abed5ff9b8715d36b4b63e4a8fdbc04c8a48f4068fbd091d7bacf86eea478ad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.3
|
Security for baseclasses 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.