Skip to content

Commit

Permalink
Improve error reporting when source cannot be minimized (#16)
Browse files Browse the repository at this point in the history
* Improve error reporting when source cannot be minimized

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
AlexWaygood and pre-commit-ci[bot] authored Apr 21, 2024
1 parent a1120c0 commit 3f76b9d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pysource_minimize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from ._minimize import CouldNotMinimize
from ._minimize import minimize
from ._minimize_base import StopMinimization


__all__ = ("minimize", "StopMinimization")
__all__ = ("minimize", "CouldNotMinimize", "StopMinimization")


version = "0.6.2"
9 changes: 8 additions & 1 deletion pysource_minimize/_minimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def minimize_ast(
return current_ast


class CouldNotMinimize(ValueError):
"""Raised to indicate that the source code could not be minimized."""


def minimize(
source: str,
checker: Callable[[str], bool],
Expand Down Expand Up @@ -87,7 +91,10 @@ def source_checker(new_ast):
return checker(source)

if not source_checker(original_ast):
raise ValueError("ast.unparse removes the error minimize can not help here")
raise CouldNotMinimize(
"Source code cannot be minimized: the error failed to reproduce "
"after roundtripping the source using `ast.parse()` and `ast.unparse()`"
)

minimized_ast = minimize_ast(
original_ast,
Expand Down

0 comments on commit 3f76b9d

Please sign in to comment.