Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More clear behavior when passing non-strings to note #3843

Closed
tybug opened this issue Jan 14, 2024 · 1 comment · Fixed by #3847
Closed

More clear behavior when passing non-strings to note #3843

tybug opened this issue Jan 14, 2024 · 1 comment · Fixed by #3847
Labels
legibility make errors helpful and Hypothesis grokable

Comments

@tybug
Copy link
Member

tybug commented Jan 14, 2024

Consider the following:

from hypothesis import *
from hypothesis.strategies import *

class A:
    pass

@given(builds(A))
def f(a):
    note(a)
    assert False

f()

This errors with the following traceback:

Traceback (most recent call last):
  File "/Users/tybug/Desktop/Liam/coding/sandbox/__hypothesis.py", line 12, in <module>
    f()
  File "/Users/tybug/Desktop/Liam/coding/sandbox/__hypothesis.py", line 8, in f
    def f(a):
              
  File "/Users/tybug/Desktop/Liam/coding/hypothesis/hypothesis-python/src/hypothesis/core.py", line 1599, in wrapped_test
    raise the_error_hypothesis_found
  File "/Users/tybug/Desktop/Liam/coding/hypothesis/hypothesis-python/src/hypothesis/core.py", line 1566, in wrapped_test
    state.run_engine()
  File "/Users/tybug/Desktop/Liam/coding/hypothesis/hypothesis-python/src/hypothesis/core.py", line 1214, in run_engine
    _raise_to_user(errors_to_report, self.settings, report_lines)
  File "/Users/tybug/Desktop/Liam/coding/hypothesis/hypothesis-python/src/hypothesis/core.py", line 1223, in _raise_to_user
    add_note(err, note)
  File "/Users/tybug/Desktop/Liam/coding/hypothesis/hypothesis-python/src/hypothesis/internal/compat.py", line 50, in add_note
    exc.add_note(note)
TypeError: note must be a str, not 'A'

In this case, it's relatively obvious that the note call is the culprit. But it may be more confusing in more complicated functions, since that information gets trimmed from the traceback.

I propose we either:

  • fail early and explicitly when note is passed a non-str value
  • automatically coerce note(x) to report(str(x)) instead of report(x)

This came up in practice in

try:
runner.run()
finally:
if data is not None:
note(root)
assume(runner.interesting_examples)
(fix to come!)

@tybug tybug added the legibility make errors helpful and Hypothesis grokable label Jan 14, 2024
@Zac-HD
Copy link
Member

Zac-HD commented Jan 14, 2024

Let's use our pretty-printer for non-strings, to give a maximally-informative string form. Thanks for spotting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legibility make errors helpful and Hypothesis grokable
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants