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.7.1.tar.gz (71.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysource_codegen-0.7.1-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pysource_codegen-0.7.1.tar.gz
  • Upload date:
  • Size: 71.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pysource_codegen-0.7.1.tar.gz
Algorithm Hash digest
SHA256 1a72d29591a9732fa9a66ee4976307081e56dd8991231b19a0bddf87a31d4c8e
MD5 a71d7fbd0dbf849e48eef72df99bd3cc
BLAKE2b-256 7da82bbac069fef2361eaef2c23706fc0de051034c14cc1c4ab3d88b095cfe5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysource_codegen-0.7.1.tar.gz:

Publisher: ci.yml on 15r10nk/pysource-codegen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for pysource_codegen-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 099e00d587a59babacaff902ad0b6d5b2f7344648c1e0baa981b30cc11a5c363
MD5 cc74b014a9ce2e9e99c60bc54b3a3950
BLAKE2b-256 63a5c851b7fac516d7ffb8943991c3ac46d7d29808b773d270d8b6b820cb2e51

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysource_codegen-0.7.1-py3-none-any.whl:

Publisher: ci.yml on 15r10nk/pysource-codegen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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