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
- https://github.com/psf/black/issues/3676
- https://github.com/psf/black/issues/3678
- https://github.com/psf/black/issues/3677
cpython
3.12
- https://github.com/python/cpython/issues/109219
- https://github.com/python/cpython/issues/109823
- https://github.com/python/cpython/issues/109719
- https://github.com/python/cpython/issues/109627
- https://github.com/python/cpython/issues/109219
- https://github.com/python/cpython/issues/109118
- https://github.com/python/cpython/issues/109114
- https://github.com/python/cpython/issues/109889
3.13
- https://github.com/python/cpython/issues/120367
- https://github.com/python/cpython/issues/120225
- https://github.com/python/cpython/issues/124746
- https://github.com/python/cpython/issues/124871
Todo:
- refactor the existing code
- add better context information to
probability() - remove
fix()function and move code intoprobability()
- add better context information to
- 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.7.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pysource_codegen-0.7.1.tar.gz | 71.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pysource_codegen-0.7.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 91.7 kB
Release files / pysource_codegen-0.7.1.tar.gz
| Download URL | pysource_codegen-0.7.1.tar.gz |
|---|---|
| Size | 71.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1a72d29591a9732fa9a66ee4976307081e56dd8991231b19a0bddf87a31d4c8e
|
|
BLAKE2b-256 checksum How to use checksums |
7da82bbac069fef2361eaef2c23706fc0de051034c14cc1c4ab3d88b095cfe5f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
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 Aug 29, 2025.
Transparency logRelease files / pysource_codegen-0.7.1-py3-none-any.whl
| Download URL | pysource_codegen-0.7.1-py3-none-any.whl |
|---|---|
| Size | 20.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
099e00d587a59babacaff902ad0b6d5b2f7344648c1e0baa981b30cc11a5c363
|
|
BLAKE2b-256 checksum How to use checksums |
63a5c851b7fac516d7ffb8943991c3ac46d7d29808b773d270d8b6b820cb2e51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
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 Aug 29, 2025.
Transparency log