Skip to content

Python 3.14.0a4: dataclasses no longer respects __annotations__ being set. #139140

@patrick-kidger

Description

@patrick-kidger

Bug report

Bug description:

The following code crashes on 3.14.0a4 but runs without error on 3.13:

import dataclasses

def my_dataclass(cls):
    try:
        annotations = cls.__dict__["__annotations__"]
    except KeyError:
        cls = dataclasses.dataclass()(cls)
    else:
        new_annotations = dict(annotations)
        for name in annotations.keys():
            if name == "ignore_this_annotation":
                new_annotations.pop(name)
        cls.__annotations__ = new_annotations
        cls = dataclasses.dataclass()(cls)
        cls.__annotations__ = annotations
    return cls

@my_dataclass
class X:
    x: int = 3
    ignore_this_annotation: int

with:

TypeError: non-default argument 'ignore_this_annotation' follows default argument 'x'

Python installed via uv venv --python=3.14.

x-ref patrick-kidger/optimistix#168 patrick-kidger/equinox#1096)

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-dataclassestopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions