Skip to content

Commit 0f88c4d

Browse files
authored
Merge pull request #574 from stephenfin/typing
typing: Fix hints for TestCase.useFixture
2 parents c8cb67a + b1e4145 commit 0f88c4d

1 file changed

Lines changed: 11 additions & 18 deletions

File tree

testtools/testcase.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import types
2424
import unittest
2525
from collections.abc import Callable, Iterator
26-
from typing import Protocol, TypeVar, cast
26+
from typing import TypeVar, cast
2727
from unittest.case import SkipTest
2828

2929
T = TypeVar("T")
@@ -56,6 +56,14 @@
5656
TestResult,
5757
)
5858

59+
# Circular import: fixtures imports gather_details from here, we import
60+
# fixtures, leading to gather_details not being available and fixtures being
61+
# unable to import it.
62+
try:
63+
import fixtures
64+
except ImportError:
65+
fixtures = None # type: ignore
66+
5967

6068
class _UnexpectedSuccess(Exception):
6169
"""An unexpected success was raised.
@@ -183,22 +191,7 @@ def gather_details(source_dict: DetailsDict, target_dict: DetailsDict) -> None:
183191
target_dict[name] = _copy_content(content_object)
184192

185193

186-
# Circular import: fixtures imports gather_details from here, we import
187-
# fixtures, leading to gather_details not being available and fixtures being
188-
# unable to import it.
189-
try:
190-
import fixtures
191-
except ImportError:
192-
fixtures = None # type: ignore
193-
194-
195-
class UseFixtureProtocol(Protocol):
196-
def setUp(self) -> None: ...
197-
def cleanUp(self) -> None: ...
198-
def getDetails(self) -> DetailsDict: ...
199-
200-
201-
UseFixtureT = TypeVar("UseFixtureT", bound=UseFixtureProtocol)
194+
FixtureT = TypeVar("FixtureT", bound="fixtures.Fixture")
202195

203196

204197
def _mods(i: int, mod: int) -> Iterator[int]:
@@ -892,7 +885,7 @@ def _run_test_method(self, result: TestResult) -> object:
892885
"""
893886
return self._get_test_method()()
894887

895-
def useFixture(self, fixture: UseFixtureT) -> UseFixtureT:
888+
def useFixture(self, fixture: "FixtureT") -> "FixtureT":
896889
"""Use fixture in a test case.
897890
898891
The fixture will be setUp, and self.addCleanup(fixture.cleanUp) called.

0 commit comments

Comments
 (0)