python / python/cpython

Incorrect tests for get_type_hints()

未关闭
#137,228 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib topic-typing type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从 Lib/test/test_typing.py 中的 test_get_type_hints_generic 和 test_get_type_hints_classes 开始,然后检查 _typed_dict_helper 中引用的定义。在继续之前,运行针对性的 typing 测试并查看链接的 PR gh-137230。完成意味着断言和名称解析预期反映 get_type_hints() 的预期行为,并且相关测试通过。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
testing-qa
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。