PEP-557 compatible dataclasses backport for Python 2.7+
Project description
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.
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py2dataclasses-3.14.114.tar.gz.
File metadata
- Download URL: py2dataclasses-3.14.114.tar.gz
- Upload date:
- Size: 41.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ad2639bd6ae71d4b3a8b4fa80b3eedf665afb26d9d4e4d311f115dd77e37a1e
|
|
| MD5 |
f73c5617eabdc9e0f3035550299043e5
|
|
| BLAKE2b-256 |
1fa5b5e47ae392f98ebf0745fedd6a6ebbb829caf2754993e8870b2dd4724734
|
File details
Details for the file py2dataclasses-3.14.114-py3-none-any.whl.
File metadata
- Download URL: py2dataclasses-3.14.114-py3-none-any.whl
- Upload date:
- Size: 33.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6d91413678ae7ca286f14de6f73891ffa1d7a7857c5391f507c3a856c62d1c2
|
|
| MD5 |
225f4d32760a856302ff0f26e2c4e369
|
|
| BLAKE2b-256 |
a98bc4649a107046bc820d4500614d672239be168662f6bb8f18476989a262e4
|
File details
Details for the file py2dataclasses-3.14.114-py2-none-any.whl.
File metadata
- Download URL: py2dataclasses-3.14.114-py2-none-any.whl
- Upload date:
- Size: 33.3 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b8a57a230292d6ad7ecd5cd81802453d4cf320ff78c98e6be4481324e2f4370
|
|
| MD5 |
c3d921e58632394b1f3fcaa97ec2dc4a
|
|
| BLAKE2b-256 |
c5af40abca1a73b79357753d3b3e43634b5ebc321aa837f5245e501aa1886d69
|