New dataclass features for old Pythons

apelapan1 pts1 comments

GitHub - arvidma/dataclasses: Vibe coded backports of 3.8+ dataclass features to 3.6 · GitHub

/" data-turbo-transient="true" />

Skip to content

Search or jump to...

Search code, repositories, users, issues, pull requests...

-->

Search

Clear

Search syntax tips

Provide feedback

--><br>We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Cancel

Submit feedback

Saved searches

Use saved searches to filter your results more quickly

-->

Name

Query

To see all available qualifiers, see our documentation.

Cancel

Create saved search

Sign in

/;ref_cta:Sign up;ref_loc:header logged out"}"<br>Sign up

Appearance settings

Resetting focus

You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

arvidma

dataclasses

Public

Notifications<br>You must be signed in to change notification settings

Fork

Star

master

BranchesTags

Go to file

CodeOpen more actions menu

Folders and files<br>NameNameLast commit message<br>Last commit date<br>Latest commit

History<br>103 Commits<br>103 Commits

.github/workflows

.github/workflows

issues/closed

issues/closed

.gitignore

.gitignore

LICENSE

LICENSE

dataclasses.py

dataclasses.py

readme.md

readme.md

ruff.toml

ruff.toml

test.py

test.py

View all files

Repository files navigation

Backports of Python dataclass features of 3.8+ to 3.6+

Adds most dataclass features from Python versions after 3.7 to the excellent but long-since<br>abandoned backport by Eric V. Smith (https://github.com/ericvsmith/dataclasses/).

For those of us still stuck writing Python for systems tied hard to the oldest LTS releases<br>of Ubuntu and RedHat.

Matrix of backported and not-yet-backported features:

Python version<br>Feature<br>Status

3.8<br>InitVar[T] generic syntax<br>Backported

3.8<br>replace() handling of InitVar fields with defaults<br>Backported

3.10<br>kw_only parameter and KW_ONLY sentinel<br>Backported

3.10<br>match_args parameter (__match_args__ generation)<br>Backported

3.10<br>slots parameter (__slots__ generation)<br>Backported

3.11<br>weakref_slot parameter<br>Backported

3.11<br>Frozen+slots pickling (__getstate__/__setstate__)<br>Backported

3.11<br>Slots with init=False default fields (bpo-44649)<br>Backported

3.11–3.12<br>fields() traceback improvements<br>Backported

3.11–3.12<br>Mutable default value validation<br>Backported

3.12<br>Field named BUILTINS in frozen classes (gh-96151)<br>Backported

3.12<br>Special underscore field names like _dflt_x (gh-98886)<br>Backported

3.12<br>Inherited __dict__ slot handling<br>Backported

3.12–3.13<br>Optimized __eq__ and __repr__ recursion guards<br>Backported

3.12<br>asdict()/astuple() atomic-type fast paths (gh-91896)<br>Backported

3.13<br>Single-exec method generation (_FuncBuilder, gh-107550)<br>Backported

3.13<br>Non-frozen subclass of frozen: mutable attr set/delete<br>Backported

3.14<br>decorator parameter for make_dataclass()<br>Backported

3.12<br>module parameter for make_dataclass()<br>Not backported

3.12<br>defaultdict support in asdict()/astuple()<br>Not backported

3.13<br>__replace__() method (copy.replace() support)<br>Not backported

Usage

Drop the single dataclasses.py file into your project (vendoring), or add it to<br>your Python path. It is a drop-in replacement for the standard library module and<br>can transparently replace the stdlib if running on a Python version that doesn't<br>support some feature you like.

Supports Python 3.6 and later from a single codebase — the only<br>version-sensitive code is the ClassVar detection, which adapts to the<br>typing internals of the running interpreter. The test suite runs in CI on<br>Python 3.6 through 3.13.

Note: don't mix this module with the stdlib dataclasses in the same class.<br>InitVar, Field, field() and the KW_ONLY sentinel are compared by<br>identity, so e.g. a stdlib InitVar annotation on a class decorated with this<br>module's @dataclass will not be recognized. Import everything from one module,<br>as in the example below.

try:<br>from dataclasses import dataclass, field, KW_ONLY<br>except ImportError:<br>from myproj.vendor.dataclasses import dataclass, field, KW_ONLY # type: ignore[no-redef]

@dataclass(slots=True, kw_only=True)<br>class Point:<br>x: float<br>y: float<br>z: float = 0.0

Full @dataclass decorator signature

@dataclass(<br>init=True, # 3.7<br>repr=True, # 3.7<br>eq=True, # 3.7<br>order=False, # 3.7<br>unsafe_hash=False, # 3.7<br>frozen=False, # 3.7<br>match_args=True, # 3.10<br>kw_only=False, # 3.10<br>slots=False, # 3.10<br>weakref_slot=False, # 3.11

About<br>Vibe coded backports of 3.8+ dataclass features to 3.6<br>Resources<br>Readme<br>Apache-2.0 license<br>Activity<br>Stars<br>1 star<br>Watchers<br>0 watching<br>Forks<br>0 forks<br>Report repository

Releases

Packages

Contributors

Languages

You can’t perform that action at this time.

backported dataclass dataclasses python true kw_only

Related Articles