From 6a3f890ffc9e900cd81b311cb5aae78f87ac27bc Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Sun, 3 Jul 2022 00:40:25 -0700 Subject: [PATCH] Support non-weakrefable types --- hypothesis-python/RELEASE.rst | 4 ++++ .../src/hypothesis/internal/conjecture/engine.py | 7 +++++-- hypothesis-python/tests/cover/test_statistical_events.py | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 hypothesis-python/RELEASE.rst diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst new file mode 100644 index 0000000000..2384844e68 --- /dev/null +++ b/hypothesis-python/RELEASE.rst @@ -0,0 +1,4 @@ +RELEASE_TYPE: patch + +:func:`hypothesis.event` now works for hashable objects which do not +support weakrefs, such as integers and tuples. diff --git a/hypothesis-python/src/hypothesis/internal/conjecture/engine.py b/hypothesis-python/src/hypothesis/internal/conjecture/engine.py index b3fad161ee..750e68edd4 100644 --- a/hypothesis-python/src/hypothesis/internal/conjecture/engine.py +++ b/hypothesis-python/src/hypothesis/internal/conjecture/engine.py @@ -1067,10 +1067,13 @@ def event_to_string(self, event): return event try: return self.events_to_strings[event] - except KeyError: + except (KeyError, TypeError): pass result = str(event) - self.events_to_strings[event] = result + try: + self.events_to_strings[event] = result + except TypeError: + pass return result diff --git a/hypothesis-python/tests/cover/test_statistical_events.py b/hypothesis-python/tests/cover/test_statistical_events.py index 34ca34439e..2eaa1287e1 100644 --- a/hypothesis-python/tests/cover/test_statistical_events.py +++ b/hypothesis-python/tests/cover/test_statistical_events.py @@ -254,3 +254,8 @@ def test(value): stats = describe_statistics(call_for_statistics(test)) assert "- Events:" in stats assert "- Highest target score: " in stats + + +@given(st.booleans()) +def test_event_with_non_weakrefable_keys(b): + event((b,))