V1: Enabling Experimental Features - rnag/dataclass-wizard GitHub Wiki
[!TIP]
Early access to V1 is available starting fromv0.33.0! This enables performance optimizations and introduces experimental changes that will be part of the upcoming majorv1release. 🚀
To opt-in to experimental v1 features, users can pass v1 = True in the Meta setting.
Example: Opting In To v1 Features
from dataclasses import dataclass
from dataclass_wizard import JSONPyWizard
from dataclass_wizard.v1 import Alias
@dataclass
class A(JSONPyWizard):
class _(JSONPyWizard.Meta):
v1 = True
my_str: str
version_info: float = Alias(load='v-info')
a = A.from_dict({'my_str': 'test', 'v-info': '1.0'})
print(a) #> A(my_str='test', version_info=1.0)
assert a.version_info == 1.0
assert a.to_dict() == {'my_str': 'test', 'version_info': 1.0}
[!NOTE]
DataclassWizardis now the recommended approach forv1opt-in, as it (1) applies the@dataclassdecorator to subclasses automatically and (2) enablesv1=Truein theMetasettings.from dataclass_wizard import DataclassWizard from dataclass_wizard.v1 import Alias class A(DataclassWizard): my_str: str version_info: float = Alias(load='v-info')
Without Inheritance
For simple dataclasses that do not subclass DataclassWizard or JSONPyWizard, you can use LoadMeta (and similar) utilities:
from dataclasses import dataclass
from dataclass_wizard import DumpMeta, LoadMeta, asdict, fromdict
from dataclass_wizard.v1 import Alias
@dataclass
class A:
my_str: str
version_info: float = Alias(load='v-info')
LoadMeta(v1=True).bind_to(A)
DumpMeta(key_transform='NONE').bind_to(A)
a = fromdict(A, {'my_str': 'test', 'v-info': '1.0'})
print(a) #> A(my_str='test', version_info=1.0)
assert a.version_info == 1.0
assert asdict(a) == {'my_str': 'test', 'version_info': 1.0}
Pass case on Sub-class
This is a lesser-known (but equally valid) approach to opting in to experimental v1 features and worth documenting.
When sub-classing from JSONWizard (or a subclass thereof), pass in case (alternatively, load_case and dump_case for de/serialization respectively) which determines the letter case for JSON keys in de-serialization. Read more about Key Case Transformation.
For example, set the AUTO key casing mode:
from dataclass_wizard import DataclassWizard
from dataclass_wizard.class_helper import get_meta
from dataclass_wizard.v1 import Alias
class A(DataclassWizard, case='AUTO'):
my_str: str
version_info: float = Alias(load='v-info')
# Confirm `v1` is automatically enabled
assert get_meta(A).v1 # > True
a = A.from_dict({'My-Str': 'test', 'v-info': '1.0'})
print(a) #> A(my_str='test', version_info=1.0)
assert a.version_info == 1.0
assert a.to_dict() == {'my_str': 'test', 'version_info': 1.0}
Discovering New Settings
All new settings related to v1 opt-in are prefixed with v1_ for convenience and discoverability. For example:
v1_unsafe_parse_dataclass_in_union = False
Note: The
v1_unsafe_parse_dataclass_in_unionsetting is documented separately. Refer to the Enhanced Union or|Type Parsing page for details.
Important Notes on Experimental Features
[!WARNING]
Experimental Nature: Thev1features are experimental and may change in future releases. Exercise caution when using these features in production environments.Backward Compatibility: Enabling
v1introduces new features and optimizations that may come with breaking changes. However, the core functionality should remain the same.For more details, refer to the Dataclass Wizard Documentation.