python / python/cpython

3.14 changes annotations behaviour for dataclasses.make_dataclass

Open
#134,370 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.14 stdlib topic-dataclasses type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

I have many dataclasses created on demand by dataclasses.make_dataclass. 3.14 changes behaviour for instances of the dataclass.

In 3.13 and before, the instances have a __annotations__ attribute with the expected contents.

In 3.14, that attribute no longer exists, but calling instance.__annotate_func__ does return what was in __annotations__ before.

The 3.14 howto does say "you may access the annotations data member manually"

import dataclasses
import inspect

dc = dataclasses.make_dataclass("foo", (("one", int), ("two", str), ("three", complex)))

value = dc(1, 'hello', 3+4j)

print(f"{dir(value)=}")

try:
    print(inspect.get_annotations(value))
except Exception as exc:
    print("inspect.get_annotations exception: ", type(exc), exc)

if hasattr(dc, "__annotate_func__"):
    print(f"{value.__annotate_func__()=}")

For Python 3.13:

dir(value)=['__annotations__', '__class__', '__dataclass_fields__', '__dataclass_params__ ....

For Python 3.14:

dir(value)=['__annotate_func__', '__annotations_cache__',  '__class__', '__dataclass_fields__' ....

In both cases inspect.get_annotations() does give TypeError.

The release notes cover annotations, but nothing about changes to dataclasses instances.

Perhaps the annotations attributes shouldn't be on instances, but they have been. I also see __annotate_func__ on instances of regular objects from Python classes.

CPython versions tested on:

3.14, 3.13

Operating systems tested on:

Linux

Linked PRs
  • gh-134387
  • gh-134509

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the dataclasses.make_dataclass and inspect.get_annotations behavior described here, then review linked PRs gh-134387 and gh-134509. Done means the 3.14 behavior is resolved or explicitly documented consistently with the 3.13 comparison and the annotations how-to.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.