python / python/cpython

Incorrect tests for get_type_hints()

Đang mở
#137,228 0 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:

A few tests for typing.get_type_hints() test behavior that I feel is incorrect. Fixing it may be more trouble than it's worth, but I thought I'd record these.

  1. test_get_type_hints_generic

https://github.com/python/cpython/blob/5236b0281b91a874b14cf15f3fdef9b7beffb22f/Lib/test/test_typing.py#L8765

This tests that the a key for the TypedDict is T | None, where T is the TypeVar of that name defined in test_typing.py. However, the relevant TypedDict field is defined in a separate module _typed_dict_helper that has its own T TypeVar. get_type_hints() should pick up the T from the helper module, not the one from test_typing.

This also affects another assertion in the same function.

  1. test_get_type_hints_classes

https://github.com/python/cpython/blob/5236b0281b91a874b14cf15f3fdef9b7beffb22f/Lib/test/test_typing.py#L6636

This calls get_type_hints() on a class defined like this:

class B(Generic[T]):
    class A(Generic[T]):
        pass

    my_inner_a1: 'B.A'
    my_inner_a2: A
    my_outer_a: 'A'  # unless somebody calls get_type_hints with localns=B.__dict__

It checks that the unquoted A refers to the nested class and the quoted A refers to a different A class in the outer scope. But the fact that the name was quoted shouldn't influence scoping logic; it should resolve to the same name whether or not it's quoted.


I have a change that fixes both of these tests, but I'm not convinced we can afford to change this.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-137230

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

Bắt đầu với test_get_type_hints_generic và test_get_type_hints_classes trong Lib/test/test_typing.py, sau đó kiểm tra các định nghĩa được tham chiếu trong _typed_dict_helper. Chạy các bài kiểm thử typing tập trung và xem xét PR được liên kết gh-137230 trước khi tiếp tục. Hoàn tất có nghĩa là các assertion và kỳ vọng về phân giải tên phản ánh hành vi dự kiến của get_type_hints() và các bài kiểm thử liên quan đều đạt.

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
testing-qa
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 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.