chore: move to Ruff and add rules (#4483)

This commit is contained in:
Henry Schreiner
2023-02-22 06:18:55 -08:00
committed by GitHub
parent a19daeac16
commit 438034c5b8
35 changed files with 266 additions and 279 deletions

View File

@@ -83,9 +83,8 @@ class Output:
b = _strip_and_dedent(other).splitlines()
if a == b:
return True
else:
self.explanation = _make_explanation(a, b)
return False
self.explanation = _make_explanation(a, b)
return False
class Unordered(Output):
@@ -96,9 +95,8 @@ class Unordered(Output):
b = _split_and_sort(other)
if a == b:
return True
else:
self.explanation = _make_explanation(a, b)
return False
self.explanation = _make_explanation(a, b)
return False
class Capture:
@@ -119,9 +117,8 @@ class Capture:
b = other
if a == b:
return True
else:
self.explanation = a.explanation
return False
self.explanation = a.explanation
return False
def __str__(self):
return self.out
@@ -138,7 +135,7 @@ class Capture:
return Output(self.err)
@pytest.fixture
@pytest.fixture()
def capture(capsys):
"""Extended `capsys` with context manager and custom equality operators"""
return Capture(capsys)
@@ -159,25 +156,22 @@ class SanitizedString:
b = _strip_and_dedent(other)
if a == b:
return True
else:
self.explanation = _make_explanation(a.splitlines(), b.splitlines())
return False
self.explanation = _make_explanation(a.splitlines(), b.splitlines())
return False
def _sanitize_general(s):
s = s.strip()
s = s.replace("pybind11_tests.", "m.")
s = _long_marker.sub(r"\1", s)
return s
return _long_marker.sub(r"\1", s)
def _sanitize_docstring(thing):
s = thing.__doc__
s = _sanitize_general(s)
return s
return _sanitize_general(s)
@pytest.fixture
@pytest.fixture()
def doc():
"""Sanitize docstrings and add custom failure explanation"""
return SanitizedString(_sanitize_docstring)
@@ -186,30 +180,20 @@ def doc():
def _sanitize_message(thing):
s = str(thing)
s = _sanitize_general(s)
s = _hexadecimal.sub("0", s)
return s
return _hexadecimal.sub("0", s)
@pytest.fixture
@pytest.fixture()
def msg():
"""Sanitize messages and add custom failure explanation"""
return SanitizedString(_sanitize_message)
# noinspection PyUnusedLocal
def pytest_assertrepr_compare(op, left, right):
def pytest_assertrepr_compare(op, left, right): # noqa: ARG001
"""Hook to insert custom failure explanation"""
if hasattr(left, "explanation"):
return left.explanation
@contextlib.contextmanager
def suppress(exception):
"""Suppress the desired exception"""
try:
yield
except exception:
pass
return None
def gc_collect():
@@ -220,7 +204,7 @@ def gc_collect():
def pytest_configure():
pytest.suppress = suppress
pytest.suppress = contextlib.suppress
pytest.gc_collect = gc_collect