python / python/cpython

`_GenericAlias` caches origin's `__module__`

未关闭
#123,250 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:

When subscripting a class, __module__ gets saved. This prevents fixing it up later:

https://github.com/python/cpython/blob/4c3f0cbeaec0d49212d305618743fabb0e74a696/Lib/typing.py#L1411

from typing import Generic, TypeVar
T = TypeVar("T")

class A(Generic[T]):
  pass

x = A[T]  # this originally was in ... somewhere, but is here for minimization
A.__module__ = "new_module"
assert x.__module__ == "new_module"  # this is going to raise AssertionError

This came up when updating the Sphinx version in trio due to our handling of private/public. We update __module__ for these classes in __init__ (to make Sphinx work, among other things) but this doesn't end up updating any stored _GenericAliass' __module__s.

CPython versions tested on:

3.11

Operating systems tested on:

Windows

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 Lib/typing.py 中链接的 _GenericAlias 实现开始,并在 Python 3.11 上复现报告的 A[T] 示例。跟踪如何获取 alias 的 module,然后验证更改 A.module 会反映到 alias 中,同时不会破坏 typing 的现有行为。

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

评估

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

把新 issue 发到你的邮箱

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