py2dataclasses
This project is a PEP-557 compatible dataclass implementation for Python 2.7.
Usage
pip install py2dataclasses-
# We have to use `field` syntax because Python 2 doesn't support type annotations. from dataclasses import dataclass, field @dataclass class Point(object): x = field(int) y = field(int) p = Point(3, 4) print(p) # Point(x=3, y=4) print(p.x) # 3 print(p == Point(3, 4)) # True
Big fat (working!) example
from __future__ import print_function
from dataclasses import (dataclass, field, fields, asdict, astuple,
replace, make_dataclass, is_dataclass, InitVar,)
from typing import ClassVar
@dataclass
class Address(object):
street = field(str)
city = field(str)
zipcode = field(str, default="00000")
@dataclass
class Person(object):
company = field(ClassVar[str], "Acme Corp")
name = field(str)
age = field(int)
address = field(Address)
salary_multiplier = field(InitVar[float], default=1.0)
salary = field(int, default=50000)
def __post_init__(self, salary_multiplier):
self.salary = int(self.salary * salary_multiplier)
addr = Address("123 Main St", "Anytown")
person = Person("Alice", 30, addr, salary_multiplier=1.5)
print(person)
# Person(name='Alice', age=30, address=Address(...), salary=75000)
print(asdict(person))
# {'name': 'Alice', 'age': 30, 'address': {...}, 'salary': 75000}
person2 = replace(person, age=31)
assert person2.age == 31
assert person.age == 30
assert Person.company == "Acme Corp"
assert is_dataclass(person)
More examples here.
loads() / dumps() use json by default and can use any serializer module
with loads() / dumps() functions:
import msgpack
from dataclasses import dumps, loads
payload = dumps(Point(3, 4), serializer=msgpack, use_bin_type=True)
point = loads(Point, payload, serializer=msgpack, raw=False)
load() / loads() reject unknown input keys by default. Pass
unknown=EXCLUDE to ignore unknown, ClassVar, or init=False input keys.
Scalar fields use marshmallow-style coercion by default; pass
strict_types=True to require values to already match their annotated runtime
types. dump() / dumps() serialize the current dataclass instance and do not
run load-time validation.
Dataclass helper caches are enabled by default with @dataclass(cache=True).
The repeated load() / loads() / validate() / validates() / dump() /
dumps() path reuses resolved field metadata for better performance. Classes
that intentionally update field definitions, field types, __annotations__,
__dataclass_fields__, or ClassVar / init=False metadata at runtime can opt
out per class with @dataclass(cache=False) or
make_dataclass(..., cache=False).
Development
Several testenvs provided to run tests in different configurations:
-
runs ported to py2.7 tests on backported
dataclasses(runs with py27)
-
-
runs ported to py2.7 tests on backported
dataclasses(runs with py3)
-
-
runs native python 3.14 tests on stdlib
dataclasses(runs with py3)
-
-
runs native python 3.14 tests on backported
dataclasses(runs with py3)
-
-
runs ported to py2.7 tests on stdlib
dataclasses(runs with py3)
-
Fixtures
tests._fixtures_py314dir contains original python 3.14 tests for thedataclasseslib. Test themselves are in__init__.pytests._fixtures_py27dir contains tests ported from python 3.14 to python 2.7 as closely as possible. Test themselves are in__init__.py
Helpers
tests._xmlrunneris a helper to run coverage alongside teststests._xmlprocessoris a helper for the CI
Adapters
tests._fixtures_compat_backport_to_stdlibtests._fixtures_compat_stdlib_to_backport
Source
dataclasses_py2dataclasses
⚠ WARNING
- Please, do not use Python 2.7 in 2026
Metadata
Release files for py2dataclasses 3.14.115
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py2dataclasses-3.14.115.tar.gz | 130.2 kB | Without provenance | Details for py2dataclasses-3.14.115.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py2dataclasses-3.14.115-py3-none-any.whl | Python 3 | none | any | Details for py2dataclasses-3.14.115-py3-none-any.whl |
Total release size: 174.6 kB
Release files / py2dataclasses-3.14.115.tar.gz
| Download URL | py2dataclasses-3.14.115.tar.gz |
|---|---|
| Size | 130.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bb7326bb92779ece10d833d7a5b68cc0e64eb012c07029b96c98144dccf88fd5
|
|
BLAKE2b-256 checksum How to use checksums |
8be168d3cadb3d77fe0898237281a02662348884f0e135d01ea35b714abcc3e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / py2dataclasses-3.14.115-py3-none-any.whl
| Download URL | py2dataclasses-3.14.115-py3-none-any.whl |
|---|---|
| Size | 44.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c8eec42b867eaec57792a25558f760ac9f603d5454845a25cf91c7d5e403a2ba
|
|
BLAKE2b-256 checksum How to use checksums |
72e0f46823b4d6cf0ddc99df5f3f75846a2ea20f6356e44c029e377115d4e7e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for py2dataclasses 3.14.115
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.