`isinstance(obj, Hashable)` raises `TypeError` when both `obj` and `type(obj)` are unhashable
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:
Assumption: isinstance(anything, any_type) should never raise.
The problem appears to be in _abc.c function _abc__abc_subclasscheck_impl (known at runtime as _abc._abc_subclasscheck()), which reads (in part):
/* 1. Check cache. */
incache = _in_weak_set(impl->_abc_cache, subclass);
The implementation assumes subclass is hashable. That's almost always true of classes, but not guaranteed.
Reproduction case:
from __future__ import annotations
from collections.abc import Hashable
class UnhashableMeta(type):
def __eq__(self, other: object) -> bool:
return super().__eq__(other)
class UnhashableClass(metaclass=UnhashableMeta):
def __eq__(self, other: object) -> bool:
return super().__eq__(other)
# any non-hashable instance of a non-hashable class
# ┌────────┴────────┐
isinstance(UnhashableClass(), Hashable)
Traceback for (cpython) Python 3.13.1:
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File ".../bug.py", line 14, in <module>
isinstance(UnhashableClass(), Hashable)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen abc>", line 119, in __instancecheck__
File "<frozen abc>", line 123, in __subclasscheck__
TypeError: unhashable type: 'UnhashableMeta'
Tracebacks are nearly identical with Python 3.8, 3.9, 3.10, 3.11, and 3.12, as well as Python 3.14.0a4.
CPython versions tested on:
3.13, 3.12, 3.11, 3.10, 3.9, 3.14
Operating systems tested on:
macOS
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
Mở Modules/_abc.c và bắt đầu từ _abc__abc_subclasscheck_impl, cụ thể là lời gọi _in_weak_set được mô tả trong issue. Chạy reproduction được cung cấp với UnhashableMeta và UnhashableClass trên CPython, sau đó xác minh rằng isinstance(UnhashableClass(), Hashable) không còn phát sinh exception và regression được bao phủ bởi một test phù hợp.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- c, python
- Lĩnh vực
- backend
- 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
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 45/100