Skip to content

Commit e92fc7d

Browse files
authored
Merge pull request #556 from testing-cabal/typing-compat
Add more typing, reduce size of testtools.compat
2 parents b9cb288 + 4d2a95c commit e92fc7d

File tree

16 files changed

+418
-323
lines changed

16 files changed

+418
-323
lines changed

pyproject.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,23 @@ module = [
7575
]
7676
ignore_missing_imports = true
7777

78+
[[tool.mypy.overrides]]
79+
module = "fixtures.*"
80+
ignore_missing_imports = true
81+
follow_imports = "skip"
82+
7883
[[tool.mypy.overrides]]
7984
module = [
8085
# FIXME(stephenfin): We would like to remove all modules from this list
8186
# except tests (we're not sadists)
8287
"testtools.assertions",
8388
"testtools.compat",
84-
"testtools.content",
85-
"testtools.content_type",
8689
"testtools.matchers.*",
8790
"testtools.monkey",
8891
"testtools.run",
8992
"testtools.runtest",
9093
"testtools.testcase",
9194
"testtools.testresult.*",
92-
"testtools.testsuite",
9395
"testtools.twistedsupport.*",
9496
"tests.*",
9597
]

tests/matchers/test_basic.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
from typing import ClassVar
55

66
from testtools import TestCase
7-
from testtools.compat import (
8-
_b,
9-
text_repr,
10-
)
7+
from testtools.compat import text_repr
118
from testtools.matchers._basic import (
129
Contains,
1310
DoesNotEndWith,
@@ -36,7 +33,7 @@ class Test_BinaryMismatch(TestCase):
3633
"""Mismatches from binary comparisons need useful describe output"""
3734

3835
_long_string = "This is a longish multiline non-ascii string\n\xa7"
39-
_long_b = _b(_long_string)
36+
_long_b = _long_string.encode("utf-8")
4037
_long_u = _long_string
4138

4239
class CustomRepr:
@@ -52,12 +49,12 @@ def test_short_objects(self):
5249
self.assertEqual(mismatch.describe(), f"{o1!r} !~ {o2!r}")
5350

5451
def test_short_mixed_strings(self):
55-
b, u = _b("\xa7"), "\xa7"
52+
b, u = b"\xa7", "\xa7"
5653
mismatch = _BinaryMismatch(b, "!~", u)
5754
self.assertEqual(mismatch.describe(), f"{b!r} !~ {u!r}")
5855

5956
def test_long_bytes(self):
60-
one_line_b = self._long_b.replace(_b("\n"), _b(" "))
57+
one_line_b = self._long_b.replace(b"\n", b" ")
6158
mismatch = _BinaryMismatch(one_line_b, "!~", self._long_b)
6259
self.assertEqual(
6360
mismatch.describe(),
@@ -249,8 +246,8 @@ def test_describe_non_ascii_unicode(self):
249246
)
250247

251248
def test_describe_non_ascii_bytes(self):
252-
string = _b("A\xa7")
253-
suffix = _b("B\xa7")
249+
string = b"A\xa7"
250+
suffix = b"B\xa7"
254251
mismatch = DoesNotStartWith(string, suffix)
255252
self.assertEqual(
256253
f"{string!r} does not start with {suffix!r}.", mismatch.describe()
@@ -265,7 +262,7 @@ def test_str(self):
265262
self.assertEqual("StartsWith('bar')", str(matcher))
266263

267264
def test_str_with_bytes(self):
268-
b = _b("\xa7")
265+
b = b"\xa7"
269266
matcher = StartsWith(b)
270267
self.assertEqual(f"StartsWith({b!r})", str(matcher))
271268

@@ -310,8 +307,8 @@ def test_describe_non_ascii_unicode(self):
310307
)
311308

312309
def test_describe_non_ascii_bytes(self):
313-
string = _b("A\xa7")
314-
suffix = _b("B\xa7")
310+
string = b"A\xa7"
311+
suffix = b"B\xa7"
315312
mismatch = DoesNotEndWith(string, suffix)
316313
self.assertEqual(
317314
f"{string!r} does not end with {suffix!r}.", mismatch.describe()
@@ -326,7 +323,7 @@ def test_str(self):
326323
self.assertEqual("EndsWith('bar')", str(matcher))
327324

328325
def test_str_with_bytes(self):
329-
b = _b("\xa7")
326+
b = b"\xa7"
330327
matcher = EndsWith(b)
331328
self.assertEqual(f"EndsWith({b!r})", str(matcher))
332329

@@ -416,17 +413,17 @@ class TestMatchesRegex(TestCase, TestMatchersInterface):
416413
("MatchesRegex('a|b')", MatchesRegex("a|b")),
417414
("MatchesRegex('a|b', re.M)", MatchesRegex("a|b", re.M)),
418415
("MatchesRegex('a|b', re.I|re.M)", MatchesRegex("a|b", re.I | re.M)),
419-
("MatchesRegex({!r})".format(_b("\xa7")), MatchesRegex(_b("\xa7"))),
416+
("MatchesRegex({!r})".format(b"\xa7"), MatchesRegex(b"\xa7")),
420417
("MatchesRegex({!r})".format("\xa7"), MatchesRegex("\xa7")),
421418
]
422419

423420
describe_examples: ClassVar = [
424421
("'c' does not match /a|b/", "c", MatchesRegex("a|b")),
425422
("'c' does not match /a\\d/", "c", MatchesRegex(r"a\d")),
426423
(
427-
"{!r} does not match /\\s+\\xa7/".format(_b("c")),
428-
_b("c"),
429-
MatchesRegex(_b("\\s+\xa7")),
424+
"{!r} does not match /\\s+\\xa7/".format(b"c"),
425+
b"c",
426+
MatchesRegex(b"\\s+\xa7"),
430427
),
431428
("{!r} does not match /\\s+\\xa7/".format("c"), "c", MatchesRegex("\\s+\xa7")),
432429
]

tests/matchers/test_doctest.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
from typing import ClassVar
55

66
from testtools import TestCase
7-
from testtools.compat import (
8-
_b,
9-
)
107
from testtools.matchers._doctest import DocTestMatches
118

129
from ..helpers import FullStackRunTest
@@ -75,7 +72,7 @@ def test_describe_non_ascii_bytes(self):
7572
permits arbitrary binary inputs. This is a slightly bogus thing to do,
7673
and under Python 3 using bytes objects will reasonably raise an error.
7774
"""
78-
header = _b("\x89PNG\r\n\x1a\n...")
75+
header = b"\x89PNG\r\n\x1a\n..."
7976
self.assertRaises(TypeError, DocTestMatches, header, doctest.ELLIPSIS)
8077

8178

tests/test_compat.py

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
import ast
66
import io
77
import sys
8-
import traceback
98

109
import testtools
1110
from testtools.compat import (
12-
_b,
13-
reraise,
1411
text_repr,
1512
unicode_output_stream,
1613
)
@@ -45,28 +42,28 @@ def test_no_encoding_becomes_ascii(self):
4542
"""A stream with no encoding attribute gets ascii/replace strings"""
4643
sout = _FakeOutputStream()
4744
unicode_output_stream(sout).write(self.uni)
48-
self.assertEqual([_b("pa???n")], sout.writelog)
45+
self.assertEqual([b"pa???n"], sout.writelog)
4946

5047
def test_encoding_as_none_becomes_ascii(self):
5148
"""A stream with encoding value of None gets ascii/replace strings"""
5249
sout = _FakeOutputStream()
5350
sout.encoding = None
5451
unicode_output_stream(sout).write(self.uni)
55-
self.assertEqual([_b("pa???n")], sout.writelog)
52+
self.assertEqual([b"pa???n"], sout.writelog)
5653

5754
def test_bogus_encoding_becomes_ascii(self):
5855
"""A stream with a bogus encoding gets ascii/replace strings"""
5956
sout = _FakeOutputStream()
6057
sout.encoding = "bogus"
6158
unicode_output_stream(sout).write(self.uni)
62-
self.assertEqual([_b("pa???n")], sout.writelog)
59+
self.assertEqual([b"pa???n"], sout.writelog)
6360

6461
def test_partial_encoding_replace(self):
6562
"""A string which can be partly encoded correctly should be"""
6663
sout = _FakeOutputStream()
6764
sout.encoding = "iso-8859-7"
6865
unicode_output_stream(sout).write(self.uni)
69-
self.assertEqual([_b("pa?\xe8?n")], sout.writelog)
66+
self.assertEqual([b"pa?\xe8?n"], sout.writelog)
7067

7168
def test_stringio(self):
7269
"""A StringIO object should maybe get an ascii native str type"""
@@ -126,11 +123,11 @@ class TestTextRepr(testtools.TestCase):
126123

127124
# Bytes with the high bit set should always be escaped
128125
bytes_examples = (
129-
(_b("\x80"), "'\\x80'", "'''\\\n\\x80'''"),
130-
(_b("\xa0"), "'\\xa0'", "'''\\\n\\xa0'''"),
131-
(_b("\xc0"), "'\\xc0'", "'''\\\n\\xc0'''"),
132-
(_b("\xff"), "'\\xff'", "'''\\\n\\xff'''"),
133-
(_b("\xc2\xa7"), "'\\xc2\\xa7'", "'''\\\n\\xc2\\xa7'''"),
126+
(b"\x80", "'\\x80'", "'''\\\n\\x80'''"),
127+
(b"\xa0", "'\\xa0'", "'''\\\n\\xa0'''"),
128+
(b"\xc0", "'\\xc0'", "'''\\\n\\xc0'''"),
129+
(b"\xff", "'\\xff'", "'''\\\n\\xff'''"),
130+
(b"\xc2\xa7", "'\\xc2\\xa7'", "'''\\\n\\xc2\\xa7'''"),
134131
)
135132

136133
# Unicode doesn't escape printable characters as per the Python 3 model
@@ -153,12 +150,12 @@ class TestTextRepr(testtools.TestCase):
153150
# Unprintable general categories not fully tested: Cc, Cf, Co, Cn, Zs
154151
)
155152

156-
b_prefix = repr(_b(""))[:-2]
153+
b_prefix = repr(b"")[:-2]
157154
u_prefix = repr("")[:-2]
158155

159156
def test_ascii_examples_oneline_bytes(self):
160157
for s, expected, _ in self.ascii_examples:
161-
b = _b(s)
158+
b = s.encode("utf-8")
162159
actual = text_repr(b, multiline=False)
163160
# Add self.assertIsInstance check?
164161
self.assertEqual(actual, self.b_prefix + expected)
@@ -173,7 +170,7 @@ def test_ascii_examples_oneline_unicode(self):
173170

174171
def test_ascii_examples_multiline_bytes(self):
175172
for s, _, expected in self.ascii_examples:
176-
b = _b(s)
173+
b = s.encode("utf-8")
177174
actual = text_repr(b, multiline=True)
178175
self.assertEqual(actual, self.b_prefix + expected)
179176
self.assertEqual(ast.literal_eval(actual), b)
@@ -187,7 +184,7 @@ def test_ascii_examples_multiline_unicode(self):
187184
def test_ascii_examples_defaultline_bytes(self):
188185
for s, one, multi in self.ascii_examples:
189186
expected = ("\n" in s and multi) or one
190-
self.assertEqual(text_repr(_b(s)), self.b_prefix + expected)
187+
self.assertEqual(text_repr(s.encode("utf-8")), self.b_prefix + expected)
191188

192189
def test_ascii_examples_defaultline_unicode(self):
193190
for s, one, multi in self.ascii_examples:
@@ -219,43 +216,6 @@ def test_unicode_examples_multiline(self):
219216
self.assertEqual(ast.literal_eval(actual), u)
220217

221218

222-
class TestReraise(testtools.TestCase):
223-
"""Tests for trivial reraise wrapper needed for Python 2/3 changes"""
224-
225-
def test_exc_info(self):
226-
"""After reraise exc_info matches plus some extra traceback"""
227-
try:
228-
raise ValueError("Bad value")
229-
except ValueError:
230-
_exc_info = sys.exc_info()
231-
try:
232-
reraise(*_exc_info)
233-
except ValueError:
234-
_new_exc_info = sys.exc_info()
235-
self.assertIs(_exc_info[0], _new_exc_info[0])
236-
self.assertIs(_exc_info[1], _new_exc_info[1])
237-
expected_tb = traceback.extract_tb(_exc_info[2])
238-
self.assertEqual(
239-
expected_tb, traceback.extract_tb(_new_exc_info[2])[-len(expected_tb) :]
240-
)
241-
242-
def test_custom_exception_no_args(self):
243-
"""Reraising does not require args attribute to contain params"""
244-
245-
class CustomException(Exception):
246-
"""Exception that expects and sets attrs but not args"""
247-
248-
def __init__(self, value):
249-
Exception.__init__(self)
250-
self.value = value
251-
252-
try:
253-
raise CustomException("Some value")
254-
except CustomException:
255-
_exc_info = sys.exc_info()
256-
self.assertRaises(CustomException, reraise, *_exc_info)
257-
258-
259219
def test_suite():
260220
from unittest import TestLoader
261221

0 commit comments

Comments
 (0)