Class creation doesn't call __set_name__ when creating class members in another __set_name__
未关闭
还没有人认领这个 Issue。
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
When inserting a class member during some other class member's __set_name__ call, the __set_name__ method of the newly-inserted member doesn't get called as expected.
As an example, take this snippet:
class I_Am_Altering_The_Deal:
def __set_name__(self, owner, name):
print(f"__set_name__({self.__class__.__name__}, {owner.__name__}, {name!r}")
owner.han_solo = Pray_I_Dont_Alter_It_Any_Further()
class Pray_I_Dont_Alter_It_Any_Further:
def __set_name__(self, owner, name):
print(f"__set_name__({self.__class__.__name__}, {owner.__name__}, {name!r}")
class Cloud_City:
luke_skywalker = I_Am_Altering_The_Deal()
I would expect both __set_name__ functions to be called, resulting in the output:
__set_name__(I_Am_Altering_The_Deal, Cloud_City, 'luke_skywalker')
__set_name__(Pray_I_Dont_Alter_It_Any_Further, Cloud_City, 'han_solo')
But what is actually output, is:
__set_name__(I_Am_Altering_The_Deal, Cloud_City, 'luke_skywalker')
Discovered while testing a PEP-487 implementation for Micropython: micropython/micropython#15503.
Maybe be related to #72983.
CPython versions tested on:
3.8, 3.10, 3.12
Operating systems tested on:
Linux
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先运行 issue 中的类创建代码片段,并将其输出与预期的两次 set_name 调用进行比较。阅读 PEP-487 的实现和相关 issue #72983;当在另一个成员的 set_name 调用期间插入的成员收到其自身预期的回调,并具备回归测试覆盖时,修复才算完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 35/100