Skip to content

Commit c0d384c

Browse files
committed
Add missing ClassVar types
We also remove MatcherTestProtocol since it is overly complicated and not really needed. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent ebc2112 commit c0d384c

File tree

2 files changed

+50
-51
lines changed

2 files changed

+50
-51
lines changed

tests/matchers/test_basic.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
22

33
import re
4-
from typing import ClassVar
4+
from typing import Any, ClassVar
55

66
from testtools import TestCase
77
from testtools.compat import text_repr
@@ -24,6 +24,7 @@
2424
_BinaryMismatch,
2525
_NotNearlyEqual,
2626
)
27+
from testtools.matchers._impl import Matcher
2728
from testtools.matchers.test import TestMatchersInterface
2829

2930
from ..helpers import FullStackRunTest
@@ -114,9 +115,9 @@ def test_long_unicode_and_object(self):
114115

115116

116117
class TestEqualsInterface(TestCase, TestMatchersInterface):
117-
matches_matcher: ClassVar = Equals(1)
118-
matches_matches: ClassVar = [1]
119-
matches_mismatches: ClassVar = [2]
118+
matches_matcher: ClassVar[Matcher[Any]] = Equals(1)
119+
matches_matches: ClassVar[list[Any]] = [1]
120+
matches_mismatches: ClassVar[list[Any]] = [2]
120121

121122
str_examples: ClassVar = [
122123
("Equals(1)", Equals(1)),
@@ -138,9 +139,9 @@ class TestEqualsInterface(TestCase, TestMatchersInterface):
138139

139140

140141
class TestNotEqualsInterface(TestCase, TestMatchersInterface):
141-
matches_matcher: ClassVar = NotEquals(1)
142-
matches_matches: ClassVar = [2]
143-
matches_mismatches: ClassVar = [1]
142+
matches_matcher: ClassVar[Matcher[Any]] = NotEquals(1)
143+
matches_matches: ClassVar[list[Any]] = [2]
144+
matches_mismatches: ClassVar[list[Any]] = [1]
144145

145146
str_examples: ClassVar = [
146147
("NotEquals(1)", NotEquals(1)),
@@ -154,9 +155,9 @@ class TestIsInterface(TestCase, TestMatchersInterface):
154155
foo = object()
155156
bar = object()
156157

157-
matches_matcher: ClassVar = Is(foo)
158-
matches_matches: ClassVar = [foo]
159-
matches_mismatches: ClassVar = [bar, 1]
158+
matches_matcher: ClassVar[Matcher[Any]] = Is(foo)
159+
matches_matches: ClassVar[list[Any]] = [foo]
160+
matches_mismatches: ClassVar[list[Any]] = [bar, 1]
160161

161162
str_examples: ClassVar = [("Is(2)", Is(2))]
162163

@@ -167,9 +168,9 @@ class TestIsInstanceInterface(TestCase, TestMatchersInterface):
167168
class Foo:
168169
pass
169170

170-
matches_matcher: ClassVar = IsInstance(Foo)
171-
matches_matches: ClassVar = [Foo()]
172-
matches_mismatches: ClassVar = [object(), 1, Foo]
171+
matches_matcher: ClassVar[Matcher[Any]] = IsInstance(Foo)
172+
matches_matches: ClassVar[list[Any]] = [Foo()]
173+
matches_mismatches: ClassVar[list[Any]] = [object(), 1, Foo]
173174

174175
str_examples: ClassVar = [
175176
("IsInstance(str)", IsInstance(str)),
@@ -187,9 +188,9 @@ class Foo:
187188

188189

189190
class TestLessThanInterface(TestCase, TestMatchersInterface):
190-
matches_matcher: ClassVar = LessThan(4)
191-
matches_matches: ClassVar = [-5, 3]
192-
matches_mismatches: ClassVar = [4, 5, 5000]
191+
matches_matcher: ClassVar[Matcher[Any]] = LessThan(4)
192+
matches_matches: ClassVar[list[Any]] = [-5, 3]
193+
matches_mismatches: ClassVar[list[Any]] = [4, 5, 5000]
193194

194195
str_examples: ClassVar = [
195196
("LessThan(12)", LessThan(12)),
@@ -202,9 +203,9 @@ class TestLessThanInterface(TestCase, TestMatchersInterface):
202203

203204

204205
class TestGreaterThanInterface(TestCase, TestMatchersInterface):
205-
matches_matcher: ClassVar = GreaterThan(4)
206-
matches_matches: ClassVar = [5, 8]
207-
matches_mismatches: ClassVar = [-2, 0, 4]
206+
matches_matcher: ClassVar[Matcher[Any]] = GreaterThan(4)
207+
matches_matches: ClassVar[list[Any]] = [5, 8]
208+
matches_mismatches: ClassVar[list[Any]] = [-2, 0, 4]
208209

209210
str_examples: ClassVar = [
210211
("GreaterThan(12)", GreaterThan(12)),
@@ -217,9 +218,9 @@ class TestGreaterThanInterface(TestCase, TestMatchersInterface):
217218

218219

219220
class TestContainsInterface(TestCase, TestMatchersInterface):
220-
matches_matcher: ClassVar = Contains("foo")
221-
matches_matches: ClassVar = ["foo", "afoo", "fooa"]
222-
matches_mismatches: ClassVar = ["f", "fo", "oo", "faoo", "foao"]
221+
matches_matcher: ClassVar[Matcher[Any]] = Contains("foo")
222+
matches_matches: ClassVar[list[Any]] = ["foo", "afoo", "fooa"]
223+
matches_mismatches: ClassVar[list[Any]] = ["f", "fo", "oo", "faoo", "foao"]
223224

224225
str_examples: ClassVar = [
225226
("Contains(1)", Contains(1)),
@@ -352,14 +353,14 @@ def test_mismatch_sets_expected(self):
352353

353354

354355
class TestSameMembers(TestCase, TestMatchersInterface):
355-
matches_matcher: ClassVar = SameMembers([1, 1, 2, 3, {"foo": "bar"}])
356-
matches_matches: ClassVar = [
356+
matches_matcher: ClassVar[Matcher[Any]] = SameMembers([1, 1, 2, 3, {"foo": "bar"}])
357+
matches_matches: ClassVar[list[Any]] = [
357358
[1, 1, 2, 3, {"foo": "bar"}],
358359
[3, {"foo": "bar"}, 1, 2, 1],
359360
[3, 2, 1, {"foo": "bar"}, 1],
360361
(2, {"foo": "bar"}, 3, 1, 1),
361362
]
362-
matches_mismatches: ClassVar = [
363+
matches_mismatches: ClassVar[list[Any]] = [
363364
{1, 2, 3},
364365
[1, 1, 2, 3, 5],
365366
[1, 2, 3, {"foo": "bar"}],
@@ -405,9 +406,9 @@ class TestSameMembers(TestCase, TestMatchersInterface):
405406

406407

407408
class TestMatchesRegex(TestCase, TestMatchersInterface):
408-
matches_matcher: ClassVar = MatchesRegex("a|b")
409-
matches_matches: ClassVar = ["a", "b"]
410-
matches_mismatches: ClassVar = ["c"]
409+
matches_matcher: ClassVar[Matcher[Any]] = MatchesRegex("a|b")
410+
matches_matches: ClassVar[list[Any]] = ["a", "b"]
411+
matches_mismatches: ClassVar[list[Any]] = ["c"]
411412

412413
str_examples: ClassVar = [
413414
("MatchesRegex('a|b')", MatchesRegex("a|b")),
@@ -430,9 +431,9 @@ class TestMatchesRegex(TestCase, TestMatchersInterface):
430431

431432

432433
class TestHasLength(TestCase, TestMatchersInterface):
433-
matches_matcher: ClassVar = HasLength(2)
434-
matches_matches: ClassVar = [[1, 2]]
435-
matches_mismatches: ClassVar = [[], [1], [3, 2, 1]]
434+
matches_matcher: ClassVar[Matcher[Any]] = HasLength(2)
435+
matches_matches: ClassVar[list[Any]] = [[1, 2]]
436+
matches_mismatches: ClassVar[list[Any]] = [[], [1], [3, 2, 1]]
436437

437438
str_examples: ClassVar = [
438439
("HasLength(2)", HasLength(2)),
@@ -444,9 +445,9 @@ class TestHasLength(TestCase, TestMatchersInterface):
444445

445446

446447
class TestNearlyInterface(TestCase, TestMatchersInterface):
447-
matches_matcher: ClassVar = Nearly(4.0, delta=0.5)
448-
matches_matches: ClassVar = [4.0, 4.5, 3.5, 4.25, 3.75]
449-
matches_mismatches: ClassVar = [4.51, 3.49, 5.0, 2.0, "not a number"]
448+
matches_matcher: ClassVar[Matcher[Any]] = Nearly(4.0, delta=0.5)
449+
matches_matches: ClassVar[list[Any]] = [4.0, 4.5, 3.5, 4.25, 3.75]
450+
matches_mismatches: ClassVar[list[Any]] = [4.51, 3.49, 5.0, 2.0, "not a number"]
450451

451452
str_examples: ClassVar = [
452453
("Nearly(4.0, delta=0.5)", Nearly(4.0, delta=0.5)),

testtools/matchers/test.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
# Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
22

33
from collections.abc import Callable
4-
from typing import Any, ClassVar, Protocol, runtime_checkable
4+
from typing import Any, ClassVar
55

6+
from testtools.matchers._impl import Matcher
67

7-
@runtime_checkable
8-
class MatcherTestProtocol(Protocol):
9-
"""Protocol for test classes that test matchers."""
108

11-
matches_matcher: ClassVar[Any]
12-
matches_matches: ClassVar[Any]
13-
matches_mismatches: ClassVar[Any]
9+
class TestMatchersInterface:
10+
"""Mixin class that provides test methods for matcher interfaces."""
11+
12+
__test__ = False # Tell pytest not to collect this as a test class
13+
14+
matches_matcher: ClassVar[Matcher[Any]]
15+
matches_matches: ClassVar[list[Any]]
16+
matches_mismatches: ClassVar[list[Any]]
1417
str_examples: ClassVar[Any]
1518
describe_examples: ClassVar[Any]
19+
1620
assertEqual: Callable[..., Any]
1721
assertNotEqual: Callable[..., Any]
1822
assertThat: Callable[..., Any]
1923

20-
21-
class TestMatchersInterface:
22-
"""Mixin class that provides test methods for matcher interfaces."""
23-
24-
__test__ = False # Tell pytest not to collect this as a test class
25-
26-
def test_matches_match(self: MatcherTestProtocol) -> None:
24+
def test_matches_match(self) -> None:
2725
matcher = self.matches_matcher
2826
matches = self.matches_matches
2927
mismatches = self.matches_mismatches
@@ -34,22 +32,22 @@ def test_matches_match(self: MatcherTestProtocol) -> None:
3432
self.assertNotEqual(None, mismatch)
3533
self.assertNotEqual(None, getattr(mismatch, "describe", None))
3634

37-
def test__str__(self: MatcherTestProtocol) -> None:
35+
def test__str__(self) -> None:
3836
# [(expected, object to __str__)].
3937
from testtools.matchers._doctest import DocTestMatches
4038

4139
examples = self.str_examples
4240
for expected, matcher in examples:
4341
self.assertThat(matcher, DocTestMatches(expected))
4442

45-
def test_describe_difference(self: MatcherTestProtocol) -> None:
43+
def test_describe_difference(self) -> None:
4644
# [(expected, matchee, matcher), ...]
4745
examples = self.describe_examples
4846
for difference, matchee, matcher in examples:
4947
mismatch = matcher.match(matchee)
5048
self.assertEqual(difference, mismatch.describe())
5149

52-
def test_mismatch_details(self: MatcherTestProtocol) -> None:
50+
def test_mismatch_details(self) -> None:
5351
# The mismatch object must provide get_details, which must return a
5452
# dictionary mapping names to Content objects.
5553
examples = self.describe_examples

0 commit comments

Comments
 (0)