Skip to content

Commit

Permalink
type annotate twisted.trial.test.test_pyunitcompat
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Sep 16, 2023
1 parent 0bb9f2b commit 7533423
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,6 @@ module = [
'twisted.trial.test.test_loader',
'twisted.trial.test.test_log',
'twisted.trial.test.test_plugins',
'twisted.trial.test.test_pyunitcompat',
'twisted.trial.test.test_reporter',
'twisted.trial.test.test_runner',
'twisted.trial.test.test_suppression',
Expand Down
56 changes: 28 additions & 28 deletions src/twisted/trial/test/test_pyunitcompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@


class PyUnitTestTests(SynchronousTestCase):
def setUp(self):
def setUp(self) -> None:
self.original = pyunitcases.PyUnitTest("test_pass")
self.test = ITestCase(self.original)

def test_callable(self):
def test_callable(self) -> None:
"""
Tests must be callable in order to be used with Python's unittest.py.
"""
Expand All @@ -45,43 +45,43 @@ class ErrorTest(SynchronousTestCase):

ran = False

def test_foo(self):
def test_foo(self) -> None:
"""
Set C{self.ran} to True and raise a C{ZeroDivisionError}
"""
self.ran = True
1 / 0

def test_dontUseAdapterWhenReporterProvidesIReporter(self):
def test_dontUseAdapterWhenReporterProvidesIReporter(self) -> None:
"""
The L{PyUnitResultAdapter} is only used when the result passed to
C{run} does *not* provide L{IReporter}.
"""

@implementer(IReporter)
class StubReporter:
class StubReporter: # type: ignore[misc]
"""
A reporter which records data about calls made to it.
@ivar errors: Errors passed to L{addError}.
@ivar failures: Failures passed to L{addFailure}.
"""

def __init__(self):
self.errors = []
self.failures = []
def __init__(self) -> None:
self.errors: list[Failure] = []
self.failures: list[None] = []

def startTest(self, test):
def startTest(self, test: object) -> None:
"""
Do nothing.
"""

def stopTest(self, test):
def stopTest(self, test: object) -> None:
"""
Do nothing.
"""

def addError(self, test, error):
def addError(self, test: object, error: Failure) -> None:
"""
Record the error.
"""
Expand All @@ -92,11 +92,11 @@ def addError(self, test, error):
test.run(result)
self.assertIsInstance(result.errors[0], Failure)

def test_success(self):
def test_success(self) -> None:
class SuccessTest(SynchronousTestCase):
ran = False

def test_foo(s):
def test_foo(s) -> None:
s.ran = True

test = SuccessTest("test_foo")
Expand All @@ -107,11 +107,11 @@ def test_foo(s):
self.assertEqual(1, result.testsRun)
self.assertTrue(result.wasSuccessful())

def test_failure(self):
def test_failure(self) -> None:
class FailureTest(SynchronousTestCase):
ran = False

def test_foo(s):
def test_foo(s) -> None:
s.ran = True
s.fail("boom!")

Expand All @@ -124,7 +124,7 @@ def test_foo(s):
self.assertEqual(1, len(result.failures))
self.assertFalse(result.wasSuccessful())

def test_error(self):
def test_error(self) -> None:
test = self.ErrorTest("test_foo")
result = pyunit.TestResult()
test.run(result)
Expand All @@ -134,14 +134,14 @@ def test_error(self):
self.assertEqual(1, len(result.errors))
self.assertFalse(result.wasSuccessful())

def test_setUpError(self):
def test_setUpError(self) -> None:
class ErrorTest(SynchronousTestCase):
ran = False

def setUp(self):
def setUp(self) -> None:
1 / 0

def test_foo(s):
def test_foo(s) -> None:
s.ran = True

test = ErrorTest("test_foo")
Expand All @@ -153,7 +153,7 @@ def test_foo(s):
self.assertEqual(1, len(result.errors))
self.assertFalse(result.wasSuccessful())

def test_tracebackFromFailure(self):
def test_tracebackFromFailure(self) -> None:
"""
Errors added through the L{PyUnitResultAdapter} have the same traceback
information as if there were no adapter at all.
Expand All @@ -170,15 +170,15 @@ def test_tracebackFromFailure(self):
pyresult.errors[0][1], "".join(traceback.format_exception(*exc_info))
)

def test_traceback(self):
def test_traceback(self) -> None:
"""
As test_tracebackFromFailure, but covering more code.
"""

class ErrorTest(SynchronousTestCase):
exc_info = None

def test_foo(self):
def test_foo(self) -> None:
try:
1 / 0
except ZeroDivisionError:
Expand All @@ -195,15 +195,15 @@ def test_foo(self):
#
# So, we just test that the result's stack ends with the
# exception's stack.

assert test.exc_info is not None
expected_stack = "".join(traceback.format_tb(test.exc_info[2]))
observed_stack = "\n".join(result.errors[0][1].splitlines()[:-1])

self.assertEqual(
expected_stack.strip(), observed_stack[-len(expected_stack) :].strip()
)

def test_tracebackFromCleanFailure(self):
def test_tracebackFromCleanFailure(self) -> None:
"""
Errors added through the L{PyUnitResultAdapter} have the same
traceback information as if there were no adapter at all, even
Expand All @@ -224,31 +224,31 @@ def test_tracebackFromCleanFailure(self):
tback.endswith("ZeroDivisionError: division by zero\n"),
)

def test_trialSkip(self):
def test_trialSkip(self) -> None:
"""
Skips using trial's skipping functionality are reported as skips in
the L{pyunit.TestResult}.
"""

class SkipTest(SynchronousTestCase):
@skipIf(True, "Let's skip!")
def test_skip(self):
def test_skip(self) -> None:
1 / 0

test = SkipTest("test_skip")
result = pyunit.TestResult()
test.run(result)
self.assertEqual(result.skipped, [(test, "Let's skip!")])

def test_pyunitSkip(self):
def test_pyunitSkip(self) -> None:
"""
Skips using pyunit's skipping functionality are reported as skips in
the L{pyunit.TestResult}.
"""

class SkipTest(SynchronousTestCase):
@pyunit.skip("skippy")
def test_skip(self):
def test_skip(self) -> None:
1 / 0

test = SkipTest("test_skip")
Expand Down

0 comments on commit 7533423

Please sign in to comment.