Skip to main content

ci

pypi version Python Versions PyPI - Downloads GitHub Sponsors

pysource-minimize

If you build a linter, formatter or any other tool which has to analyse python source code you might end up searching bugs in pretty large input files.

pysource_minimize is able to remove everything from the python source which is not related to the problem.

CLI

You can use pysource-minimize from the command line like follow:

pysource-minimize --file bug.py --track "Assertion" -- python bug.py

This executes python bug.py and tries to find the string “Assertion” in the output. The ---file bug.py will be minimized as long as “assertion” is part of the output of the command. The --file option can be specified multiple times and there is also an --dir option which can be used to search directories recursively for Python files.

example

API

Example for single files:

>>> from pysource_minimize import minimize

>>> source = """
... def f():
...     print("bug"+"other string")
...     return 1+1
... f()
... """

>>> print(minimize(source, lambda new_source: "bug" in new_source))
"""bug"""

This example minimizes multiple files and searches for sets which have 2 common elements:

>>> from pathlib import Path
>>> from typing import Dict
>>> from pprint import pprint
>>> from pysource_minimize._minimize import minimize_all
>>> sources = {
...     Path("a.py"): """\
... l={1,81894,9874,89228,897985,897498,9879,9898}
...     """,
...     Path("b.py"): """\
... l={5,81894,9274,89218,897985,897298,9879,9898}
...     """,
...     Path("c.py"): """\
... l={0,81894,9874,89218,897985,897498,9879,9298}
...     """,
... }
>>> def check(sources: Dict[Path, str | None], current_filename: Path) -> bool:
...     # current_filename can be used for progress output
...     # print(f"working on {current_filename} ...")
...     sets = []
...     for source in sources.values():
...         if source is not None:
...             globals = {}
...             try:
...                 exec(source, globals)
...             except:
...                 return False
...             if "l" not in globals:
...                 return False
...             sets.append(globals["l"])
...     return (
...         len(sets) >= 2
...         and all(isinstance(s, set) for s in sets)
...         and len(set.intersection(*sets)) >= 2
...     )
...
>>> pprint(minimize_all(sources, checker=check))
{PosixPath('a.py'): None,
 PosixPath('b.py'): 'l = {81894, 0}',
 PosixPath('c.py'): 'l = {0, 81894}'}

You might think that there are no two zeros in the original sets. This problem can occur if your check function is not specific enough. pysource-minimize tries to minimize numbers and strings, does so for one of the sets and finds that it satisfies your check. It generates new code during the minimization and can only use the check function to know if the solution is correct. This kind of problem can be solved by using a more precise check function or a --track argument when using the CLI. For example, you can add a check that all numbers in the set must be non-zero. However, this problem will not occur if you are looking for real minimal examples that throw certain exceptions. The worst that can happen here is that pysource-minimize finds another example that triggers the same problem.

fixed check function
>>> from pathlib import Path
>>> from typing import Dict
>>> from pprint import pprint
>>> from pysource_minimize._minimize import minimize_all
>>> sources = {
...     Path("a.py"): """\
... l={1,81894,9874,89228,897985,897498,9879,9898}
...     """,
...     Path("b.py"): """\
... l={5,81894,9274,89218,897985,897298,9879,9898}
...     """,
...     Path("c.py"): """\
... l={0,81894,9874,89218,897985,897498,9879,9298}
...     """,
... }
>>> def check(sources: Dict[Path, str | None], current_filename: Path) -> bool:
...     # current_filename can be used for progress output
...     # print(f"working on {current_filename} ...")
...     sets = []
...     for source in sources.values():
...         if source is not None:
...             globals = {}
...             try:
...                 exec(source, globals)
...             except:
...                 return False
...             if "l" not in globals:
...                 return False
...             sets.append(globals["l"])
...     return (
...         len(sets) >= 2
...         and all(isinstance(s, set) for s in sets)
...         and len(result := set.intersection(*sets)) >= 2
...         and 0 not in result
...     )
...
>>> pprint(minimize_all(sources, checker=check))
{PosixPath('a.py'): None,
 PosixPath('b.py'): 'l = {81894, 89218}',
 PosixPath('c.py'): 'l = {81894, 89218}'}

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

Similar projects

  • shrinkray which can not only minimize python code but C, C++, JSON and Dimacs too.

Metadata

Release files for pysource-minimize 0.11.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-minimize 0.11.0
File Size Uploaded Provenance Actions
pysource_minimize-0.11.0.tar.gz 9.4 MB With provenance Details for pysource_minimize-0.11.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pysource-minimize 0.11.0
File Interpreter ABI Platform Actions
pysource_minimize-0.11.0-py3-none-any.whl Python 3 none any Details for pysource_minimize-0.11.0-py3-none-any.whl

Total release size: 9.4 MB

Release files / pysource_minimize-0.11.0.tar.gz

Download URL pysource_minimize-0.11.0.tar.gz
Size 9.4 MB
Tags Source
SHA-256 checksum
How to use checksums
bce7d9d6f1f618136500976e3957b8a41cd2661dfa408447ff06f3c839343d5b
BLAKE2b-256 checksum
How to use checksums
1f16a3965300b6bf2bc9109ddd18ed1d4598548698d013cae93110f1541bbab7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 7, 2026.

Transparency log

Release files / pysource_minimize-0.11.0-py3-none-any.whl

Download URL pysource_minimize-0.11.0-py3-none-any.whl
Size 19.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
06ee932a56fe5910bae3b05f7f4c4c5f566154f29a3144154c05cd7ff1f9752c
BLAKE2b-256 checksum
How to use checksums
b3f0606d12ec8eb06d32ffbc2dce0cb3d78c18b43bc85afae21ccdd6d66432a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 7, 2026.

Transparency log

Security for pysource-minimize 0.11.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_minimize-0.11.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 7, 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