python / python/cpython

Possible shared-keys dict bug with string subclasses

未关闭
#144,689 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.14 interpreter-core type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

So I'm not entirely sure whether this is a bug or just undefined behaviour, because I wasn't intending to do it. An unexpected code path led to some sqlalchemy objects being used as attribute names in a class without my knowledge, which was working.. until in one window I had 3.14.2 running and suddenly got an AttributeError I hadn't seen before.

Tracking it down, it turned out to be that certain attributes were appearing and disappearing in very unexpected ways.

Reducing it to the minimum case I can find, the code

import sys

print(sys.version)

class StrSub(str):
    pass

class Widget:
    pass

a = Widget()
a.date = "a_value"

b = Widget()
setattr(b, StrSub("date"), "b_value")

print(f'("date" in b.__dict__):               {("date" in b.__dict__)!s:>5}')
print(f'("date" in b.__dict__.keys()):        {("date" in b.__dict__.keys())!s:>5}')
print(f'("date" in list(b.__dict__.keys())):  {("date" in list(b.__dict__.keys()))!s:>5}')
print(f'len(b.__dict__):                      {len(b.__dict__)!s:>5}')

behaves differently in 3.14.1 and 3.14.2 than most other versions:

3.12.12 (main, Feb  3 2026, 22:51:04) [Clang 21.1.4 ]
("date" in b.__dict__):                True
("date" in b.__dict__.keys()):         True
("date" in list(b.__dict__.keys())):   True
len(b.__dict__):                          1
 
3.13.12 (main, Feb  3 2026, 22:52:12) [Clang 21.1.4 ]
("date" in b.__dict__):                True
("date" in b.__dict__.keys()):         True
("date" in list(b.__dict__.keys())):   True
len(b.__dict__):                          1
 
3.14.0 (main, Nov 19 2025, 22:48:15) [Clang 21.1.4 ]
("date" in b.__dict__):                True
("date" in b.__dict__.keys()):         True
("date" in list(b.__dict__.keys())):   True
len(b.__dict__):                          1
 
3.14.1 (main, Dec  2 2025, 19:46:13) [Clang 21.1.4 ]
("date" in b.__dict__):               False
("date" in b.__dict__.keys()):        False
("date" in list(b.__dict__.keys())):  False
len(b.__dict__):                          0
 
3.14.2 (main, Jan 27 2026, 23:59:57) [Clang 21.1.4 ]
("date" in b.__dict__):                True
("date" in b.__dict__.keys()):         True
("date" in list(b.__dict__.keys())):  False
len(b.__dict__):                          0
 
3.15.0a5 (main, Feb  3 2026, 22:53:11) [Clang 21.1.4 ]
("date" in b.__dict__):                True
("date" in b.__dict__.keys()):         True
("date" in list(b.__dict__.keys())):   True
len(b.__dict__):                          1

This is deeper in the weeds than I know much about, though!

If I convert to a plain str, everything behaves as expected.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先,在列出的 Python 版本上运行提供的最小复现,并比较 dict、keys-view 和 list-of-keys 的结果。跟踪涉及字符串子类的 CPython 字典处理;完成意味着已理解并修正该行为,或通过回归测试明确解决该问题。

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

评估

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

把新 issue 发到你的邮箱

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