python / python/cpython

weakref.WeakMethod equality behaves differently than weakref.ref (always False after garbage collection)

Open
#101,848 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

Equality comparison of weakref.ref objects succeeds both before and after object deletion. Equality of two weakref.WeakMethod objects, however, is always false if the underlying object has been deleted:

>>> from weakref import WeakMethod, ref
>>> class T:
...   def x(self): ...
...
>>> t = T()
>>> r1 = ref(t)
>>> r2 = ref(t)
>>> m1 = WeakMethod(t.x)
>>> m2 = WeakMethod(t.x)
>>> r1 == r2
True
>>> m1 == m2
True
>>> del t
>>> r1 == r2
True
>>> m1 == m2
False

Is this a bug? or perhaps desirable behavior or a fundamental limitation of bound method weak references?

Your environment

  • Python 3.11.0 | packaged by conda-forge | (main, Jan 15 2023, 05:44:48) [Clang 14.0.6 ] on darwin
  • macos x86_64, v12.6.1 (21G217)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python REPL example and compare weakref.ref with weakref.WeakMethod before and after garbage collection. Then inspect the WeakMethod and ref implementations and their equality-related tests, if present. Done means establishing the intended post-deletion equality semantics and adding coverage or a focused fix if the behavior is confirmed as a bug.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.