Skip to content

Commit 6b46551

Browse files
authored
Merge pull request #455 from allenap/preserve-type-when-using-fixture
Preserve type when using fixture
2 parents 9d05a38 + 2ca1eba commit 6b46551

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

testtools/compat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
import locale
1717
import os
1818
import sys
19+
import typing
1920
import unicodedata
2021

2122
# Ensure retro-compatibility with older testtools releases
2223
from io import BytesIO, StringIO
2324

2425

25-
def reraise(exc_class, exc_obj, exc_tb, _marker=object()):
26+
def reraise(exc_class, exc_obj, exc_tb, _marker=object()) -> typing.NoReturn:
2627
"""Re-raise an exception received from sys.exc_info() or similar."""
2728
raise exc_obj.with_traceback(exc_tb)
2829

testtools/testcase.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import sys
2222
import types
2323
import unittest
24+
from typing import Any, Protocol, TypeVar
2425
from unittest.case import SkipTest
2526

2627
from testtools import content
@@ -168,6 +169,15 @@ def gather_details(source_dict, target_dict):
168169
fixtures = try_import("fixtures")
169170

170171

172+
class UseFixtureProtocol(Protocol):
173+
def setUp(self) -> Any: ...
174+
def cleanUp(self) -> Any: ...
175+
def getDetails(self) -> dict[str, content.Content]: ...
176+
177+
178+
UseFixtureT = TypeVar("UseFixtureT", bound=UseFixtureProtocol)
179+
180+
171181
def _mods(i, mod):
172182
(q, r) = divmod(i, mod)
173183
while True:
@@ -729,7 +739,7 @@ def _run_test_method(self, result):
729739
"""
730740
return self._get_test_method()()
731741

732-
def useFixture(self, fixture):
742+
def useFixture(self, fixture: UseFixtureT) -> UseFixtureT:
733743
"""Use fixture in a test case.
734744
735745
The fixture will be setUp, and self.addCleanup(fixture.cleanUp) called.

0 commit comments

Comments
 (0)