python / python/cpython

get_type_hints() fails on TypedDict inheriting from a generic TypedDict under PEP 563

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

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

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

説明

Bug report

Bug description:

This fails:

from __future__ import annotations
import typing
class TD1[T](typing.TypedDict):
    a: T

class TD2(TD1):
    pass

print(typing.get_type_hints(TD2))
$ uv run --python=3.13 sample3.py 
Traceback (most recent call last):
  File "/Users/jelle/py/cpython/sample3.py", line 9, in <module>
    print(typing.get_type_hints(TD2))
          ~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/opt/homebrew/Cellar/python@3.13/3.13.7/Frameworks/Python.framework/Versions/3.13/lib/python3.13/typing.py", line 2461, in get_type_hints
    value = _eval_type(value, base_globals, base_locals, base.__type_params__)
  File "/opt/homebrew/Cellar/python@3.13/3.13.7/Frameworks/Python.framework/Versions/3.13/lib/python3.13/typing.py", line 474, in _eval_type
    return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard)
           ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.13/3.13.7/Frameworks/Python.framework/Versions/3.13/lib/python3.13/typing.py", line 1081, in _evaluate
    eval(self.__forward_code__, globalns, localns),
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
NameError: name 'T' is not defined

Also reproduces on 3.12 and on the latest RC of 3.14.

(Got here while investigating https://github.com/python/cpython/issues/137226#issuecomment-3275500742, but since this variant is a longstanding bug we don't need to fix it for 3.14.0. I want to record and eventually fix it though.)

CPython versions tested on:

CPython main branch, 3.15, 3.14, 3.13, 3.12

Operating systems tested on:

macOS

Linked PRs
  • gh-150762

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

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

はじめの一歩

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

調査の方向性

提供された TypedDict の再現コードを影響を受ける CPython バージョンで実行し、その後 typing.py の get_type_hints() と、トレースバックに示されている _eval_type() の呼び出しを調べてください。リンクされた PR gh-150762 に関連する動作とテストを比較してください。例が継承したジェネリックアノテーションを NameError を発生させずに解決できれば完了です。

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

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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