python / python/cpython

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

Đang mở
#138,949 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib topic-typing type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Chạy trình tái hiện TypedDict được cung cấp trên một phiên bản CPython bị ảnh hưởng, sau đó kiểm tra typing.py tại get_type_hints() và lệnh gọi _eval_type() được hiển thị trong traceback. So sánh hành vi và các bài kiểm thử liên quan đến PR được liên kết gh-150762; hoàn thành khi ví dụ phân giải được annotation generic kế thừa mà không phát sinh NameError.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.