From 3f76b9d900c1be9b337c8236c71821c0bed3a446 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 21 Apr 2024 04:32:15 +0100 Subject: [PATCH] Improve error reporting when source cannot be minimized (#16) * 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> --- pysource_minimize/__init__.py | 3 ++- pysource_minimize/_minimize.py | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) 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,