python / python/cpython

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

未關閉
#138,949 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib topic-typing type-bug
主要語言
Python
星號
77.2k
分支
35.9k
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. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

在受影響的 CPython 版本上執行提供的 TypedDict 重現程式,然後檢查 typing.py 中的 get_type_hints() 以及 traceback 中顯示的 _eval_type() 呼叫。比較與已連結 PR gh-150762 相關的行為和測試;當範例能夠解析其繼承的泛型註解且不引發 NameError 時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。