Skip to content

Commit

Permalink
Merge pull request #369 from testing-cabal/ruff
Browse files Browse the repository at this point in the history
Make ruff-clean and enable ruff in CI
  • Loading branch information
jelmer committed Nov 24, 2023
2 parents ad2e490 + 0dfade5 commit 8dc8116
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 36 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Expand Up @@ -33,10 +33,14 @@ jobs:
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade setuptools wheel setuptools_scm
python3 -m pip install --upgrade setuptools wheel setuptools_scm ruff
python3 -m pip install sphinx
python3 -m pip install ".[test,twisted]"
- name: Ruff
run: |
python -m ruff check .
- name: Tests
run: |
python -W once -m testtools.run testtools.tests.test_suite
Expand Down
4 changes: 1 addition & 3 deletions testtools/__init__.py
Expand Up @@ -47,9 +47,7 @@
]

from testtools.helpers import try_import
from testtools.matchers._impl import Matcher
# Shut up, pyflakes. We are importing for documentation, not for namespacing.
Matcher
from testtools.matchers._impl import Matcher # noqa: F401

from testtools.runtest import (
MultipleExceptions,
Expand Down
4 changes: 2 additions & 2 deletions testtools/compat.py
Expand Up @@ -83,8 +83,8 @@ def text_repr(text, multiline=None):
# making sure that quotes are not escaped.
offset = len(prefix) + 1
lines = []
for l in text.split(nl):
r = repr(l)
for line in text.split(nl):
r = repr(line)
q = r[-1]
lines.append(r[offset:-1].replace("\\" + q, q))
# Combine the escaped lines and append two of the closing quotes,
Expand Down
16 changes: 9 additions & 7 deletions testtools/matchers/_datastructures.py
@@ -1,12 +1,5 @@
# Copyright (c) 2009-2015 testtools developers. See LICENSE for details.

__all__ = [
'ContainsAll',
'MatchesListwise',
'MatchesSetwise',
'MatchesStructure',
]

"""Matchers that operate with knowledge of Python data structures."""

from ..helpers import map_values
Expand All @@ -17,6 +10,15 @@
)
from ._impl import Mismatch

__all__ = [
'ContainsAll',
'MatchesListwise',
'MatchesSetwise',
'MatchesStructure',
]




def ContainsAll(items):
"""Make a matcher that checks whether a list of things is contained
Expand Down
9 changes: 6 additions & 3 deletions testtools/matchers/_dict.py
Expand Up @@ -180,7 +180,8 @@ class MatchesDict(_CombinedMatcher):
'Differences': _MatchCommonKeys,
}

format_expected = lambda self, expected: _format_matcher_dict(expected)
def format_expected(self, expected) -> str:
return _format_matcher_dict(expected)


class ContainsDict(_CombinedMatcher):
Expand All @@ -203,7 +204,8 @@ class ContainsDict(_CombinedMatcher):
'Differences': _MatchCommonKeys,
}

format_expected = lambda self, expected: _format_matcher_dict(expected)
def format_expected(self, expected):
return _format_matcher_dict(expected)


class ContainedByDict(_CombinedMatcher):
Expand All @@ -226,7 +228,8 @@ class ContainedByDict(_CombinedMatcher):
'Differences': _MatchCommonKeys,
}

format_expected = lambda self, expected: _format_matcher_dict(expected)
def format_expected(self, expected):
return _format_matcher_dict(expected)


class KeysEqual(Matcher):
Expand Down
2 changes: 1 addition & 1 deletion testtools/matchers/_exception.py
Expand Up @@ -98,7 +98,7 @@ def match(self, matchee):
return Mismatch(f'{matchee!r} returned {result!r}')
# Catch all exceptions: Raises() should be able to match a
# KeyboardInterrupt or SystemExit.
except:
except BaseException:
exc_info = sys.exc_info()
if self.exception_matcher:
mismatch = self.exception_matcher.match(exc_info)
Expand Down
2 changes: 1 addition & 1 deletion testtools/runtest.py
Expand Up @@ -191,7 +191,7 @@ def _run_user(self, fn, *args, **kwargs):
"""
try:
return fn(*args, **kwargs)
except:
except BaseException:
return self._got_user_exception(sys.exc_info())

def _got_user_exception(self, exc_info, tb_label='traceback'):
Expand Down
4 changes: 2 additions & 2 deletions testtools/testcase.py
Expand Up @@ -737,7 +737,7 @@ def useFixture(self, fixture):
e.args[-1][0] is fixtures.fixture.SetupError):
gather_details(e.args[-1][1].args[0], self.getDetails())
raise
except:
except BaseException:
exc_info = sys.exc_info()
try:
# fixture._details is not available if using the newer
Expand All @@ -750,7 +750,7 @@ def useFixture(self, fixture):
fixture._details is not None
):
gather_details(fixture.getDetails(), self.getDetails())
except:
except BaseException:
# Report the setUp exception, then raise the error during
# gather_details.
self._report_traceback(exc_info)
Expand Down
3 changes: 2 additions & 1 deletion testtools/tests/matchers/test_basic.py
Expand Up @@ -157,7 +157,8 @@ class TestIsInterface(TestCase, TestMatchersInterface):

class TestIsInstanceInterface(TestCase, TestMatchersInterface):

class Foo:pass
class Foo:
pass

matches_matcher = IsInstance(Foo)
matches_matches = [Foo()]
Expand Down
5 changes: 2 additions & 3 deletions testtools/tests/test_run.py
Expand Up @@ -146,7 +146,7 @@ def list(self, test):
in testtools.testsuite.iterate_tests(test)})
out = io.StringIO()
try:
program = run.TestProgram(
run.TestProgram(
argv=['prog', '-l', 'testtools.runexample.test_suite'],
stdout=out, testRunner=CaptureList)
except SystemExit:
Expand Down Expand Up @@ -310,7 +310,7 @@ def test_run_locals(self):

class Failing(TestCase):
def test_a(self):
a = 1
a = 1 # noqa: F841
self.fail('a')
runner = run.TestToolsTestRunner(tb_locals=True, stdout=stdout.stream)
runner.run(Failing('test_a'))
Expand All @@ -319,7 +319,6 @@ def test_a(self):

def test_stdout_honoured(self):
self.useFixture(SampleTestFixture())
tests = []
out = io.StringIO()
exc = self.assertRaises(SystemExit, run.main,
argv=['prog', 'testtools.runexample.test_suite'],
Expand Down
8 changes: 4 additions & 4 deletions testtools/tests/test_testcase.py
Expand Up @@ -205,7 +205,7 @@ class TestErrorHolder(TestCase):
def makeException(self):
try:
raise RuntimeError("danger danger")
except:
except BaseException:
return sys.exc_info()

def makePlaceHolder(self, test_id="foo", error=None,
Expand Down Expand Up @@ -1909,12 +1909,12 @@ def foo():
def test_called_with_arguments(self):
# The function is called with the arguments given to Nullary's
# constructor.
l = []
line = []
def foo(*args, **kwargs):
l.append((args, kwargs))
line.append((args, kwargs))
wrapped = Nullary(foo, 1, 2, a="b")
wrapped()
self.assertEqual(l, [((1, 2), {'a': 'b'})])
self.assertEqual(line, [((1, 2), {'a': 'b'})])

def test_returns_wrapped(self):
# Calling Nullary returns whatever the function returns.
Expand Down
17 changes: 10 additions & 7 deletions testtools/tests/test_testresult.py
Expand Up @@ -93,7 +93,7 @@
def make_erroring_test():
class Test(TestCase):
def error(self):
a = 1
a = 1 # noqa: F841
1/0
return Test("error")

Expand Down Expand Up @@ -129,7 +129,7 @@ def test(self):
def make_exception_info(exceptionFactory, *args, **kwargs):
try:
raise exceptionFactory(*args, **kwargs)
except:
except BaseException:
return sys.exc_info()


Expand Down Expand Up @@ -2092,9 +2092,12 @@ def check_outcome_details(self, outcome):

def get_details_and_string(self):
"""Get a details dict and expected string."""
text1 = lambda: [_b("1\n2\n")]
text2 = lambda: [_b("3\n4\n")]
bin1 = lambda: [_b("5\n")]
def text1():
return [_b('1\n2\n')]
def text2():
return [_b('3\n4\n')]
def bin1():
return [_b('5\n')]
details = {'text 1': Content(ContentType('text', 'plain'), text1),
'text 2': Content(ContentType('text', 'strange'), text2),
'bin 1': Content(ContentType('application', 'binary'), bin1)}
Expand Down Expand Up @@ -2828,8 +2831,8 @@ def test_empty_attachment(self):
"""))

def test_lots_of_different_attachments(self):
jpg = lambda x: content_from_stream(
io.StringIO(x), ContentType('image', 'jpeg'))
def jpg(x):
return content_from_stream(io.StringIO(x), ContentType('image', 'jpeg'))
attachments = {
'attachment': text_content('foo'),
'attachment-1': text_content('traceback'),
Expand Down
2 changes: 1 addition & 1 deletion testtools/tests/twistedsupport/test_matchers.py
Expand Up @@ -41,7 +41,7 @@ def make_failure(exc_value):
"""Raise ``exc_value`` and return the failure."""
try:
raise exc_value
except:
except BaseException:
return Failure()


Expand Down

0 comments on commit 8dc8116

Please sign in to comment.