Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error reporting when source cannot be minimized #16

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading