Skip to content

Commit

Permalink
Fix syntax errors in docstring code snippets:
Browse files Browse the repository at this point in the history
Some of the code examples in the docstrings where not valid Python code, so I
wrote a small test to ensure all the checks have valid Python code. This
doesn't do anything fancy like checking for imports (though maybe it should),
it merely checks that the AST tree can be built.
  • Loading branch information
dosisod committed Jan 9, 2024
1 parent a295cee commit f04d147
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
6 changes: 3 additions & 3 deletions docs/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ if x == y and x == z:

# and

if x is None and y is None
if x is None and y is None:
pass
```

Expand Down Expand Up @@ -2150,7 +2150,7 @@ Bad:
from hashlib import sha512

h = sha512()
h.update(b"data)
h.update(b"data")
```

Good:
Expand All @@ -2172,7 +2172,7 @@ Bad:

```python
nums = [123, 456]
num = f"{num[0]}")
num = f"{num[0]}"
```

Good:
Expand Down
2 changes: 1 addition & 1 deletion refurb/checks/hashlib/simplify_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ErrorInfo(Error):
from hashlib import sha512
h = sha512()
h.update(b"data)
h.update(b"data")
```
Good:
Expand Down
2 changes: 1 addition & 1 deletion refurb/checks/logical/use_equal_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ErrorInfo(Error):
# and
if x is None and y is None
if x is None and y is None:
pass
```
Expand Down
2 changes: 1 addition & 1 deletion refurb/checks/readability/use_str_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ErrorInfo(Error):
```
nums = [123, 456]
num = f"{num[0]}")
num = f"{num[0]}"
```
Good:
Expand Down
25 changes: 25 additions & 0 deletions test/test_check_formatting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import ast
import re
from functools import cache
from pathlib import Path
from textwrap import dedent

import pytest

import refurb
from refurb.error import Error
Expand Down Expand Up @@ -55,6 +59,26 @@ def get_categories_from_docs() -> list[str]:
return categories


def assert_python_snippets_are_parseable(error: type[Error]) -> None:
# This check is simple enough that it doesn't need detailed docs
if error.code == 105:
return

docs = dedent(error(0, 0, "").__doc__ or "")

groups = re.findall(r"```([\S\s]*?)```", docs)
assert groups, "Missing docstring"

assert len(groups) >= 2, 'Expected at least 2 "good" and "bad" code examples in docstring'

try:
for code in groups:
ast.parse(code.strip())

except SyntaxError as ex:
pytest.fail(f"Python code in docs is invalid: {ex.msg}")


def test_checks_are_formatted_properly() -> None:
error_names: set[str] = set()

Expand All @@ -68,6 +92,7 @@ def test_checks_are_formatted_properly() -> None:
assert_category_exists(error)
assert_categories_are_sorted(error)
assert_categories_are_valid(error, get_categories_from_docs())
assert_python_snippets_are_parseable(error)

assert error.name, "name field missing for class"

Expand Down

0 comments on commit f04d147

Please sign in to comment.