Search code examples
python-hypothesis

`event` throws TypeError caused by WeakKeyDictionary when tuple is passed


I wanted to see the statistics of list values and I passed the value to event() after converting it to tuple to make it hashable.

@dataclass(frozen=True)
class Foo:
    x: int
    y: str

    def __hash__(self):
        return hash((self.x, self.y))

@given(lists(builds(Foo, integers(), text()), max_size=3))
def test_foo(l):
    t = tuple(l)
    event(t)

However, it still doesn't work because of the following error:

self = <WeakKeyDictionary at 0x105c17430>, key = ()

    def __getitem__(self, key):
>       return self.data[ref(key)]
E       TypeError: cannot create weak reference to 'tuple' object

../../../.pyenv/versions/3.10.4/lib/python3.10/weakref.py:416: TypeError

Because tuple is builtin, I don't come up with the easy work around. I'm interested in how others are doing when they want to see list/tuple values with event().


Solution

  • Hmm, I'll take this as a bug report - as a workaround you can convert to str, since events are always counted as strings in the end anyway.

    (update: fixed a few hours later)