diff --git a/pysource_minimize/__init__.py b/pysource_minimize/__init__.py index aa438d3..953ff79 100644 --- a/pysource_minimize/__init__.py +++ b/pysource_minimize/__init__.py @@ -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" diff --git a/pysource_minimize/_minimize.py b/pysource_minimize/_minimize.py index d2dba48..7dc9b2a 100644 --- a/pysource_minimize/_minimize.py +++ b/pysource_minimize/_minimize.py @@ -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], @@ -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,