Registry / serialization / dataclasses

dataclasses

JSON →
library0.8pypypi✓ verified 31d ago

The `dataclasses` library is a backport of the standard library `dataclasses` module, designed specifically for Python 3.6. It provides the `@dataclass` decorator and related utilities, enabling simpler creation of data-holding classes without boilerplate. Its current version is 0.8. As it targets a specific, older Python version, its release cadence is stable and infrequent, with no new feature development anticipated.

pip install dataclasses
INSTALL
IMPORT
SIG · DATACLASSES
D
dataclasses
serializationpythonv0.8
Install
1.5s avg
Import
27ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.028s · 17.8MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.026s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

dataclass
✓ from dataclasses import dataclass
field
✓ from dataclasses import field

Define a simple data class using the `@dataclass` decorator. Note the `from __future__ import annotations` which is highly recommended for type hints in Python 3.6 to handle forward references and complex types gracefully. Mutable default values for fields should use `default_factory`.

from __future__ import annotations # Recommended for Python 3.6 type hints from dataclasses import dataclass, field @dataclass class User: id: int name: str = "Guest" email: str | None = None # Using Union syntax (PEP 604) or typing.Optional tags: list[str] = field(default_factory=list) # Example usage user1 = User(id=1, name="Alice") user2 = User(id=2, email="bob@example.com") print(user1) print(user2) assert user1.name == "Alice" assert user2.tags == []
Debug
Known issues
breakingThis library is exclusively for Python 3.6. Installing it on Python 3.7 or newer is redundant and can cause subtle issues if you rely on features specific to this backport or expect it to override the standard library module. Python 3.7+ includes `dataclasses` as a built-in module.
fix
Remove `dataclasses` from your project's dependencies when upgrading to Python 3.7 or newer. The standard library module will be used automatically.
affects: Python >=3.7
gotchaWhen using type hints in Python 3.6 with `dataclasses`, `from __future__ import annotations` is highly recommended. Without it, you may encounter `NameError` for forward references or issues with complex type hints (e.g., `list[str]`, `dict[str, int]`). Alternatively, string literal type hints (e.g., `field: 'MyClass'`) can be used.
fix
Add `from __future__ import annotations` at the top of your module, or use string literal type hints for any forward references or complex generic types.
affects: Python 3.6
gotchaThis backport provides the functionality of `dataclasses` as it existed in Python 3.7. It does not include features introduced in later Python versions, such as `kw_only` or `slots=True` (Python 3.10+).
fix
If these newer features are required, consider upgrading your Python environment to 3.10 or newer. There is no workaround to enable these features in the 3.6 backport.
affects: All versions of this backport on Python 3.6
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'dataclasses'
The `dataclasses` module is built into Python from version 3.7 onwards; for Python 3.6, it needs to be installed as a backport library.
fix
Install the backport library using pip: `pip install dataclasses`
TypeError: non-default argument follows default argument
Python (and thus dataclasses) requires that all arguments with default values must appear after all arguments without default values in a class definition's `__init__` method, which dataclasses automatically generates. This is often encountered with inheritance where field order is implicitly merged.
fix
Rearrange your dataclass fields, or fields in parent classes, so that all fields without default values are defined before any fields with default values.
TypeError: mutable default <class 'list'> for field 'my_list' is not allowed
Dataclasses prevent using mutable types (like lists, dictionaries, or sets) directly as default values because all instances would share the same mutable object, leading to unexpected side effects.
fix
Use `dataclasses.field(default_factory=...)` to provide a zero-argument callable that returns a new mutable object for each instance. For example, `my_list: List[int] = field(default_factory=list)`.
AttributeError: 'MyClass' object has no attribute 'my_field'
This can occur when `field(default_factory=...)` is used in a dataclass where `init=False` is set for the dataclass itself or for the specific field, and no custom `__init__` or `__post_init__` method explicitly initializes the field. When `init=False`, the automatically generated `__init__` does not include the field, thus the `default_factory` is not called.
fix
Either ensure `init=True` (the default) for the dataclass or the field, or if `init=False` is necessary, explicitly initialize the field in a custom `__init__` or `__post_init__` method, calling the `default_factory` manually.
Upgrade
Version history
0.8latest on PyPI · released Nov 13, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
47 hits · last 30 days
node
44
Resources
dataclasses — pip install dataclasses · libregistry