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
Activity
JelleZijlstra commentedon May 20, 2025
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 commentedon May 20, 2025
@JelleZijlstra is this expected as well?
AlexKautz commentedon May 20, 2025
I'll add documentation that the presence of
__annotations__
on instances is not supported behavior.JelleZijlstra commentedon May 20, 2025
@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:
@AlexKautz thanks! I'll review your PR.
rogerbinns commentedon May 20, 2025
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 commentedon May 20, 2025
__annotate_func__
and__annotations_cache__
are internal only and I reserve the right to make them work completely differently in the next release.gh-134370: Added clarification on instance annotations (#134387)
pythongh-134370: Added clarification on instance annotations (pythonG…
[3.14] gh-134370: Added clarification on instance annotations (GH-134387
1 remaining item