Skip to content
New issue

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

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

Already on GitHub? # to your account

hypothesis.strategies.text fails if min_size is (unreasonably) large #3892

Closed
StefanBRas opened this issue Feb 21, 2024 · 2 comments · Fixed by #3894
Closed

hypothesis.strategies.text fails if min_size is (unreasonably) large #3892

StefanBRas opened this issue Feb 21, 2024 · 2 comments · Fixed by #3894

Comments

@StefanBRas
Copy link

Reproduction:

import hypothesis.strategies as st
st.text(min_size=19_000_000_000_000_000).example()

will throw:

  File "hypothesis/internal/conjecture/utils.py", line 235, in __init__
    self.p_continue = _calc_p_continue(average_size - min_size, max_size - min_size)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/hypothesis/internal/conjecture/utils.py", line 307, in _calc_p_continue
    assert 0 <= p_continue < 1, p_continue
           ^^^^^^^^^^^^^^^^^^^
 AssertionError: 1.0
Stacktrace
  File "<stdin>", line 1, in <module>
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/strategies/_internal/strategies.py", line 337, in example
    example_generating_inner_function()
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/strategies/_internal/strategies.py", line 326, in example_generating_inner_function
    @settings(
           ^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/core.py", line 1611, in wrapped_test
    raise the_error_hypothesis_found
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/core.py", line 1578, in wrapped_test
    state.run_engine()
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/core.py", line 1106, in run_engine
    runner.run()
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/engine.py", line 503, in run
    self._run()
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/engine.py", line 911, in _run
    self.generate_new_examples()
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/engine.py", line 641, in generate_new_examples
    zero_data = self.cached_test_function(bytes(BUFFER_SIZE))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/engine.py", line 1093, in cached_test_function
    self.test_function(data)
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/engine.py", line 234, in test_function
    self.__stoppable_test_function(data)
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/engine.py", line 212, in __stoppable_test_function
    self._test_function(data)
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/core.py", line 1033, in _execute_once_for_engine
    escalate_hypothesis_internal_error()
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/core.py", line 1006, in _execute_once_for_engine
    result = self.execute_once(data)
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/core.py", line 942, in execute_once
    result = self.test_runner(data, run)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/core.py", line 733, in default_executor
    return function(data)
           ^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/core.py", line 857, in run
    kw, argslices = context.prep_args_kwargs_from_strategies(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/control.py", line 157, in prep_args_kwargs_from_strategies
    obj = check(self.data.draw(s, observe_as=f"generate:{k}"))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/data.py", line 1755, in draw
    return strategy.do_draw(self)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/strategies/_internal/lazy.py", line 163, in do_draw
    return data.draw(self.wrapped_strategy)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/data.py", line 1750, in draw
    return strategy.do_draw(self)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/strategies/_internal/strings.py", line 87, in do_draw
    return data.draw_string(
           ^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/data.py", line 1627, in draw_string
    value = self.provider.draw_string(**kwargs, forced=forced)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/data.py", line 1174, in draw_string
    elements = many(
               ^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/utils.py", line 235, in __init__
    self.p_continue = _calc_p_continue(average_size - min_size, max_size - min_size)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sras/dev/python_testing/.venv/lib/python3.11/site-packages/hypothesis/internal/conjecture/utils.py", line 307, in _calc_p_continue
    assert 0 <= p_continue < 1, p_continue
           ^^^^^^^^^^^^^^^^^^^
AssertionError: 1.0

That isn't the exact number, but it will start breaking around there. Tested on hypothesis version '6.98.9'.

And yes I don't really want to generate that large of a string - I was accidentally feeding an uncapped generated number into the strategy.

@Zac-HD
Copy link
Member

Zac-HD commented Feb 21, 2024

Huh. This should be definitely give an error, but not that error!

@StefanBRas
Copy link
Author

Yeah, I just figured it was nice to catch earlier so you don't get a lot of bug reports (like this one).

It's definitely an edge case, so I can see why it hasn't been caught before. If you want to guard against it, you could try using some kind of property-based testing tool lol

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants