python / python/cpython

3.14 changes annotations behaviour for dataclasses.make_dataclass

オープン
#134,370 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

3.14 stdlib topic-dataclasses type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

ここで説明されている dataclasses.make_dataclass と inspect.get_annotations の動作から始め、リンクされている PR gh-134387 と gh-134509 を確認してください。3.14 の動作が解決されるか、3.13 との比較および annotations how-to と一貫する形で明示的に文書化されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。