Hypothesis strategies for generating Python programs, something like CSmith
Project description
hypothesmith
Hypothesis strategies for generating Python programs, something like CSmith.
This is definitely pre-alpha, but if you want to play with it feel free! You can even keep the shiny pieces when - not if - it breaks.
Get it today with pip install hypothesmith
,
or by cloning the GitHub repo.
You can run the tests, such as they are, with tox
on Python 3.6 or later.
Use tox -va
to see what environments are available.
Usage
This package provides two Hypothesis strategies for generating Python source code.
The generated code will always be syntatically valid, and is useful for testing parsers, linters, auto-formatters, and other tools that operate on source code.
DO NOT EXECUTE CODE GENERATED BY THESE STRATEGIES.
It could do literally anything that running Python code is able to do, including changing, deleting, or uploading important data. Arbitrary code can be useful, but "arbitrary code execution" can be very, very bad.
hypothesmith.from_grammar(start="file_input", *, auto_target=True)
Generates syntactically-valid Python source code based on the grammar.
Valid values for start
are "single_input"
, "file_input"
, or
"eval_input"
; respectively a single interactive statement, a module or
sequence of commands read from a file, and input for the eval() function.
If auto_target
is True
, this strategy uses hypothesis.target()
internally to drive towards larger and more complex examples. We recommend
leaving this enabled, as the grammar is quite complex and only simple examples
tend to be generated otherwise.
hypothesmith.from_node(node=libcst.Module, *, auto_target=True)
Generates syntactically-valid Python source code based on the node types
defined by the LibCST
project.
You can pass any subtype of libcst.CSTNode
. Alternatively, you can use
Hypothesis' built-in from_type(node_type).map(lambda n: libcst.Module([n]).code
,
after Hypothesmith has registered the required strategies. However, this does
not include automatic targeting and limitations of LibCST may lead to invalid
code being generated.
Notable bugs found with Hypothesmith
- BPO-40661, a segfault in the new parser, was given maximum priority and blocked the planned release of CPython 3.9 beta1.
- BPO-38953
tokenize
->untokenize
roundtrip bugs. lib2to3
errors on \r in comment- Black fails on files ending in a backslash
- At least three round-trip bugs in LibCST (search commits for "hypothesis")
- Invalid code generated by LibCST
Changelog
Patch notes can be found in CHANGELOG.md
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for hypothesmith-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47fd14a31aada35590b6b26e595c41f4996e809b3ce8939528097913d1073725 |
|
MD5 | cf14403aaf9a0a4e8e8ea41cbd1db082 |
|
BLAKE2b-256 | 48c6aace3cdd0a6dd58d056fdffff730b07f3b93c751655df7f9137d15fe1fe9 |