Skip to content

Commit

Permalink
fix: Improve error reporting when source cannot be minimized (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored and 15r10nk committed Apr 23, 2024
1 parent 0519171 commit b7a3f29
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 b7a3f29

Please sign in to comment.