Skip to content

Commit

Permalink
Add hook to check the file can be parsed as Python.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Jan 27, 2021
1 parent d8a06f0 commit 64387b8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
9 changes: 9 additions & 0 deletions doc-source/hooks.rst
Expand Up @@ -30,6 +30,15 @@ Pull ``# noqa: ...`` comments that immediately follow docstrings back up to the
This hook takes no arguments.


``check_ast``
--------------------

Check the source can be parsed as a Python Abstract Syntax Tree.
This could be called early in the execution -- to check the file is valid before starting reformatting -- and again at the end to ensure no errors were introduced by the reformatting.

This hook takes no arguments.


``ellipsis_reformat``
-----------------------

Expand Down
16 changes: 15 additions & 1 deletion formate/mini_hooks.py
Expand Up @@ -27,9 +27,10 @@
#

# stdlib
import ast
import re

__all__ = ["noqa_reformat"]
__all__ = ["noqa_reformat", "check_ast"]


def noqa_reformat(source: str) -> str:
Expand All @@ -42,3 +43,16 @@ def noqa_reformat(source: str) -> str:
"""

return re.sub(r'"""[\n\s]+#\s+noqa', '""" # noqa', source)


def check_ast(source: str) -> str:
"""
Check the source can be parsed as a Python Abstract Syntax Tree.
:param source: The source to check.
:raises SyntaxError: If the source is not valid Python.
"""

ast.parse(source)
return source
2 changes: 1 addition & 1 deletion tests/test_integration.py
Expand Up @@ -3,8 +3,8 @@

# 3rd party
import pytest
from coincidence.selectors import not_pypy, only_pypy
from coincidence.regressions import AdvancedDataRegressionFixture, check_file_output, check_file_regression
from coincidence.selectors import not_pypy, only_pypy
from consolekit.terminal_colours import strip_ansi
from consolekit.testing import CliRunner, Result
from domdf_python_tools.paths import PathPlus, in_directory
Expand Down
42 changes: 41 additions & 1 deletion tests/test_mini_hooks.py
@@ -1,5 +1,8 @@
# 3rd party
import pytest

# this package
from formate.mini_hooks import noqa_reformat
from formate.mini_hooks import check_ast, noqa_reformat


def test_noqa_reformat():
Expand All @@ -20,3 +23,40 @@ def test_noqa_reformat():
assert noqa_reformat('\n'.join(code)) == '\n'.join(expected)

assert noqa_reformat('\n'.join(expected)) == '\n'.join(expected)


def test_check_ast():
code = [
"def foo(:",
'\t"""',
"\tDoes something,",
'\t"""',
'',
" # noqa: D400 ",
]

with pytest.raises(SyntaxError):
check_ast('\n'.join(code))

code = [
"def foo():",
'"""',
"\tDoes something,",
'\t"""',
'',
" # noqa: D400 ",
]

with pytest.raises(SyntaxError):
check_ast('\n'.join(code))

code = [
"def foo():",
'\t"""',
"\tDoes something,",
'\t"""',
'',
" # noqa: D400 ",
]

assert check_ast('\n'.join(code)) == '\n'.join(code)

0 comments on commit 64387b8

Please sign in to comment.