Skip to content

3.14 changes annotations behaviour for dataclasses.make_dataclass #134370

Open
@rogerbinns

Description

@rogerbinns

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

Activity

JelleZijlstra

JelleZijlstra commented on May 20, 2025

@JelleZijlstra
Member

The presence of __annotations__ on instances (not classes) was always an undocumented feature and PEP-749 mentions that this will no longer work. We can document this better, though.

ambv

ambv commented on May 20, 2025

@ambv
Contributor

@JelleZijlstra is this expected as well?

>>> @dataclass
... class K:
...     i: int
...
>>> '__annotations__' in dir(K)
False
>>> dir(K)
['__annotate_func__', '__annotations_cache__', '__class__', '__dataclass_fields__', '__dataclass_params__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__match_args__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__replace__', '__repr__', '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__']
>>> K.__annotations__
{'i': <class 'int'>}
AlexKautz

AlexKautz commented on May 20, 2025

@AlexKautz
Contributor

I'll add documentation that the presence of __annotations__ on instances is not supported behavior.

JelleZijlstra

JelleZijlstra commented on May 20, 2025

@JelleZijlstra
Member

@ambv I haven't really thought about how dir() should work here, that might be worth changing.

This sort of thing is not new though:

$ python
Python 3.12.2 (main, May  6 2024, 20:50:52) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class K: pass
... 
>>> "__annotations__" in dir(K)
False
>>> K.__annotations__
{}
>>> 

@AlexKautz thanks! I'll review your PR.

rogerbinns

rogerbinns commented on May 20, 2025

@rogerbinns
Author

I'll add documentation that the presence of annotations on instances is not supported behavior.

It would be far preferable if the attribute just doesn't exist on instances, or __annotate_func__ / ___annotations_cache__ in 3.14.

The annotations related attributes being present on instances leads to Hyrum's Law, and what happened to me!

JelleZijlstra

JelleZijlstra commented on May 20, 2025

@JelleZijlstra
Member

__annotate_func__ and __annotations_cache__ are internal only and I reserve the right to make them work completely differently in the next release.

added a commit that references this issue on May 22, 2025

gh-134370: Added clarification on instance annotations (#134387)

bd4046f
added a commit that references this issue on May 22, 2025

pythongh-134370: Added clarification on instance annotations (pythonG…

added a commit that references this issue on May 22, 2025

[3.14] gh-134370: Added clarification on instance annotations (GH-134387

7e73918
added
stdlibPython modules in the Lib dir
3.14bugs and security fixes
on May 23, 2025

1 remaining item

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixesstdlibPython modules in the Lib dirtopic-dataclassestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      3.14 changes annotations behaviour for dataclasses.make_dataclass · Issue #134370 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant