Disable pytest assertion-rewrites inside Hypothesis, using magic module docstrings
See https://github.com/milesgranger/cramjam/issues/201 for the full details.
For some reason on some systems we reproducibly see a test failure with a traceback in hypothesis internals:
@pytest.mark.parametrize(
> "input_type", (bytes, bytearray, "numpy", cramjam.Buffer, cramjam.File, memoryview)
tests/test_variants.py:171:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
<frozen importlib._bootstrap>:1027: in _find_and_load
???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:688: in _load_unlocked
???
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/_pytest/assertion/rewrite.py:175: in exec_module
source_stat, co = _rewrite_test(fn, self.config)
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/_pytest/assertion/rewrite.py:355: in _rewrite_test
tree = ast.parse(source, filename=strfn)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
source = b'# This file is part of Hypothesis, which may be found at\n# [https://github.com/HypothesisWorks/hypothesis/\n#\n#](https://github.com/HypothesisWorks/hypothesis//n#\n#) Cop...ns.\n find_integer(lambda k: attempt_replace(k))\n find_integer(lambda k: attempt_replace(-k))\n'
filename = '/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/hypothesis/internal/conjecture/optimiser.py'
mode = 'exec'
def parse(source, filename='<unknown>', mode='exec', *,
type_comments=False, feature_version=None):
"""
Parse the source into an AST node.
Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
Pass type_comments=True to get back type comments where the syntax allows.
"""
flags = PyCF_ONLY_AST
if type_comments:
flags |= PyCF_TYPE_COMMENTS
if isinstance(feature_version, tuple):
major, minor = feature_version # Should be a 2-tuple.
assert major == 3
feature_version = minor
elif feature_version is None:
feature_version = -1
# Else it should be an int giving the minor version for 3.x.
> return compile(source, filename, mode, flags,
E File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/hypothesis/internal/conjecture/optimiser.py", line 52
E self.improvements = 0
E SyntaxError: could not convert string to float: 'V' - Consider hexadecimal for huge integer literals to avoid decimal conversion limits.
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/ast.py:50: SyntaxError
See for example this CI run.
It also only fails the first time the test runs after a fresh build, subsequent runs on the same build complete correctly.
It seems to have started with Hypothesis 6.123.0. I added an upper version pin for Hypothesis in this commit and the cramjam CI seems to be getting past the error: https://github.com/ngoldbaum/cramjam/actions/runs/13294977266
Sorry to not have easy steps to reproduce this - it's very obnoxious to debug and doesn't seem to be easily reproducible without going really far out of your way to trigger it. I've never actually triggered it outside of CI and then never been able to re-trigger it.
It looks like this isn't an error in Hypothesis per se, but rather an error in ast.parse() when Pytest's assertion-rewriting logic is applied to some of Hypothesis' source code. Which is still pretty strange!
While I don't know why this would happen, it seems reasonable for Hypothesis to disable rewrites by adding the magic string to our module docstrings...
@Zac-HD we still seem to be hitting this in the cramjam tests: https://github.com/milesgranger/cramjam/issues/201#issuecomment-2912182110