|
45 | 45 | MyClass(my_str='20', is_active_tuple=(True, False, True), list_of_int=[1, 2, 3]) |
46 | 46 |
|
47 | 47 | .. note:: |
48 | | - The example above demonstrates automatic type coercion, key casing transforms, |
49 | | - and support for nested dataclass structures. |
50 | | - ``DataclassWizard`` also auto-applies ``@dataclass`` to subclasses. |
51 | | - See the docs for more examples and advanced usage. |
| 48 | + The example above demonstrates automatic type coercion, key casing |
| 49 | + transforms, and support for nested dataclasses. ``DataclassWizard`` |
| 50 | + also auto-applies ``@dataclass`` to subclasses. |
| 51 | + |
| 52 | +.. important:: |
| 53 | + |
| 54 | + A new **v1 engine** is available as an opt-in, offering explicit |
| 55 | + environment precedence, nested dataclass support, and improved performance. |
| 56 | + See the `Field Guide to V1 Opt-in`_ for details. |
52 | 57 |
|
53 | 58 | .. contents:: Contents |
54 | 59 | :depth: 1 |
55 | 60 | :local: |
56 | 61 | :backlinks: none |
57 | 62 |
|
58 | | -``v1`` Opt-In 🚀 |
59 | | ----------------- |
| 63 | +Why Dataclass Wizard? |
| 64 | +--------------------- |
| 65 | + |
| 66 | +Dataclass Wizard helps you turn Python dataclasses into robust, |
| 67 | +high-performance serialization schemas with minimal effort. |
| 68 | + |
| 69 | +- 🚀 Fast — code-generated loaders and dumpers |
| 70 | +- 🪶 Lightweight — pure Python, minimal dependencies |
| 71 | +- 🧠 Typed — powered by Python type hints |
| 72 | +- 🧙 Flexible — JSON, YAML, TOML, and environment variables |
| 73 | +- 🧪 Reliable — battle-tested with extensive test coverage |
| 74 | + |
| 75 | +Quick Examples |
| 76 | +-------------- |
60 | 77 |
|
61 | | -Early access to **V1** is available! To opt in, simply enable ``v1=True`` in the ``Meta`` settings: |
| 78 | +.. rubric:: JSON De/Serialization |
62 | 79 |
|
63 | 80 | .. code-block:: python3 |
64 | 81 |
|
65 | 82 | from dataclasses import dataclass |
66 | | - from dataclass_wizard import JSONPyWizard |
67 | | - from dataclass_wizard.v1 import Alias |
| 83 | + from dataclass_wizard import JSONWizard |
68 | 84 |
|
69 | 85 | @dataclass |
70 | | - class A(JSONPyWizard): |
71 | | - class _(JSONPyWizard.Meta): |
72 | | - v1 = True |
| 86 | + class Data(JSONWizard): |
| 87 | + value: int |
73 | 88 |
|
74 | | - my_str: str |
75 | | - version_info: float = Alias(load='v-info') |
| 89 | + data = Data.from_dict({"value": "123"}) |
| 90 | + assert data.value == 123 |
| 91 | + print(data.to_json()) |
76 | 92 |
|
77 | | - # Alternatively, for simple dataclasses that don't subclass `JSONPyWizard`: |
78 | | - # LoadMeta(v1=True).bind_to(A) |
| 93 | +.. rubric:: Environment Variables |
79 | 94 |
|
80 | | - a = A.from_dict({'my_str': 'test', 'v-info': '1.0'}) |
81 | | - assert a.version_info == 1.0 |
82 | | - assert a.to_dict() == {'my_str': 'test', 'version_info': 1.0} |
| 95 | +.. code-block:: python3 |
83 | 96 |
|
84 | | -For more information, see the `Field Guide to V1 Opt-in`_. |
| 97 | + import os |
| 98 | + from dataclass_wizard import EnvWizard |
85 | 99 |
|
86 | | -Performance Improvements |
87 | | -~~~~~~~~~~~~~~~~~~~~~~~~ |
| 100 | + os.environ['DEBUG'] = 'true' |
88 | 101 |
|
89 | | -The upcoming **V1** release brings significant performance improvements in de/serialization. Personal benchmarks show that **V1** can make Dataclass Wizard |
90 | | -approximately **2x faster** than ``pydantic``! |
| 102 | + class Config(EnvWizard): |
| 103 | + debug: bool |
91 | 104 |
|
92 | | -While some features are still being refined and fully supported, **v1** positions Dataclass Wizard alongside other high-performance serialization libraries in Python. |
| 105 | + cfg = Config() |
| 106 | + assert cfg.debug is True |
93 | 107 |
|
94 | | -Why Use Dataclass Wizard? |
95 | | -------------------------- |
| 108 | +.. rubric:: EnvWizard v1 (Opt-in) |
96 | 109 |
|
97 | | -Effortlessly handle complex data with one of the *fastest* and *lightweight* libraries available! Perfect for APIs, JSON wrangling, and more. |
| 110 | +.. code-block:: python3 |
98 | 111 |
|
99 | | -- 🚀 **Blazing Fast** — One of the fastest libraries out there! |
100 | | -- 🪶 **Lightweight** — Pure Python, minimal dependencies |
101 | | -- 👶 Easy Setup — Intuitive, hassle-free |
102 | | -- ☝️ **Battle-Tested** — Proven reliability with solid test coverage |
103 | | -- ⚙️ Highly Customizable — Endless de/serialization options to fit your needs |
104 | | -- 🎉 Built-in Support — JSON, YAML, TOML, and environment/settings management |
105 | | -- 📦 **Full Python Type Support** — Powered by type hints with full support for native types and ``typing-extensions`` |
106 | | -- 📝 Auto-Generate Schemas — JSON to Dataclass made easy |
| 112 | + import os |
| 113 | + from dataclass_wizard.v1 import EnvWizard, Env |
| 114 | +
|
| 115 | + os.environ['DEBUG_MODE'] = 'true' |
107 | 116 |
|
108 | | -Key Features |
109 | | ------------- |
110 | 117 |
|
111 | | -- 🔄 Flexible (de)serialization — Marshal dataclasses to/from JSON, TOML, YAML, or ``dict`` with ease. |
112 | | -- 🌿 Environment Magic — Map env vars and ``.env`` files to strongly-typed class fields effortlessly. |
113 | | -- 🧑💻 Field Properties Made Simple — Add properties with default values to your dataclasses. |
114 | | -- 🧙♂️ JSON-to-Dataclass Wizardry — Auto-generate a dataclass schema from any JSON file or string instantly. |
| 118 | + class Config(EnvWizard): |
| 119 | + debug: bool = Env('DEBUG_MODE') |
| 120 | +
|
| 121 | +
|
| 122 | + cfg = Config() |
| 123 | + assert cfg.debug is True |
| 124 | +
|
| 125 | +.. tip:: |
| 126 | + EnvWizard v1 introduces explicit environment precedence and nested |
| 127 | + dataclass support. See `Field Guide to V1 Opt-in`_ for full details. |
115 | 128 |
|
116 | 129 | Installation |
117 | 130 | ------------ |
118 | 131 |
|
119 | | -*Dataclass Wizard* is available on `PyPI`_. You can install it with ``pip``: |
| 132 | +Install from `PyPI`_: |
120 | 133 |
|
121 | 134 | .. code-block:: console |
122 | 135 |
|
123 | | - $ pip install dataclass-wizard |
| 136 | + pip install dataclass-wizard |
124 | 137 |
|
125 | | -Also available on `conda`_ via `conda-forge`_. To install via ``conda``: |
| 138 | +Or via `conda-forge`_: |
126 | 139 |
|
127 | 140 | .. code-block:: console |
128 | 141 |
|
129 | | - $ conda install dataclass-wizard -c conda-forge |
| 142 | + conda install -c conda-forge dataclass-wizard |
130 | 143 |
|
131 | | -This library supports **Python 3.9+**. Support for Python 3.6 – 3.8 was |
132 | | -available in earlier releases but is no longer maintained, as those |
133 | | -versions no longer receive security updates. |
| 144 | +Dataclass Wizard supports **Python 3.9+**. |
134 | 145 |
|
135 | | -For convenience, the table below outlines the last compatible release |
136 | | -of *Dataclass Wizard* for unsupported Python versions (3.6 – 3.8): |
| 146 | +.. note:: |
| 147 | + Python 3.6–3.8 users should install the last supported release, |
| 148 | + ``dataclass-wizard==0.26.1``. |
| 149 | + See `PyPI`_ for historical versions and the `Changelog`_ for details. |
137 | 150 |
|
138 | | -.. list-table:: |
139 | | - :header-rows: 1 |
140 | | - :widths: 15 35 15 |
141 | | - |
142 | | - * - Python Version |
143 | | - - Last Version of ``dataclass-wizard`` |
144 | | - - Python EOL |
145 | | - * - 3.8 |
146 | | - - 0.26.1_ |
147 | | - - 2024-10-07 |
148 | | - * - 3.7 |
149 | | - - 0.26.1_ |
150 | | - - 2023-06-27 |
151 | | - * - 3.6 |
152 | | - - 0.26.1_ |
153 | | - - 2021-12-23 |
154 | | - |
155 | | -.. _0.26.1: https://pypi.org/project/dataclass-wizard/0.26.1/ |
156 | 151 | .. _PyPI: https://pypi.org/project/dataclass-wizard/ |
157 | | -.. _conda: https://anaconda.org/conda-forge/dataclass-wizard |
158 | 152 | .. _conda-forge: https://conda-forge.org/ |
159 | 153 | .. _Changelog: https://dcw.ritviknag.com/en/latest/history.html |
160 | 154 |
|
161 | | -See the package on `PyPI`_ and the `Changelog`_ in the docs for the latest version details. |
162 | | - |
163 | 155 | Wizard Mixins ✨ |
164 | 156 | ---------------- |
165 | 157 |
|
166 | | -In addition to ``JSONWizard``, these `Mixin`_ classes simplify common tasks and make your data handling *spellbindingly* efficient: |
| 158 | +In addition to ``JSONWizard``, Dataclass Wizard provides a set of focused |
| 159 | +Mixin_ classes to simplify common serialization tasks: |
| 160 | + |
| 161 | +- 🪄 `EnvWizard`_ — Load environment variables and ``.env`` files into typed schemas, |
| 162 | + including support for secret directories. |
| 163 | +- 🎩 `JSONPyWizard`_ — A helper for ``JSONWizard`` that preserves keys as-is |
| 164 | + (no case transformations). |
| 165 | +- 🔮 `JSONListWizard`_ — Extend ``JSONWizard`` to convert lists into container objects. |
| 166 | +- 💼 `JSONFileWizard`_ — Load and save dataclass instances from local JSON files. |
167 | 167 |
|
168 | | -- 🪄 `EnvWizard`_ — Load environment variables and `.env` files into typed schemas, even supporting secret files (keys as file names). |
169 | | -- 🎩 `JSONPyWizard`_ — A helper for ``JSONWizard`` that preserves your keys as-is (no camelCase changes). |
170 | | -- 🔮 `JSONListWizard`_ — Extend ``JSONWizard`` to convert lists into `Container`_ objects. |
171 | | -- 💼 `JSONFileWizard`_ — Convert dataclass instances to/from local JSON files with ease. |
172 | | -- 🌳 `TOMLWizard`_ — Map your dataclasses to/from TOML format. |
173 | | -- 🧙♂️ `YAMLWizard`_ — Convert between YAML and dataclass instances using ``PyYAML``. |
| 168 | +Optional format support: |
| 169 | + |
| 170 | +- 🌳 `TOMLWizard`_ — Map dataclasses to and from TOML. |
| 171 | +- 🧙♂️ `YAMLWizard`_ — Convert between YAML and dataclasses using ``PyYAML``. |
174 | 172 |
|
175 | 173 | Supported Types 🧑💻 |
176 | 174 | --------------------- |
@@ -202,149 +200,6 @@ with the v1 code generation engine. |
202 | 200 |
|
203 | 201 | See `Type Hooks`_ for details and examples. |
204 | 202 |
|
205 | | -Usage and Examples |
206 | | ------------------- |
207 | | - |
208 | | -.. rubric:: Seamless JSON De/Serialization with ``JSONWizard`` |
209 | | - |
210 | | -.. code-block:: python3 |
211 | | -
|
212 | | - from __future__ import annotations # Optional in Python 3.10+ |
213 | | -
|
214 | | - from dataclasses import dataclass, field |
215 | | - from enum import Enum |
216 | | - from datetime import date |
217 | | -
|
218 | | - from dataclass_wizard import JSONWizard |
219 | | -
|
220 | | -
|
221 | | - @dataclass |
222 | | - class Data(JSONWizard): |
223 | | - # Use Meta to customize JSON de/serialization |
224 | | - class _(JSONWizard.Meta): |
225 | | - key_transform_with_dump = 'LISP' # Transform keys to LISP-case during dump |
226 | | -
|
227 | | - a_sample_bool: bool |
228 | | - values: list[Inner] = field(default_factory=list) |
229 | | -
|
230 | | -
|
231 | | - @dataclass |
232 | | - class Inner: |
233 | | - # Nested data with optional enums and typed dictionaries |
234 | | - vehicle: Car | None |
235 | | - my_dates: dict[int, date] |
236 | | -
|
237 | | -
|
238 | | - class Car(Enum): |
239 | | - SEDAN = 'BMW Coupe' |
240 | | - SUV = 'Toyota 4Runner' |
241 | | -
|
242 | | -
|
243 | | - # Input JSON-like dictionary |
244 | | - my_dict = { |
245 | | - 'values': [{'vehicle': 'Toyota 4Runner', 'My-Dates': {'123': '2023-01-31'}}], |
246 | | - 'aSampleBool': 'TRUE' |
247 | | - } |
248 | | -
|
249 | | - # Deserialize into strongly-typed dataclass instances |
250 | | - data = Data.from_dict(my_dict) |
251 | | - print((v := data.values[0]).vehicle) # Prints: <Car.SUV: 'Toyota 4Runner'> |
252 | | - assert v.my_dates[123] == date(2023, 1, 31) # > True |
253 | | -
|
254 | | - # Serialize back into pretty-printed JSON |
255 | | - print(data.to_json(indent=2)) |
256 | | -
|
257 | | -.. rubric:: Map Environment Variables with ``EnvWizard`` |
258 | | - |
259 | | -Easily map environment variables to Python dataclasses: |
260 | | - |
261 | | -.. code-block:: python3 |
262 | | -
|
263 | | - import os |
264 | | - from dataclass_wizard import EnvWizard |
265 | | -
|
266 | | - os.environ.update({ |
267 | | - 'APP_NAME': 'My App', |
268 | | - 'MAX_CONNECTIONS': '10', |
269 | | - 'DEBUG_MODE': 'true' |
270 | | - }) |
271 | | -
|
272 | | - class AppConfig(EnvWizard): |
273 | | - app_name: str |
274 | | - max_connections: int |
275 | | - debug_mode: bool |
276 | | -
|
277 | | - config = AppConfig() |
278 | | - print(config.app_name) # My App |
279 | | - print(config.debug_mode) # True |
280 | | -
|
281 | | -📖 See more `on EnvWizard`_ in the full documentation. |
282 | | - |
283 | | -.. rubric:: Dataclass Properties with ``property_wizard`` |
284 | | - |
285 | | -Add field properties to your dataclasses with default values using ``property_wizard``: |
286 | | - |
287 | | -.. code-block:: python3 |
288 | | -
|
289 | | - from __future__ import annotations # This can be removed in Python 3.10+ |
290 | | -
|
291 | | - from dataclasses import dataclass, field |
292 | | - from typing_extensions import Annotated |
293 | | -
|
294 | | - from dataclass_wizard import property_wizard |
295 | | -
|
296 | | -
|
297 | | - @dataclass |
298 | | - class Vehicle(metaclass=property_wizard): |
299 | | - wheels: Annotated[int | str, field(default=4)] |
300 | | - # or, alternatively: |
301 | | - # _wheels: int | str = 4 |
302 | | -
|
303 | | - @property |
304 | | - def wheels(self) -> int: |
305 | | - return self._wheels |
306 | | -
|
307 | | - @wheels.setter |
308 | | - def wheels(self, value: int | str): |
309 | | - self._wheels = int(value) |
310 | | -
|
311 | | -
|
312 | | - v = Vehicle() |
313 | | - print(v.wheels) # 4 |
314 | | - v.wheels = '6' |
315 | | - print(v.wheels) # 6 |
316 | | -
|
317 | | - assert v.wheels == 6, 'Setter correctly handles type conversion' |
318 | | -
|
319 | | -📖 For a deeper dive, visit the documentation on `field properties`_. |
320 | | - |
321 | | -.. rubric:: Generate Dataclass Schemas with CLI |
322 | | - |
323 | | -Quickly generate Python dataclasses from JSON input using the ``wiz-cli`` tool: |
324 | | - |
325 | | -.. code-block:: console |
326 | | -
|
327 | | - $ echo '{"myFloat": "1.23", "Items": [{"created": "2021-01-01"}]}' | wiz gs - output.py |
328 | | -
|
329 | | -.. code-block:: python3 |
330 | | -
|
331 | | - from dataclasses import dataclass |
332 | | - from datetime import date |
333 | | - from typing import List, Union |
334 | | -
|
335 | | - from dataclass_wizard import JSONWizard |
336 | | -
|
337 | | - @dataclass |
338 | | - class Data(JSONWizard): |
339 | | - my_float: Union[float, str] |
340 | | - items: List['Item'] |
341 | | -
|
342 | | - @dataclass |
343 | | - class Item: |
344 | | - created: date |
345 | | -
|
346 | | -📖 Check out the full CLI documentation at wiz-cli_. |
347 | | - |
348 | 203 | JSON Marshalling |
349 | 204 | ---------------- |
350 | 205 |
|
@@ -1647,3 +1502,4 @@ This package was created with Cookiecutter_ and the `rnag/cookiecutter-pypackage |
1647 | 1502 | .. _V1 Alias: https://dcw.ritviknag.com/en/latest/common_use_cases/v1_alias.html |
1648 | 1503 | .. _Type Hooks: https://dcw.ritviknag.com/en/latest/advanced_usage/type_hooks.html |
1649 | 1504 | .. _`ipaddress.IPv4Address`: https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Address |
| 1505 | +.. _V1 Env Magic: https://dcw.ritviknag.com/en/latest/env_magic_v1.html |
0 commit comments