Skip to content

Commit 121c3a1

Browse files
authored
Merge pull request testing-cabal#593 from stephenfin/typing
Another round of typing fixes
2 parents 6ce79d5 + 17370fe commit 121c3a1

File tree

17 files changed

+148
-156
lines changed

17 files changed

+148
-156
lines changed

tests/test_assert_that.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def __str__(self) -> str:
7474
# Create a test class that inherits from the actual test class
7575
test_self = self
7676

77-
class Test(test_self.__class__): # type: ignore[misc,name-defined]
77+
class Test(test_self.__class__):
7878
def test(self):
7979
test_self.assert_that_callable("foo", Matcher())
8080

tests/test_content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def test___init___None_errors(self):
260260
# we are intentionally passing an invalid type
261261
self.assertThat(
262262
lambda: TracebackContent(None, None),
263-
raises_value_error, # type: ignore[arg-type]
263+
raises_value_error,
264264
)
265265

266266
def test___init___sets_ivars(self):

tests/test_content_type.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def test_json_content(self):
5454
# The JSON content type represents implictly UTF-8 application/json.
5555
self.assertThat(JSON.type, Equals("application"))
5656
self.assertThat(JSON.subtype, Equals("json"))
57-
self.assertThat(JSON.parameters, Equals({}))
57+
expected_parameters: dict[str, str] = {}
58+
self.assertThat(JSON.parameters, Equals(expected_parameters))
5859

5960

6061
def test_suite():

tests/test_fixturesupport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
try:
2424
from fixtures.tests.helpers import LoggingFixture
2525
except ImportError:
26-
LoggingFixture = None # type: ignore
26+
LoggingFixture = None
2727

2828

2929
class TestFixtureSupport(TestCase):

tests/test_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
try:
2626
import testresources
2727
except ImportError:
28-
testresources = None # type: ignore
28+
testresources = None
2929

3030

3131
if fixtures:

tests/test_testcase.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,11 +2334,11 @@ def test_other_attribute(self):
23342334
orig = PlaceHolder("foo")
23352335
orig.thing = "fred" # type: ignore[attr-defined]
23362336
case = DecorateTestCaseResult(orig, self.make_result)
2337-
self.assertEqual("fred", case.thing) # type: ignore[attr-defined]
2337+
self.assertEqual("fred", case.thing)
23382338
self.assertRaises(AttributeError, getattr, case, "other")
2339-
case.other = "barbara" # type: ignore[attr-defined]
2339+
case.other = "barbara"
23402340
self.assertEqual("barbara", orig.other) # type: ignore[attr-defined]
2341-
del case.thing # type: ignore[attr-defined]
2341+
del case.thing
23422342
self.assertRaises(AttributeError, getattr, orig, "thing")
23432343

23442344

tests/test_testresult.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,11 +2108,13 @@ def setUp(self):
21082108
self.result = TextTestResult(io.StringIO())
21092109

21102110
def getvalue(self):
2111+
assert isinstance(self.result.stream, io.StringIO)
21112112
return self.result.stream.getvalue()
21122113

21132114
def test__init_sets_stream(self):
2114-
result = TextTestResult("fp")
2115-
self.assertEqual("fp", result.stream)
2115+
stream = io.StringIO()
2116+
result = TextTestResult(stream)
2117+
self.assertEqual(stream, result.stream)
21162118

21172119
def reset_output(self):
21182120
self.result.stream = io.StringIO()
@@ -2285,6 +2287,7 @@ def __init__(self, stream):
22852287

22862288
def addSuccess(self, test, details=None):
22872289
super().addSuccess(test, details)
2290+
assert self.stream is not None
22882291
self.stream.write("CUSTOM_SUCCESS_MARKER\n")
22892292

22902293
stream = io.StringIO()
@@ -2954,8 +2957,14 @@ def check_outcome_details_to_exec_info(self, outcome, expected=None):
29542957
expected = outcome
29552958
details, _err_str = self.get_details_and_string()
29562959
getattr(self.converter, outcome)(self, details=details)
2957-
err = self.converter._details_to_exc_info(details)
2958-
self.assertEqual([(expected, self, err)], self.result._events)
2960+
expected_err = self.converter._details_to_exc_info(details)
2961+
self.assertEqual(1, len(self.result._events))
2962+
event_outcome, event_test, event_err = self.result._events[0]
2963+
self.assertEqual(expected, event_outcome)
2964+
self.assertEqual(self, event_test)
2965+
# Compare exc type and value; traceback objects differ between calls
2966+
self.assertEqual(expected_err[:2], event_err[:2])
2967+
self.assertIsNotNone(event_err[2])
29592968

29602969
def check_outcome_details_to_nothing(self, outcome, expected=None):
29612970
"""Call an outcome with a details dict to be swallowed."""
@@ -3556,7 +3565,7 @@ def test_no_details(self):
35563565

35573566
def test_binary_content(self):
35583567
content = content_from_stream(
3559-
io.StringIO("foo"), content_type=ContentType("image", "jpeg")
3568+
io.BytesIO(b"foo"), content_type=ContentType("image", "jpeg")
35603569
)
35613570
string = _details_to_str({"attachment": content})
35623571
self.assertThat(
@@ -3611,16 +3620,16 @@ def test_empty_attachment(self):
36113620
)
36123621

36133622
def test_lots_of_different_attachments(self):
3614-
def jpg(x):
3615-
return content_from_stream(io.StringIO(x), ContentType("image", "jpeg"))
3623+
def jpg(x: bytes) -> Content:
3624+
return content_from_stream(io.BytesIO(x), ContentType("image", "jpeg"))
36163625

36173626
attachments = {
36183627
"attachment": text_content("foo"),
36193628
"attachment-1": text_content("traceback"),
3620-
"attachment-2": jpg("pic1"),
3629+
"attachment-2": jpg(b"pic1"),
36213630
"attachment-3": text_content("bar"),
36223631
"attachment-4": text_content(""),
3623-
"attachment-5": jpg("pic2"),
3632+
"attachment-5": jpg(b"pic2"),
36243633
}
36253634
string = _details_to_str(attachments, special="attachment-1")
36263635
self.assertThat(
@@ -3648,7 +3657,7 @@ def setUp(self):
36483657
self.log = []
36493658
self.result = TestByTestResult(self.on_test)
36503659
now = iter(range(5))
3651-
self.result._now = lambda: next(now) # type: ignore[method-assign]
3660+
self.result._now = lambda: next(now) # type: ignore[method-assign,assignment,return-value]
36523661

36533662
def assertCalled(self, **kwargs):
36543663
defaults = {

tests/test_testsuite.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ class TestIterateTests(TestCase):
391391
def test_iterate_suite(self):
392392
a = PlaceHolder("a")
393393
b = PlaceHolder("b")
394-
suite = unittest.TestSuite([a, b]) # type: ignore[list-item]
394+
suite = unittest.TestSuite([a, b])
395395
self.assertEqual([a, b], list(iterate_tests(suite)))
396396

397397
def test_iterate_single_test(self):
@@ -413,7 +413,7 @@ class Subclass(unittest.TestSuite):
413413
def sort_tests(self):
414414
self._tests = sorted_tests(self, True)
415415

416-
input_suite = Subclass([b, a]) # type: ignore[list-item]
416+
input_suite = Subclass([b, a])
417417
suite = sorted_tests(input_suite)
418418
self.assertEqual([a, b], list(iterate_tests(suite)))
419419
self.assertEqual([input_suite], list(iter(suite)))
@@ -425,22 +425,22 @@ def test_custom_suite_without_sort_tests_works(self):
425425
class Subclass(unittest.TestSuite):
426426
pass
427427

428-
input_suite = Subclass([b, a]) # type: ignore[list-item]
428+
input_suite = Subclass([b, a])
429429
suite = sorted_tests(input_suite)
430430
self.assertEqual([b, a], list(iterate_tests(suite)))
431431
self.assertEqual([input_suite], list(iter(suite)))
432432

433433
def test_sorts_simple_suites(self):
434434
a = PlaceHolder("a")
435435
b = PlaceHolder("b")
436-
suite = sorted_tests(unittest.TestSuite([b, a])) # type: ignore[list-item]
436+
suite = sorted_tests(unittest.TestSuite([b, a]))
437437
self.assertEqual([a, b], list(iterate_tests(suite)))
438438

439439
def test_duplicate_simple_suites(self):
440440
a = PlaceHolder("a")
441441
b = PlaceHolder("b")
442442
c = PlaceHolder("a")
443-
self.assertRaises(ValueError, sorted_tests, unittest.TestSuite([a, b, c])) # type: ignore[list-item]
443+
self.assertRaises(ValueError, sorted_tests, unittest.TestSuite([a, b, c]))
444444

445445
def test_multiple_duplicates(self):
446446
# If there are multiple duplicates on a test suite, we report on them
@@ -450,9 +450,7 @@ def test_multiple_duplicates(self):
450450
c = PlaceHolder("a")
451451
d = PlaceHolder("b")
452452
error = self.assertRaises(
453-
ValueError,
454-
sorted_tests,
455-
unittest.TestSuite([a, b, c, d]), # type: ignore[list-item]
453+
ValueError, sorted_tests, unittest.TestSuite([a, b, c, d])
456454
)
457455
self.assertThat(
458456
str(error),

tests/twistedsupport/test_runtest.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@
4545
flush_logged_errors,
4646
)
4747
except ImportError:
48-
assert_fails_with = None # type: ignore[assignment,misc]
48+
assert_fails_with = None # type: ignore[assignment]
4949
AsynchronousDeferredRunTest = None # type: ignore[assignment,misc]
50-
flush_logged_errors = None # type: ignore[assignment,misc]
50+
flush_logged_errors = None # type: ignore[assignment]
5151
SynchronousDeferredRunTest = None # type: ignore[assignment,misc]
5252

5353
try:
5454
from twisted.internet import defer
5555
except ImportError:
56-
defer = None # type: ignore[assignment,misc]
56+
defer = None # type: ignore[assignment]
5757

5858
try:
5959
from twisted.python import failure, log
6060
except ImportError:
61-
failure = None # type: ignore[assignment,misc]
62-
log = None # type: ignore[assignment,misc]
61+
failure = None # type: ignore[assignment]
62+
log = None # type: ignore[assignment]
6363

6464
try:
6565
from twisted.internet.base import DelayedCall
@@ -69,7 +69,7 @@
6969
try:
7070
from testtools.twistedsupport._runtest import _get_global_publisher_and_observers
7171
except ImportError:
72-
_get_global_publisher_and_observers = None # type: ignore[assignment,misc]
72+
_get_global_publisher_and_observers = None # type: ignore[assignment]
7373

7474

7575
class X:
@@ -306,7 +306,7 @@ def test_something(self):
306306

307307
def fire_deferred():
308308
self.assertThat(call_log, Equals(["setUp"]))
309-
d.callback(marker) # type: ignore[arg-type]
309+
d.callback(marker)
310310

311311
test = SomeCase("test_something")
312312
timeout = self.make_timeout()

testtools/_types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2026 testtools developers. See LICENSE for details.
2+
3+
from types import TracebackType
4+
from typing import TypeAlias
5+
6+
# Type for exc_info tuples from sys.exc_info()
7+
ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType]
8+
OptExcInfo: TypeAlias = ExcInfo | tuple[None, None, None]

0 commit comments

Comments
 (0)