hypothesmith

raw JSON →
0.3.3 verified Fri May 01 auth: no python

Hypothesis strategies for generating Python programs, similar to CSmith. Current version 0.3.3; release cadence is low, mostly bugfixes. Requires Python >= 3.8.

pip install hypothesmith
error ModuleNotFoundError: No module named 'hypothesmith'
cause hypothesmith not installed.
fix
Run 'pip install hypothesmith'.
error hypothesis.errors.InvalidArgument: Expected a strategy, got ...
cause Incorrect import path; from_grammar returns a strategy but may be called incorrectly.
fix
Ensure you use 'from hypothesmith import from_grammar' and pass valid grammar rules.
error AttributeError: module 'hypothesmith' has no attribute 'from_node'
cause Using an older API that was removed or renamed.
fix
Use 'from hypothesmith import from_grammar' instead of from_node.
gotcha The grammar strings use angle brackets around rule names (e.g., '<stmt>'), not just the rule name.
fix Use '<stmt>' not 'stmt' when passing to from_grammar.
deprecated from_grammar() replaces the older from_node() API; from_node may be removed in future versions.
fix Use from_grammar() instead of from_node().
gotcha Generated code may be syntactically correct but semantically invalid (e.g., referencing undefined names).
fix Combine with Hypothesis strategies to generate valid names or use custom filters.

Generate Python code strings from grammar rules; use with Hypothesis to fuzz parsers or interpreters.

from hypothesmith import from_grammar
from hypothesis import given, strategies as st

# Generate a simple expression using Python's grammar
@given(from_grammar('<eval_input>', start='<eval_input>'))
def test_parse(code):
    assert isinstance(code, str)
    assert len(code) > 0

test_parse()