-
-
Notifications
You must be signed in to change notification settings - Fork 22
V1: Enabling Experimental Features
Tip
Early access to V1 is available starting from v0.33.0
! This enables performance optimizations and introduces experimental changes that will be part of the upcoming major v1
release. 🚀
To opt-in to experimental v1
features, users can pass v1 = True
in the Meta
setting.
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}
For simple dataclasses that do not subclass 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}
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 key_case
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 dataclasses import dataclass
from dataclass_wizard import JSONPyWizard
from dataclass_wizard.class_helper import get_meta
from dataclass_wizard.v1 import Alias
@dataclass
class A(JSONPyWizard, key_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}
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_union
setting is documented separately. Refer to the Enhanced Union or|
Type Parsing page for details.
Warning
Experimental Nature: The v1
features are experimental and may change in future releases. Exercise caution when using these features in production environments.
Backward Compatibility: Enabling v1
introduces 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.