Skip to main content

generate random python code to test linter/formatter/and other tools

Project description

pypi version Python Versions PyPI - Downloads GitHub Sponsors

Introduction

pysource_codegen is able to generate random python code which can be compiled. The compiled code should not be executed.

This is still a very early version, but it does its job. It is general useful to test tools like formatters, linters or anything which operates on python code.

Install:

pip install pysource-codegen

Usage:

The tool can be used over the CLI:

pysource-codegen --seed 42

or as a library:

from pysource_codegen import generate

seed = 42
print(generate(seed))

You might find pysource-minimize also useful to reduce the generated code which triggers your bug down to a minimal code snipped, which can be used to fix the issue.

from pysource_codegen import generate
from pysource_minimize import minimize


def contains_bug(code):
    """
    returns True if the code triggers a bug and False otherwise
    """
    try:
        test_something_with(code)  # might throw

        if "bug" in code:  # maybe other checks
            return True
    except:
        return True
    return False


def find_issue():
    for seed in range(0, 10000):
        code = generate(seed)

        if contains_bug(code):
            new_code = minimize(code, contains_bug)

            print("the following code triggers a bug")
            print(new_code)

            return


find_issue()

Bugs found in other projects:

black

cpython

3.12

3.13

Todo:

  • refactor the existing code
    • add better context information to probability()
    • remove fix() function and move code into probability()
  • use probabilities for the ast-nodes from existing python code (use markov chains)
  • support older python versions
  • allow to customize the probabilities to generate code to test specific language features
  • hypothesis support

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

pysource_codegen-0.6.0.tar.gz (157.3 kB view details)

Uploaded Source

Built Distribution

pysource_codegen-0.6.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file pysource_codegen-0.6.0.tar.gz.

File metadata

  • Download URL: pysource_codegen-0.6.0.tar.gz
  • Upload date:
  • Size: 157.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.2

File hashes

Hashes for pysource_codegen-0.6.0.tar.gz
Algorithm Hash digest
SHA256 0337e3cf3639f017567ab298684c78eb15c877b093965259c725a13a4917be4e
MD5 08813652f2292a52a95e4c117d26b81c
BLAKE2b-256 385d1ab81f5f4eb3fb9203a91ea3cdb7da7b19b38993d669e39000a6454128e1

See more details on using hashes here.

File details

Details for the file pysource_codegen-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pysource_codegen-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 858f2bbed6de7a7b7e9bbea5cb8212ec7e66fa7f2a7ba433fe05f72438017b30
MD5 45f0eff3131c2d6f8cde405735cedb77
BLAKE2b-256 45e4c3a105d7d43bf237b18169eb1a06e1b93dc0daa96e74efa14c6d908979b4

See more details on using hashes here.

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