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 Common errors
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.
Warnings
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.
Imports
- from hypothesmith import from_grammar wrong
import hypothesmith; hypothesmith.from_grammarcorrectfrom hypothesmith import from_grammar - from hypothesmith import from_grammar wrong
from hypothesmith import versioncorrectfrom hypothesmith import from_grammar, __version__
Quickstart
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()