Skip to main content

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

  1. pip install py2dataclasses
  2.  # 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:

  • test_ported_on_portedlib_py27
    • runs ported to py2.7 tests on backported dataclasses (runs with py27)

  • test_ported_on_portedlib
    • runs ported to py2.7 tests on backported dataclasses (runs with py3)

  • test_native_on_nativelib
    • runs native python 3.14 tests on stdlib dataclasses (runs with py3)

  • test_native_on_portedlib
    • runs native python 3.14 tests on backported dataclasses (runs with py3)

  • test_ported_on_nativelib
    • runs ported to py2.7 tests on stdlib dataclasses (runs with py3)

Fixtures

  • tests._fixtures_py314 dir contains original python 3.14 tests for the dataclasses lib. Test themselves are in __init__.py
  • tests._fixtures_py27 dir contains tests ported from python 3.14 to python 2.7 as closely as possible. Test themselves are in __init__.py

Helpers

  • tests._xmlrunner is a helper to run coverage alongside tests
  • tests._xmlprocessor is a helper for the CI

Adapters

  • tests._fixtures_compat_backport_to_stdlib
  • tests._fixtures_compat_stdlib_to_backport

Source

  • dataclasses
  • _py2dataclasses

⚠ WARNING

  • Please, do not use Python 2.7 in 2026

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py2dataclasses-3.14.114.tar.gz (41.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

py2dataclasses-3.14.114-py3-none-any.whl (33.4 kB view details)

Uploaded Python 3

py2dataclasses-3.14.114-py2-none-any.whl (33.3 kB view details)

Uploaded Python 2

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

Hashes for py2dataclasses-3.14.114.tar.gz
Algorithm Hash digest
SHA256 7ad2639bd6ae71d4b3a8b4fa80b3eedf665afb26d9d4e4d311f115dd77e37a1e
MD5 f73c5617eabdc9e0f3035550299043e5
BLAKE2b-256 1fa5b5e47ae392f98ebf0745fedd6a6ebbb829caf2754993e8870b2dd4724734

See more details on using hashes here.

File details

Details for the file py2dataclasses-3.14.114-py3-none-any.whl.

File metadata

File hashes

Hashes for py2dataclasses-3.14.114-py3-none-any.whl
Algorithm Hash digest
SHA256 f6d91413678ae7ca286f14de6f73891ffa1d7a7857c5391f507c3a856c62d1c2
MD5 225f4d32760a856302ff0f26e2c4e369
BLAKE2b-256 a98bc4649a107046bc820d4500614d672239be168662f6bb8f18476989a262e4

See more details on using hashes here.

File details

Details for the file py2dataclasses-3.14.114-py2-none-any.whl.

File metadata

File hashes

Hashes for py2dataclasses-3.14.114-py2-none-any.whl
Algorithm Hash digest
SHA256 7b8a57a230292d6ad7ecd5cd81802453d4cf320ff78c98e6be4481324e2f4370
MD5 c3d921e58632394b1f3fcaa97ec2dc4a
BLAKE2b-256 c5af40abca1a73b79357753d3b3e43634b5ebc321aa837f5245e501aa1886d69

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page