Possible shared-keys dict bug with string subclasses
Chưa có ai nhận issue này.
- 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:
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách chạy bản tái hiện tối thiểu được cung cấp trên các phiên bản Python đã liệt kê và so sánh kết quả của dict, keys-view và list-of-keys. Theo dõi cách CPython xử lý dictionary liên quan đến các lớp con của chuỗi; được coi là hoàn tất khi hành vi đã được hiểu và sửa lỗi, hoặc được giải quyết rõ ràng bằng một bài kiểm thử hồi quy.
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
- compilers
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 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