Skip to main content

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

Changelog

Patch notes can be found in CHANGELOG.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hypothesmith-0.2.1.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

hypothesmith-0.2.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file hypothesmith-0.2.1.tar.gz.

File metadata

  • Download URL: hypothesmith-0.2.1.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.14

File hashes

Hashes for hypothesmith-0.2.1.tar.gz
Algorithm Hash digest
SHA256 dc5577cc9ab77a8d42a12934cecdbaeee629c8f3b3a84a8a9114d9ef88636131
MD5 5f94ada0d5d34715d35eb10afe5ff3fb
BLAKE2b-256 2d2ee399d8fca2388a81056b111d8941d8586a05fa17404d7e4e30278380fe72

See more details on using hashes here.

Provenance

File details

Details for the file hypothesmith-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: hypothesmith-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.14

File hashes

Hashes for hypothesmith-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 27834ba322243fddf792c47a935eca3dbdfd5fa29bc7ebbf3a34dbb24e3c9644
MD5 1f2711ddcb785d826f1559db252f6b94
BLAKE2b-256 c103d01d1e1a81a9741740324ba6b2f8d9ccf90a1c8403909fba18217aaa2ffe

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page