From b1e4145e3315152fe5b8691efc12043b48782ab0 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Fri, 27 Feb 2026 20:48:37 +0000 Subject: [PATCH] typing: Fix hints for TestCase.useFixture We have fixtures installed in the mypy env so we can rely on it for typing. Signed-off-by: Stephen Finucane --- testtools/testcase.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/testtools/testcase.py b/testtools/testcase.py index 334bc2b9..e07d1ecb 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -23,7 +23,7 @@ import types import unittest from collections.abc import Callable, Iterator -from typing import Protocol, TypeVar, cast +from typing import TypeVar, cast from unittest.case import SkipTest T = TypeVar("T") @@ -56,6 +56,14 @@ TestResult, ) +# Circular import: fixtures imports gather_details from here, we import +# fixtures, leading to gather_details not being available and fixtures being +# unable to import it. +try: + import fixtures +except ImportError: + fixtures = None # type: ignore + class _UnexpectedSuccess(Exception): """An unexpected success was raised. @@ -183,22 +191,7 @@ def gather_details(source_dict: DetailsDict, target_dict: DetailsDict) -> None: target_dict[name] = _copy_content(content_object) -# Circular import: fixtures imports gather_details from here, we import -# fixtures, leading to gather_details not being available and fixtures being -# unable to import it. -try: - import fixtures -except ImportError: - fixtures = None # type: ignore - - -class UseFixtureProtocol(Protocol): - def setUp(self) -> None: ... - def cleanUp(self) -> None: ... - def getDetails(self) -> DetailsDict: ... - - -UseFixtureT = TypeVar("UseFixtureT", bound=UseFixtureProtocol) +FixtureT = TypeVar("FixtureT", bound="fixtures.Fixture") def _mods(i: int, mod: int) -> Iterator[int]: @@ -892,7 +885,7 @@ def _run_test_method(self, result: TestResult) -> object: """ return self._get_test_method()() - def useFixture(self, fixture: UseFixtureT) -> UseFixtureT: + def useFixture(self, fixture: "FixtureT") -> "FixtureT": """Use fixture in a test case. The fixture will be setUp, and self.addCleanup(fixture.cleanUp) called.