Skip to content

Commit efa9317

Browse files
committed
Enable more ruff rules
This aligns us with testtools. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent d8948ff commit efa9317

10 files changed

Lines changed: 49 additions & 38 deletions

doc/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def make(self, dependency_resources):
2828
return "You need to implement your own getResource."
2929

3030

31-
class MyResource(object):
31+
class MyResource:
3232
"""My pet resource."""
3333

3434

pyproject.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,11 @@ packages = ["testresources"]
7373
exclude = ["testresources/_version.py"]
7474

7575
[tool.ruff.lint]
76-
select = ["E4", "E5", "E7", "E9", "F"]
76+
select = ["E", "F", "I", "PIE", "UP", "RSE", "RUF"]
77+
ignore = [
78+
# we make use of resources as a class var to initialize things
79+
"RUF012"
80+
]
81+
82+
[tool.ruff.lint.per-file-ignores]
83+
"testresources/tests/*" = ["S"]

testresources/__init__.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import heapq
2121
import inspect
2222
import unittest
23-
2423
from collections.abc import MutableSet
2524

2625
# same format as sys.version_info: "A tuple containing the five components of
@@ -138,7 +137,7 @@ def _kruskals_graph_MST(graph):
138137
edges = set()
139138
for from_node, to_nodes in graph.items():
140139
for to_node, value in to_nodes.items():
141-
edge = (value,) + tuple(sorted([from_node, to_node]))
140+
edge = (value, *tuple(sorted([from_node, to_node])))
142141
edges.add(edge)
143142
edges = list(edges)
144143
heapq.heapify(edges)
@@ -529,7 +528,7 @@ class TestLoader(unittest.TestLoader):
529528
suiteClass = OptimisingTestSuite
530529

531530

532-
class TestResourceManager(object):
531+
class TestResourceManager:
533532
"""A manager for resources that can be shared across tests.
534533
535534
ResourceManagers can report activity to a TestResult. The methods
@@ -797,7 +796,7 @@ def __init__(
797796
be embedded in the string returned by the id() method, to identify
798797
the generic resource. Defaults to '__name__'.
799798
"""
800-
super(GenericResource, self).__init__()
799+
super().__init__()
801800
self.resource_factory = resource_factory
802801
self.setup_method_name = setup_method_name
803802
self.teardown_method_name = teardown_method_name
@@ -819,9 +818,8 @@ def id(self):
819818
820819
:see: The `id_attribute_name` parameter.
821820
"""
822-
return "%s[%s]" % (
823-
super(GenericResource, self).id(),
824-
getattr(self.resource_factory, self.id_attribute_name),
821+
return (
822+
f"{super().id()}[{getattr(self.resource_factory, self.id_attribute_name)}]"
825823
)
826824

827825

@@ -851,7 +849,7 @@ def __init__(self, fixture):
851849
852850
:param fixture: The fixture to wrap.
853851
"""
854-
super(FixtureResource, self).__init__()
852+
super().__init__()
855853
self.fixture = fixture
856854

857855
def clean(self, resource):
@@ -866,7 +864,7 @@ def id(self):
866864
867865
The default is to call str(fixture) to get such information.
868866
"""
869-
return "%s[%s]" % (super(FixtureResource, self).id(), str(self.fixture))
867+
return f"{super().id()}[{self.fixture!s}]"
870868

871869
def _reset(self, resource, dependency_resources):
872870
self.fixture.reset()
@@ -896,15 +894,15 @@ class is neded, or you can use multiple inheritance and call into
896894
resources = []
897895

898896
def setUp(self):
899-
super(ResourcedTestCase, self).setUp()
897+
super().setUp()
900898
self.setUpResources()
901899

902900
def setUpResources(self):
903901
setUpResources(self, self.resources, _get_result())
904902

905903
def tearDown(self):
906904
self.tearDownResources()
907-
super(ResourcedTestCase, self).tearDown()
905+
super().tearDown()
908906

909907
def tearDownResources(self):
910908
tearDownResources(self, self.resources, _get_result())
@@ -946,7 +944,7 @@ def neededResources(resources):
946944
dependencies = neededResources(
947945
[dependency for name, dependency in resource.resources]
948946
)
949-
for resource in dependencies + [resource]:
947+
for resource in [*dependencies, resource]:
950948
if resource in seen:
951949
continue
952950
seen.add(resource)

testresources/tests/TestUtil.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def visitTests(suite, visitor):
5757
visitTests(test, visitor)
5858
else:
5959
print(
60-
"unvisitable non-unittest.TestCase element %r (%r)"
61-
% (test, test.__class__)
60+
f"unvisitable non-unittest.TestCase element {test!r} "
61+
f"({test.__class__!r})"
6262
)
6363

6464

@@ -80,7 +80,7 @@ class TestLoader(unittest.TestLoader):
8080
suiteClass = TestSuite
8181

8282

83-
class TestVisitor(object):
83+
class TestVisitor:
8484
"""A visitor for Tests"""
8585

8686
def visitSuite(self, aTestSuite):

testresources/tests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
def test_suite():
2424
import testresources.tests.test_optimising_test_suite
25+
import testresources.tests.test_resource_graph
2526
import testresources.tests.test_resourced_test_case
2627
import testresources.tests.test_test_loader
2728
import testresources.tests.test_test_resource
28-
import testresources.tests.test_resource_graph
2929

3030
result = TestUtil.TestSuite()
3131
result.addTest(testresources.tests.test_test_loader.test_suite())
@@ -36,7 +36,7 @@ def test_suite():
3636
return result
3737

3838

39-
class ResultWithoutResourceExtensions(object):
39+
class ResultWithoutResourceExtensions:
4040
"""A test fake which does not have resource extensions."""
4141

4242

testresources/tests/test_optimising_test_suite.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
# license.
1616
#
1717

18-
import testtools
1918
import random
19+
import unittest
20+
21+
import testtools
22+
2023
import testresources
2124
from testresources import split_by_resources
2225
from testresources.tests import ResultWithResourceExtensions
23-
import unittest
2426

2527

2628
def test_suite():
@@ -56,7 +58,7 @@ def clean(self, resource):
5658

5759
def make(self, dependency_resources):
5860
self.makes += 1
59-
resource = "boo %d" % self.makes
61+
resource = f"boo {self.makes}"
6062
self.calls.append(("make", resource))
6163
return resource
6264

@@ -84,7 +86,7 @@ def runTest(self):
8486
return test_case
8587

8688
def setUp(self):
87-
super(TestOptimisingTestSuite, self).setUp()
89+
super().setUp()
8890
self.optimising_suite = testresources.OptimisingTestSuite()
8991

9092
def testAddTest(self):
@@ -293,7 +295,7 @@ class Resource(testresources.TestResource):
293295
"""Dummy resource."""
294296

295297
def __init__(self, name):
296-
super(Resource, self).__init__()
298+
super().__init__()
297299
self.name = name
298300

299301
def make(self, dependency_resources):
@@ -399,7 +401,7 @@ class TestCostOfSwitching(testtools.TestCase):
399401
"""Tests for cost_of_switching."""
400402

401403
def setUp(self):
402-
super(TestCostOfSwitching, self).setUp()
404+
super().setUp()
403405
self.suite = testresources.OptimisingTestSuite()
404406

405407
def makeResource(self, setUpCost=1, tearDownCost=1):
@@ -486,7 +488,7 @@ def testTwoCasesInGraph(self):
486488

487489
class TestGraphStuff(testtools.TestCase):
488490
def setUp(self):
489-
super(TestGraphStuff, self).setUp()
491+
super().setUp()
490492

491493
class MockTest(unittest.TestCase):
492494
def __repr__(self):
@@ -583,7 +585,7 @@ def testBasicSortTests(self):
583585
[self.case1, self.case2, self.case3, self.case4],
584586
[self.case3, self.case2, self.case1, self.case4],
585587
],
586-
"failed with permutation %s" % (permutation,),
588+
f"failed with permutation {permutation}",
587589
)
588590

589591
def testGlobalMinimum(self):
@@ -667,7 +669,7 @@ def testSortingTwelveIndependentIsFast(self):
667669
# Add more sample tests
668670
cases = [self.case1, self.case2, self.case3, self.case4]
669671
for pos in range(5, 13):
670-
cases.append(testtools.clone_test_with_new_id(cases[0], "case%d" % pos))
672+
cases.append(testtools.clone_test_with_new_id(cases[0], f"case{pos}"))
671673
# We care that this is fast in this test, so we don't need to have
672674
# overlapping resource usage
673675
for case, manager in zip(cases, managers):
@@ -684,7 +686,7 @@ def testSortingTwelveOverlappingIsFast(self):
684686
# Add more sample tests
685687
cases = [self.case1, self.case2, self.case3, self.case4]
686688
for pos in range(5, 13):
687-
cases.append(testtools.clone_test_with_new_id(cases[0], "case%d" % pos))
689+
cases.append(testtools.clone_test_with_new_id(cases[0], f"case{pos}"))
688690
tempdir = testresources.TestResourceManager()
689691
# give all tests a tempdir, enough to provoke a single partition in
690692
# the current code.

testresources/tests/test_resource_graph.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""Test _resource_graph(resource_sets)."""
2020

2121
import testtools
22+
2223
import testresources
2324
from testresources import _resource_graph
2425

testresources/tests/test_resourced_test_case.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#
1717

1818
import unittest
19+
1920
import testtools
21+
2022
import testresources
2123
from testresources.tests import ResultWithResourceExtensions
2224

@@ -38,13 +40,13 @@ def make(self, dependency_resources):
3840
return self._resource
3941

4042

41-
class MockResourceInstance(object):
43+
class MockResourceInstance:
4244
"""A resource instance."""
4345

4446

4547
class TestResourcedTestCase(testtools.TestCase):
4648
def setUp(self):
47-
super(TestResourcedTestCase, self).setUp()
49+
super().setUp()
4850

4951
class Example(testresources.ResourcedTestCase):
5052
def test_example(self):
@@ -60,7 +62,7 @@ class OtherBaseCase(unittest.TestCase):
6062

6163
def setUp(self):
6264
self.setUpCalled = True
63-
super(OtherBaseCase, self).setUp()
65+
super().setUp()
6466

6567
class OurCase(testresources.ResourcedTestCase, OtherBaseCase):
6668
def runTest(self):
@@ -76,7 +78,7 @@ class OtherBaseCase(unittest.TestCase):
7678

7779
def tearDown(self):
7880
self.tearDownCalled = True
79-
super(OtherBaseCase, self).setUp()
81+
super().setUp()
8082

8183
class OurCase(testresources.ResourcedTestCase, OtherBaseCase):
8284
def runTest(self):

testresources/tests/test_test_loader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
#
1717

1818
import testtools
19-
from testresources import TestLoader, OptimisingTestSuite
19+
20+
from testresources import OptimisingTestSuite, TestLoader
2021
from testresources.tests import TestUtil
2122

2223

testresources/tests/test_test_resource.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import testresources
2222
from testresources.tests import (
23-
ResultWithResourceExtensions,
2423
ResultWithoutResourceExtensions,
24+
ResultWithResourceExtensions,
2525
)
2626

2727

@@ -48,7 +48,7 @@ def reset(self):
4848
self.calls.append("reset" + self.suffix)
4949

5050

51-
class MockResourceInstance(object):
51+
class MockResourceInstance:
5252
def __init__(self, name):
5353
self._name = name
5454

@@ -63,7 +63,7 @@ class MockResource(testresources.TestResourceManager):
6363
"""Mock resource that logs the number of make and clean calls."""
6464

6565
def __init__(self):
66-
super(MockResource, self).__init__()
66+
super().__init__()
6767
self.makes = 0
6868
self.cleans = 0
6969

@@ -79,7 +79,7 @@ class MockResettableResource(MockResource):
7979
"""Mock resource that logs the number of reset calls too."""
8080

8181
def __init__(self):
82-
super(MockResettableResource, self).__init__()
82+
super().__init__()
8383
self.resets = 0
8484

8585
def _reset(self, resource, dependency_resources):

0 commit comments

Comments
 (0)