{"id":1444,"library":"dataclasses","title":"dataclasses (Python 3.6 Backport)","description":"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.","status":"maintenance","version":"0.8","language":"python","source_language":"en","source_url":"https://github.com/ericvsmith/dataclasses","tags":["backport","python36","dataclasses","data-structures","type-hints"],"install":[{"cmd":"pip install dataclasses","lang":"bash","label":"Install for Python 3.6"}],"dependencies":[],"imports":[{"symbol":"dataclass","correct":"from dataclasses import dataclass"},{"symbol":"field","correct":"from dataclasses import field"}],"quickstart":{"code":"from __future__ import annotations # Recommended for Python 3.6 type hints\nfrom dataclasses import dataclass, field\n\n@dataclass\nclass User:\n    id: int\n    name: str = \"Guest\"\n    email: str | None = None  # Using Union syntax (PEP 604) or typing.Optional\n    tags: list[str] = field(default_factory=list)\n\n# Example usage\nuser1 = User(id=1, name=\"Alice\")\nuser2 = User(id=2, email=\"bob@example.com\")\n\nprint(user1)\nprint(user2)\n\nassert user1.name == \"Alice\"\nassert user2.tags == []","lang":"python","description":"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`."},"warnings":[{"fix":"Remove `dataclasses` from your project's dependencies when upgrading to Python 3.7 or newer. The standard library module will be used automatically.","message":"This 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.","severity":"breaking","affected_versions":"Python >=3.7"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":"Python 3.6"},{"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.","message":"This 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+).","severity":"gotcha","affected_versions":"All versions of this backport on Python 3.6"}],"env_vars":null,"search_vec":"'0.8':44 '3.6':3,21 'anticip':65 'backport':4,10,66 'boilerpl':39 'cadenc':55 'class':37 'creation':32 'current':41 'data':35,70 'data-hold':34 'data-structur':69 'dataclass':1,6,15,25,68 'decor':26 'design':17 'develop':64 'enabl':30 'featur':63 'hint':74 'hold':36 'infrequ':59 'librari':7,14 'modul':16 'new':62 'older':50 'provid':23 'python':2,20,51 'python36':67 'relat':28 'releas':54 'simpler':31 'specif':18,49 'stabl':57 'standard':13 'structur':71 'target':47 'type':73 'type-hint':72 'util':29 'version':42,52 'without':38","created_at":"2026-04-09T03:48:12.257578+00:00","updated_at":"2026-04-16T05:26:00.045044+00:00","problems":[{"fix":"Install the backport library using pip: `pip install dataclasses`","cause":"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.","error":"ModuleNotFoundError: No module named 'dataclasses'"},{"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.","cause":"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.","error":"TypeError: non-default argument follows default argument"},{"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)`.","cause":"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.","error":"TypeError: mutable default <class 'list'> for field 'my_list' is not allowed"},{"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.","cause":"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.","error":"AttributeError: 'MyClass' object has no attribute 'my_field'"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.8","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/ericvsmith/dataclasses","docs":null,"changelog":null,"pypi":"https://pypi.org/project/dataclasses/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}