Skip to content

Commit 31b06a8

Browse files
committed
Add more typing, reduce size of testtools.compat
1 parent 882f2fc commit 31b06a8

File tree

9 files changed

+193
-126
lines changed

9 files changed

+193
-126
lines changed

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ module = [
8181
# except tests (we're not sadists)
8282
"testtools.assertions",
8383
"testtools.compat",
84-
"testtools.content",
85-
"testtools.content_type",
8684
"testtools.matchers.*",
8785
"testtools.monkey",
8886
"testtools.run",

tests/test_fixturesupport.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
content,
88
content_type,
99
)
10-
from testtools.compat import _b
1110
from testtools.matchers import (
1211
Contains,
1312
Equals,
@@ -70,7 +69,7 @@ class DetailsFixture(fixtures.Fixture):
7069
def setUp(self):
7170
fixtures.Fixture.setUp(self)
7271
self.addCleanup(delattr, self, "content")
73-
self.content = [_b("content available until cleanUp")]
72+
self.content = [b"content available until cleanUp"]
7473
self.addDetail(
7574
"content", content.Content(content_type.UTF8_TEXT, self.get_content)
7675
)
@@ -86,7 +85,7 @@ def test_foo(self):
8685
# Add a colliding detail (both should show up)
8786
self.addDetail(
8887
"content",
89-
content.Content(content_type.UTF8_TEXT, lambda: [_b("foo")]),
88+
content.Content(content_type.UTF8_TEXT, lambda: [b"foo"]),
9089
)
9190

9291
result = ExtendedTestResult()

tests/test_run.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import testtools
1313
from testtools import TestCase, run, skipUnless
14-
from testtools.compat import _b
1514
from testtools.matchers import (
1615
Contains,
1716
DocTestMatches,
@@ -40,8 +39,7 @@ def __init__(self, broken=False):
4039
:param broken: If True, the sample file will not be importable.
4140
"""
4241
if not broken:
43-
init_contents = _b(
44-
"""\
42+
init_contents = b"""\
4543
from testtools import TestCase
4644
4745
class TestFoo(TestCase):
@@ -53,7 +51,6 @@ def test_suite():
5351
from unittest import TestLoader
5452
return TestLoader().loadTestsFromName(__name__)
5553
"""
56-
)
5754
else:
5855
init_contents = b"class not in\n"
5956
self.package = fixtures.PythonPackage(
@@ -80,8 +77,7 @@ def __init__(self):
8077
[
8178
(
8279
"__init__.py",
83-
_b(
84-
"""
80+
b"""
8581
from fixtures import Fixture
8682
from testresources import (
8783
FixtureResource,
@@ -112,8 +108,7 @@ def test_quux(self):
112108
def test_suite():
113109
from unittest import TestLoader
114110
return OptimisingTestSuite(TestLoader().loadTestsFromName(__name__))
115-
"""
116-
),
111+
""",
117112
)
118113
],
119114
)
@@ -137,8 +132,7 @@ def __init__(self):
137132
[
138133
(
139134
"__init__.py",
140-
_b(
141-
"""
135+
b"""
142136
from testtools import TestCase, clone_test_with_new_id
143137
144138
class TestExample(TestCase):
@@ -148,8 +142,7 @@ def test_foo(self):
148142
def load_tests(loader, tests, pattern):
149143
tests.addTest(clone_test_with_new_id(tests._tests[1]._tests[0], "fred"))
150144
return tests
151-
"""
152-
),
145+
""",
153146
)
154147
],
155148
)
@@ -288,12 +281,10 @@ def test_run_orders_tests(self):
288281
f = open(tempname, "wb")
289282
try:
290283
f.write(
291-
_b(
292-
"""
284+
b"""
293285
testtools.runexample.TestFoo.test_bar
294286
testtools.runexample.missingtest
295287
"""
296-
)
297288
)
298289
finally:
299290
f.close()
@@ -328,12 +319,10 @@ def test_run_load_list(self):
328319
f = open(tempname, "wb")
329320
try:
330321
f.write(
331-
_b(
332-
"""
322+
b"""
333323
testtools.runexample.TestFoo.test_bar
334324
testtools.runexample.missingtest
335325
"""
336-
)
337326
)
338327
finally:
339328
f.close()
@@ -368,12 +357,10 @@ def test_load_list_preserves_custom_suites(self):
368357
f = open(tempname, "wb")
369358
try:
370359
f.write(
371-
_b(
372-
"""
360+
b"""
373361
testtools.resourceexample.TestFoo.test_bar
374362
testtools.resourceexample.TestFoo.test_foo
375363
"""
376-
)
377364
)
378365
finally:
379366
f.close()

testtools/compat.py

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,19 @@
11
# Copyright (c) 2008-2015 testtools developers. See LICENSE for details.
22

3-
"""Compatibility support for python 2 and 3."""
3+
"""Compatibility support - kept for backwards compatibility."""
44

55
__all__ = [
66
"BytesIO",
77
"StringIO",
8-
"_b",
9-
"advance_iterator",
10-
"reraise",
8+
"text_repr",
119
"unicode_output_stream",
1210
]
1311

1412
import codecs
1513
import io
16-
import locale
17-
import os
1814
import sys
19-
import types
2015
import unicodedata
2116
from io import BytesIO, StringIO # for backwards-compat
22-
from typing import Any, NoReturn
23-
24-
25-
def reraise(
26-
exc_class: type[BaseException],
27-
exc_obj: BaseException,
28-
exc_tb: types.TracebackType,
29-
_marker: Any = object(),
30-
) -> NoReturn:
31-
"""Re-raise an exception received from sys.exc_info() or similar."""
32-
raise exc_obj.with_traceback(exc_tb)
33-
34-
35-
def _u(s):
36-
return s
37-
38-
39-
def _b(s):
40-
"""A byte literal."""
41-
return s.encode("latin-1")
42-
43-
44-
advance_iterator = next
4517

4618

4719
def _slow_escape(text):
@@ -149,15 +121,3 @@ def unicode_output_stream(stream):
149121
except AttributeError:
150122
pass
151123
return writer(stream, "replace")
152-
153-
154-
def _get_exception_encoding():
155-
"""Return the encoding we expect messages from the OS to be encoded in"""
156-
if os.name == "nt":
157-
# GZ 2010-05-24: Really want the codepage number instead, the error
158-
# handling of standard codecs is more deterministic
159-
return "mbcs"
160-
# GZ 2010-05-23: We need this call to be after initialisation, but there's
161-
# no benefit in asking more than once as it's a global
162-
# setting that can change after the message is formatted.
163-
return locale.getlocale(locale.LC_MESSAGES)[1] or "ascii"

0 commit comments

Comments
 (0)