Skip to content

Can not store bound method in WeakKeyDictionary while lambda works in python 3.15.2 #163057

Done

Description

@ThingamabobsCoding

Bug report

import weakref

storage = weakref.WeakKeyDictionary()

ltest = lambda: print('lambda')

class Test:

    def method(self):
        print('method')

inst = Test()
storage[ltest] = 'lval'
storage[inst.method] = 'cval'

for wref, val in storage.copy().items():
    print(wref)

Output:

<function <lambda> at 0x00000185DE78E610>

I expected 2 stored callables. I'm fairly convinced this used to work.
I also created also a question on StackOverflow: https://stackoverflow.com/q/80020034/13629335

Any help is appreciated.

CPython versions tested on:

3.15

Operating systems tested on:

Windows

Activity

  1. added
    type-bug
    on Dec 3, 2026
  2. ThingamabobsCoding commented on Dec 3, 2026

    @ThingamabobsCoding
    Author

    https://github.com/python/cpython/blob/main/Lib/weakref.py#L370 Indicates it just stores a regular weakref.ref and does not account for WeakMethod. Am I mistaken and this was always the case ?

  3. ThingamabobsCoding commented on Dec 3, 2026

    @ThingamabobsCoding
    Author

    The code never worked except in my memory. My bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions