|
11 | 11 | import math |
12 | 12 | from collections import defaultdict |
13 | 13 | from typing import NoReturn, Union |
| 14 | +from weakref import WeakKeyDictionary |
14 | 15 |
|
15 | 16 | from hypothesis import Verbosity, settings |
16 | 17 | from hypothesis._settings import note_deprecation |
@@ -168,18 +169,38 @@ def note(value: str) -> None: |
168 | 169 | report(value) |
169 | 170 |
|
170 | 171 |
|
171 | | -def event(value: str) -> None: |
172 | | - """Record an event that occurred this test. Statistics on number of test |
| 172 | +def event(value: str, payload: Union[str, int, float] = "") -> None: |
| 173 | + """Record an event that occurred during this test. Statistics on the number of test |
173 | 174 | runs with each event will be reported at the end if you run Hypothesis in |
174 | 175 | statistics reporting mode. |
175 | 176 |
|
176 | | - Events should be strings or convertible to them. |
| 177 | + Event values should be strings or convertible to them. If an optional |
| 178 | + payload is given, it will be included in the string for :ref:`statistics`. |
177 | 179 | """ |
178 | 180 | context = _current_build_context.value |
179 | 181 | if context is None: |
180 | 182 | raise InvalidArgument("Cannot make record events outside of a test") |
181 | 183 |
|
182 | | - context.data.note_event(value) |
| 184 | + payload = _event_to_string(payload, (str, int, float)) |
| 185 | + context.data.events[_event_to_string(value)] = payload |
| 186 | + |
| 187 | + |
| 188 | +_events_to_strings: WeakKeyDictionary = WeakKeyDictionary() |
| 189 | + |
| 190 | + |
| 191 | +def _event_to_string(event, allowed_types=str): |
| 192 | + if isinstance(event, allowed_types): |
| 193 | + return event |
| 194 | + try: |
| 195 | + return _events_to_strings[event] |
| 196 | + except (KeyError, TypeError): |
| 197 | + pass |
| 198 | + result = str(event) |
| 199 | + try: |
| 200 | + _events_to_strings[event] = result |
| 201 | + except TypeError: |
| 202 | + pass |
| 203 | + return result |
183 | 204 |
|
184 | 205 |
|
185 | 206 | def target(observation: Union[int, float], *, label: str = "") -> Union[int, float]: |
|
0 commit comments