python / python/cpython

3.14 changes annotations behaviour for dataclasses.make_dataclass

Aperta
#134,370 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.14 stdlib topic-dataclasses type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dal comportamento di dataclasses.make_dataclass e inspect.get_annotations descritto qui, quindi esamina le PR collegati gh-134387 e gh-134509. Il lavoro è completato quando il comportamento di 3.14 è risolto o documentato esplicitamente in modo coerente con il confronto con 3.13 e con l'annotations how-to.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.