Skip to main content

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

3.14

Sponsors

I would like to thank my sponsors. Without them, I would not be able to invest so much time in my projects.

Gold sponsor 🥇

logfire

Bronze sponsor 🥉

openai

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

Metadata

Release files for pysource-codegen 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pysource-codegen 0.8.0
File Size Uploaded Provenance Actions
pysource_codegen-0.8.0.tar.gz 74.9 kB With provenance Details for pysource_codegen-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pysource-codegen 0.8.0
File Interpreter ABI Platform Actions
pysource_codegen-0.8.0-py3-none-any.whl Python 3 none any Details for pysource_codegen-0.8.0-py3-none-any.whl

Total release size: 96.3 kB

Release files / pysource_codegen-0.8.0.tar.gz

Download URL pysource_codegen-0.8.0.tar.gz
Size 74.9 kB
Tags Source
SHA-256 checksum
How to use checksums
fdd2b18f3accc494783d54b91fefc5bba8dcc83f42464c2ade0a605bd8b99283
BLAKE2b-256 checksum
How to use checksums
d7f0e93f969577284d344d58c6fa3b97216e02188767dc33b0a581a5ac947d5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / pysource_codegen-0.8.0-py3-none-any.whl

Download URL pysource_codegen-0.8.0-py3-none-any.whl
Size 21.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b3989893e22b2b4d3a101a726478e8560924bdc9443d90a50fd84be394d3228
BLAKE2b-256 checksum
How to use checksums
0a4f1bbbbb41651a2b6265fc5c85cb1d3cade5cd65ffcc399aaa7e536d3489ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Security for pysource-codegen 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pysource_codegen-0.8.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page