Skip to content

Commit

Permalink
Small refactoring commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jul 19, 2022
1 parent 91ef6fe commit 0ade3ab
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
18 changes: 7 additions & 11 deletions flake8_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,14 @@ class Plugin:
name = __name__
version = __version__

def __init__(
self, tree: Optional[ast.AST] = None, filename: Optional[str] = None
) -> None:
if tree is None:
assert filename is not None
self._tree = self.load_file(filename)
else:
self._tree = tree

def load_file(self, filename: str) -> ast.AST:
def __init__(self, tree: ast.AST) -> None:
self._tree = tree

@classmethod
def from_filename(cls, filename: str) -> "Plugin":
with tokenize.open(filename) as f:
return ast.parse(f.read())
source = f.read()
return cls(ast.parse(source))

def run(self) -> Generator[Tuple[int, int, str, Type[Any]], None, None]:
visitor = Visitor()
Expand Down
24 changes: 15 additions & 9 deletions tests/test_flake8_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@

import pytest
from hypothesis import HealthCheck, given, settings
from hypothesmith import from_grammar
from hypothesmith import from_grammar, from_node

from flake8_trio import TRIO100, Error, Plugin, Visitor, make_error


class Flake8TrioTestCase(unittest.TestCase):
def assert_expected_errors(self, test_file: str, *expected: Error) -> None:
filename = Path(__file__).absolute().parent / test_file
plugin = Plugin(filename=str(filename))
plugin = Plugin.from_filename(str(filename))
errors = tuple(plugin.run())
self.assertEqual(errors, expected)

def test_tree(self):
plugin = Plugin(tree=ast.parse(""))
plugin = Plugin(ast.parse(""))
errors = list(plugin.run())
self.assertEqual(errors, [])

Expand All @@ -43,22 +43,28 @@ def test_trio100_py39(self):

@pytest.mark.fuzz
class TestFuzz(unittest.TestCase):
@settings(suppress_health_check=[HealthCheck.too_slow])
@given(from_grammar().map(ast.parse))
@settings(max_examples=10_000, suppress_health_check=[HealthCheck.too_slow])
@given((from_grammar() | from_node()).map(ast.parse))
def test_does_not_crash_on_any_valid_code(self, syntax_tree: ast.AST):
# Given any syntatically-valid source code, the checker should
# not crash. This tests doesn't check that we do the *right* thing,
# just that we don't crash on valid-if-poorly-styled code!
Visitor().visit(syntax_tree)

def test_does_not_crash_on_site_code(self):
@staticmethod
def _iter_python_files():
# Because the generator isn't perfect, we'll also test on all the code
# we can easily find in our current Python environment - this includes
# the standard library, and all installed packages.
for base in sorted(set(site.PREFIXES)):
for dirname, _, files in os.walk(base):
for f in files:
if f.endswith(".py"):
self.assertFalse(
any(Plugin(filename=str(Path(dirname) / f)).run())
)
yield Path(dirname) / f

def test_does_not_crash_on_site_code(self):
for path in self._iter_python_files():
try:
Plugin.from_filename(str(path)).run()
except Exception as err:
raise AssertionError(f"Failed on {path}") from err
14 changes: 9 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# The test environment and commands
[tox]
skipsdist = True

[testenv:check]
description = Format code and run linters (quick)
Expand All @@ -11,23 +10,28 @@ deps =
flake8-bugbear
flake8-comprehensions
pyright
trio
# Required for pyright strict mode
hypothesis
hypothesmith
pytest
trio
ignore_errors =
# true means "run all, fail if any failed"
true
commands =
shed
flake8
pyright
flake8 --exclude .*,tests/trio*.py
pyright --pythonversion 3.10

[testenv:test]
description = Runs pytest, optionally with posargs
deps =
pytest
pytest-cov
pytest-xdist
hypothesis
hypothesmith
commands =
pip install -e .[dev]
pytest {posargs:-n auto}


Expand Down
5 changes: 0 additions & 5 deletions typings/pycodestyle/__init__.pyi

This file was deleted.

0 comments on commit 0ade3ab

Please sign in to comment.