`isinstance(obj, Hashable)` raises `TypeError` when both `obj` and `type(obj)` are unhashable
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Modules/_abc.c を開き、_abc__abc_subclasscheck_impl から、特に issue で説明されている _in_weak_set の呼び出しから調査を始めます。提供されている UnhashableMeta と UnhashableClass の再現コードを CPython で実行し、isinstance(UnhashableClass(), Hashable) が例外を発生させなくなっていること、また回帰が適切なテストでカバーされていることを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- c, python
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 45/100