generate random python code to test linter/formatter/and other tools
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a72d29591a9732fa9a66ee4976307081e56dd8991231b19a0bddf87a31d4c8e
|
|
| MD5 |
a71d7fbd0dbf849e48eef72df99bd3cc
|
|
| BLAKE2b-256 |
7da82bbac069fef2361eaef2c23706fc0de051034c14cc1c4ab3d88b095cfe5f
|
Provenance
The following attestation bundles were made for pysource_codegen-0.7.1.tar.gz:
Publisher:
ci.yml on 15r10nk/pysource-codegen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pysource_codegen-0.7.1.tar.gz -
Subject digest:
1a72d29591a9732fa9a66ee4976307081e56dd8991231b19a0bddf87a31d4c8e - Sigstore transparency entry: 450319075
- Sigstore integration time:
-
Permalink:
15r10nk/pysource-codegen@edaa52de9d112341999717bc6b5cb609c1dc2c28 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/15r10nk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@edaa52de9d112341999717bc6b5cb609c1dc2c28 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pysource_codegen-0.7.1-py3-none-any.whl.
File metadata
- Download URL: pysource_codegen-0.7.1-py3-none-any.whl
- Upload date:
- Size: 20.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
099e00d587a59babacaff902ad0b6d5b2f7344648c1e0baa981b30cc11a5c363
|
|
| MD5 |
cc74b014a9ce2e9e99c60bc54b3a3950
|
|
| BLAKE2b-256 |
63a5c851b7fac516d7ffb8943991c3ac46d7d29808b773d270d8b6b820cb2e51
|
Provenance
The following attestation bundles were made for pysource_codegen-0.7.1-py3-none-any.whl:
Publisher:
ci.yml on 15r10nk/pysource-codegen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pysource_codegen-0.7.1-py3-none-any.whl -
Subject digest:
099e00d587a59babacaff902ad0b6d5b2f7344648c1e0baa981b30cc11a5c363 - Sigstore transparency entry: 450319087
- Sigstore integration time:
-
Permalink:
15r10nk/pysource-codegen@edaa52de9d112341999717bc6b5cb609c1dc2c28 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/15r10nk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@edaa52de9d112341999717bc6b5cb609c1dc2c28 -
Trigger Event:
push
-
Statement type: