From e34530c6ff0a6ec9b111ecc95dd6c2cd61221654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jelmer=20Vernoo=C4=B3?= Date: Fri, 24 Nov 2023 20:23:03 +0000 Subject: [PATCH 1/2] Make testtools ruff-clean --- testtools/__init__.py | 4 +--- testtools/compat.py | 4 ++-- testtools/matchers/_datastructures.py | 16 +++++++++------- testtools/matchers/_dict.py | 9 ++++++--- testtools/matchers/_exception.py | 2 +- testtools/runtest.py | 2 +- testtools/testcase.py | 4 ++-- testtools/tests/matchers/test_basic.py | 3 ++- testtools/tests/test_run.py | 5 ++--- testtools/tests/test_testcase.py | 8 ++++---- testtools/tests/test_testresult.py | 17 ++++++++++------- testtools/tests/twistedsupport/test_matchers.py | 2 +- 12 files changed, 41 insertions(+), 35 deletions(-) diff --git a/testtools/__init__.py b/testtools/__init__.py index 00b1920e..91fb83b7 100644 --- a/testtools/__init__.py +++ b/testtools/__init__.py @@ -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, diff --git a/testtools/compat.py b/testtools/compat.py index a9556e6e..6562317d 100644 --- a/testtools/compat.py +++ b/testtools/compat.py @@ -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, diff --git a/testtools/matchers/_datastructures.py b/testtools/matchers/_datastructures.py index 9a7f84fe..dcb640db 100644 --- a/testtools/matchers/_datastructures.py +++ b/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 @@ -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 diff --git a/testtools/matchers/_dict.py b/testtools/matchers/_dict.py index 29ec1041..953f4c8f 100644 --- a/testtools/matchers/_dict.py +++ b/testtools/matchers/_dict.py @@ -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): @@ -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): @@ -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): diff --git a/testtools/matchers/_exception.py b/testtools/matchers/_exception.py index 66aaa906..fe2c7669 100644 --- a/testtools/matchers/_exception.py +++ b/testtools/matchers/_exception.py @@ -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) diff --git a/testtools/runtest.py b/testtools/runtest.py index 4f0e894b..db95c884 100644 --- a/testtools/runtest.py +++ b/testtools/runtest.py @@ -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'): diff --git a/testtools/testcase.py b/testtools/testcase.py index a9b017bb..faa0672d 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -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 @@ -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) diff --git a/testtools/tests/matchers/test_basic.py b/testtools/tests/matchers/test_basic.py index bfda8fab..86ac7be5 100644 --- a/testtools/tests/matchers/test_basic.py +++ b/testtools/tests/matchers/test_basic.py @@ -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()] diff --git a/testtools/tests/test_run.py b/testtools/tests/test_run.py index 9808421b..cd89d5b4 100644 --- a/testtools/tests/test_run.py +++ b/testtools/tests/test_run.py @@ -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: @@ -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')) @@ -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'], diff --git a/testtools/tests/test_testcase.py b/testtools/tests/test_testcase.py index 20799115..69c5213d 100644 --- a/testtools/tests/test_testcase.py +++ b/testtools/tests/test_testcase.py @@ -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, @@ -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. diff --git a/testtools/tests/test_testresult.py b/testtools/tests/test_testresult.py index 015972cc..59fabf4a 100644 --- a/testtools/tests/test_testresult.py +++ b/testtools/tests/test_testresult.py @@ -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") @@ -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() @@ -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)} @@ -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'), diff --git a/testtools/tests/twistedsupport/test_matchers.py b/testtools/tests/twistedsupport/test_matchers.py index 31475033..ba26e329 100644 --- a/testtools/tests/twistedsupport/test_matchers.py +++ b/testtools/tests/twistedsupport/test_matchers.py @@ -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() From 0dfade56aa7930f8b2e26f2f2b52d9a66fc27b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jelmer=20Vernoo=C4=B3?= Date: Fri, 24 Nov 2023 20:24:02 +0000 Subject: [PATCH 2/2] Run ruff in ci. Fixes #157 --- .github/workflows/test.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d80333df..4c6b788a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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