Skip to content

Commit

Permalink
type annotate twisted.test.test_monkey
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Sep 8, 2023
1 parent 70fff80 commit 8321b80
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,6 @@ module = [
'twisted.test.test_htb',
'twisted.test.test_iosim',
'twisted.test.test_lockfile',
'twisted.test.test_monkey',
'twisted.trial._asyncrunner',
'twisted.trial._dist.distreporter',
'twisted.trial._dist.disttrial',
Expand Down
38 changes: 20 additions & 18 deletions src/twisted/test/test_monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
"""
Tests for L{twisted.python.monkey}.
"""
from __future__ import annotations

from typing_extensions import NoReturn

from twisted.python.monkey import MonkeyPatcher
from twisted.trial import unittest


class TestObj:
def __init__(self):
def __init__(self) -> None:
self.foo = "foo value"
self.bar = "bar value"
self.baz = "baz value"
Expand All @@ -22,12 +24,12 @@ class MonkeyPatcherTests(unittest.SynchronousTestCase):
Tests for L{MonkeyPatcher} monkey-patching class.
"""

def setUp(self):
def setUp(self) -> None:
self.testObject = TestObj()
self.originalObject = TestObj()
self.monkeyPatcher = MonkeyPatcher()

def test_empty(self):
def test_empty(self) -> None:
"""
A monkey patcher without patches shouldn't change a thing.
"""
Expand All @@ -39,7 +41,7 @@ def test_empty(self):
self.assertEqual(self.originalObject.bar, self.testObject.bar)
self.assertEqual(self.originalObject.baz, self.testObject.baz)

def test_constructWithPatches(self):
def test_constructWithPatches(self) -> None:
"""
Constructing a L{MonkeyPatcher} with patches should add all of the
given patches to the patch list.
Expand All @@ -52,7 +54,7 @@ def test_constructWithPatches(self):
self.assertEqual("hehe", self.testObject.bar)
self.assertEqual(self.originalObject.baz, self.testObject.baz)

def test_patchExisting(self):
def test_patchExisting(self) -> None:
"""
Patching an attribute that exists sets it to the value defined in the
patch.
Expand All @@ -61,14 +63,14 @@ def test_patchExisting(self):
self.monkeyPatcher.patch()
self.assertEqual(self.testObject.foo, "haha")

def test_patchNonExisting(self):
def test_patchNonExisting(self) -> None:
"""
Patching a non-existing attribute fails with an C{AttributeError}.
"""
self.monkeyPatcher.addPatch(self.testObject, "nowhere", "blow up please")
self.assertRaises(AttributeError, self.monkeyPatcher.patch)

def test_patchAlreadyPatched(self):
def test_patchAlreadyPatched(self) -> None:
"""
Adding a patch for an object and attribute that already have a patch
overrides the existing patch.
Expand All @@ -80,7 +82,7 @@ def test_patchAlreadyPatched(self):
self.monkeyPatcher.restore()
self.assertEqual(self.testObject.foo, self.originalObject.foo)

def test_restoreTwiceIsANoOp(self):
def test_restoreTwiceIsANoOp(self) -> None:
"""
Restoring an already-restored monkey patch is a no-op.
"""
Expand All @@ -91,28 +93,28 @@ def test_restoreTwiceIsANoOp(self):
self.monkeyPatcher.restore()
self.assertEqual(self.testObject.foo, self.originalObject.foo)

def test_runWithPatchesDecoration(self):
def test_runWithPatchesDecoration(self) -> None:
"""
runWithPatches should run the given callable, passing in all arguments
and keyword arguments, and return the return value of the callable.
"""
log = []
log: list[tuple[int, int, int | None]] = []

def f(a, b, c=None):
def f(a: int, b: int, c: int | None = None) -> str:
log.append((a, b, c))
return "foo"

result = self.monkeyPatcher.runWithPatches(f, 1, 2, c=10)
self.assertEqual("foo", result)
self.assertEqual([(1, 2, 10)], log)

def test_repeatedRunWithPatches(self):
def test_repeatedRunWithPatches(self) -> None:
"""
We should be able to call the same function with runWithPatches more
than once. All patches should apply for each call.
"""

def f():
def f() -> tuple[str, str, str]:
return (self.testObject.foo, self.testObject.bar, self.testObject.baz)

self.monkeyPatcher.addPatch(self.testObject, "foo", "haha")
Expand All @@ -125,7 +127,7 @@ def f():
("haha", self.originalObject.bar, self.originalObject.baz), result
)

def test_runWithPatchesRestores(self):
def test_runWithPatchesRestores(self) -> None:
"""
C{runWithPatches} should restore the original values after the function
has executed.
Expand All @@ -135,13 +137,13 @@ def test_runWithPatchesRestores(self):
self.monkeyPatcher.runWithPatches(lambda: None)
self.assertEqual(self.originalObject.foo, self.testObject.foo)

def test_runWithPatchesRestoresOnException(self):
def test_runWithPatchesRestoresOnException(self) -> None:
"""
Test runWithPatches restores the original values even when the function
raises an exception.
"""

def _():
def _() -> NoReturn:
self.assertEqual(self.testObject.foo, "haha")
self.assertEqual(self.testObject.bar, "blahblah")
raise RuntimeError("Something went wrong!")
Expand All @@ -153,7 +155,7 @@ def _():
self.assertEqual(self.testObject.foo, self.originalObject.foo)
self.assertEqual(self.testObject.bar, self.originalObject.bar)

def test_contextManager(self):
def test_contextManager(self) -> None:
"""
L{MonkeyPatcher} is a context manager that applies its patches on
entry and restore original values on exit.
Expand All @@ -163,7 +165,7 @@ def test_contextManager(self):
self.assertEqual(self.testObject.foo, "patched value")
self.assertEqual(self.testObject.foo, self.originalObject.foo)

def test_contextManagerPropagatesExceptions(self):
def test_contextManagerPropagatesExceptions(self) -> None:
"""
Exceptions propagate through the L{MonkeyPatcher} context-manager
exit method.
Expand Down

0 comments on commit 8321b80

Please sign in to comment.