Skip to content

Commit

Permalink
Warn and continue on syntaxerror
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Oct 6, 2023
1 parent df000ca commit b394308
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RELEASE_TYPE: patch

This patch ensures that the :ref:`hypothesis codemod <codemods>` CLI
will print a warning instead of stopping with an internal error if
one of your files contains invalid syntax (:issue:`3759`).
17 changes: 16 additions & 1 deletion hypothesis-python/src/hypothesis/extra/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,22 @@ def _refactor(func, fname):
except (OSError, UnicodeError) as err:
# Permissions or encoding issue, or file deleted, etc.
return f"skipping {fname!r} due to {err}"
newcode = func(oldcode)

if "hypothesis" not in oldcode:
return # This is a fast way to avoid running slow no-op codemods

try:
newcode = func(oldcode)
except Exception as err:
from libcst import ParserSyntaxError

if isinstance(err, ParserSyntaxError):
from hypothesis.extra._patching import indent

msg = indent(str(err).replace("\n\n", "\n"), " ").strip()
return f"skipping {fname!r} due to {msg}"
raise

if newcode != oldcode:
with open(fname, mode="w", encoding="utf-8") as f:
f.write(newcode)
Expand Down

0 comments on commit b394308

Please sign in to comment.