Skip to content

Commit

Permalink
type annotate twisted.test.test_randbytes
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Sep 19, 2023
1 parent 8fb37ca commit 1a1caac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ module = [
'twisted.test.test_postfix',
'twisted.test.test_process',
'twisted.test.test_protocols',
'twisted.test.test_randbytes',
'twisted.test.test_reflect',
'twisted.test.test_sip',
'twisted.test.test_sob',
Expand Down
32 changes: 22 additions & 10 deletions src/twisted/test/test_randbytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@
"""
Test cases for L{twisted.python.randbytes}.
"""
from __future__ import annotations

from typing import Callable

from typing_extensions import NoReturn, Protocol

from twisted.python import randbytes
from twisted.trial import unittest


class _SupportsAssertions(Protocol):
def assertEqual(self, a: object, b: object) -> object:
...

def assertNotEqual(self, a: object, b: object) -> object:
...


class SecureRandomTestCaseBase:
"""
Base class for secureRandom test cases.
"""

def _check(self, source):
def _check(self: _SupportsAssertions, source: Callable[[int], bytes]) -> None:
"""
The given random bytes source should return the number of bytes
requested each time it is called and should probably not return the
Expand All @@ -37,7 +49,7 @@ class SecureRandomTests(SecureRandomTestCaseBase, unittest.TestCase):
Test secureRandom under normal conditions.
"""

def test_normal(self):
def test_normal(self) -> None:
"""
L{randbytes.secureRandom} should return a string of the requested
length and make some effort to make its result otherwise unpredictable.
Expand All @@ -52,36 +64,36 @@ class ConditionalSecureRandomTests(
Test random sources one by one, then remove it to.
"""

def setUp(self):
def setUp(self) -> None:
"""
Create a L{randbytes.RandomFactory} to use in the tests.
"""
self.factory = randbytes.RandomFactory()

def errorFactory(self, nbytes):
def errorFactory(self, nbytes: object) -> NoReturn:
"""
A factory raising an error when a source is not available.
"""
raise randbytes.SourceNotAvailable()

def test_osUrandom(self):
def test_osUrandom(self) -> None:
"""
L{RandomFactory._osUrandom} should work as a random source whenever
L{os.urandom} is available.
"""
self._check(self.factory._osUrandom)

def test_withoutAnything(self):
def test_withoutAnything(self) -> None:
"""
Remove all secure sources and assert it raises a failure. Then try the
fallback parameter.
"""
self.factory._osUrandom = self.errorFactory
self.factory._osUrandom = self.errorFactory # type: ignore[method-assign]
self.assertRaises(
randbytes.SecureRandomNotAvailable, self.factory.secureRandom, 18
)

def wrapper():
def wrapper() -> bytes:
return self.factory.secureRandom(18, fallback=True)

s = self.assertWarns(
Expand All @@ -99,13 +111,13 @@ class RandomBaseTests(SecureRandomTestCaseBase, unittest.SynchronousTestCase):
'Normal' random test cases.
"""

def test_normal(self):
def test_normal(self) -> None:
"""
Test basic case.
"""
self._check(randbytes.insecureRandom)

def test_withoutGetrandbits(self):
def test_withoutGetrandbits(self) -> None:
"""
Test C{insecureRandom} without C{random.getrandbits}.
"""
Expand Down

0 comments on commit 1a1caac

Please sign in to comment.