0.1.1 - This version may not be safe as it has not been updated for a long time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform
Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.
Apache-2.0 - Apache License 2.0Simple dataclasses alternative (beta)
baseclasses is available on PyPI:
pip install baseclassesbaseclasses 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:
__pre_init__ ability to mutate kwargsdefault_factory funcInternalStateField
str and repr
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):
passLike dataclasses, but:
kwargs in all but the simplest casesmetaclass and simple inheritance over monkey-patched generated codefrozen status per child classdefault_factory(lambda **kwargs: ...) to access init fields__pre_init__
obj.get_fields() instead of dataclasses.get_fields(obj))Like dataclassy but:
metaclass and simple inheritance over monkey-patched generated codeThere 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.
init property to field (to mirror dataclasses)obj.replace(**kwargs) (to mirror dataclasses)serialize property to field