|
23 | 23 | import types |
24 | 24 | import unittest |
25 | 25 | from collections.abc import Callable, Iterator |
26 | | -from typing import Any, Protocol, TypeVar, cast |
| 26 | +from typing import Protocol, TypeVar, cast |
27 | 27 | from unittest.case import SkipTest |
28 | 28 |
|
29 | 29 | T = TypeVar("T") |
@@ -271,9 +271,9 @@ def __init__(self, *args: object, **kwargs: object) -> None: |
271 | 271 | test_method = self._get_test_method() |
272 | 272 | if runTest is None: |
273 | 273 | runTest = getattr(test_method, "_run_test_with", self.run_tests_with) |
274 | | - self.__RunTest: type[RunTest] | Callable[..., RunTest] = cast( |
275 | | - "type[RunTest] | Callable[..., RunTest]", runTest |
276 | | - ) |
| 274 | + # runTest should be a RunTest class or factory function |
| 275 | + assert callable(runTest), f"runTest must be callable, got {type(runTest)}" |
| 276 | + self.__RunTest: type[RunTest] | Callable[..., RunTest] = runTest |
277 | 277 | if getattr(test_method, "__unittest_expecting_failure__", False): |
278 | 278 | setattr(self, self._testMethodName, _expectedFailure(test_method)) |
279 | 279 | # Used internally for onException processing - used to gather extra |
@@ -1130,16 +1130,23 @@ class WithAttributes: |
1130 | 1130 | testtools.testcase.MyTest/test_bar[foo] |
1131 | 1131 | """ |
1132 | 1132 |
|
1133 | | - _get_test_method: Any # Provided by the class we're mixed with |
| 1133 | + # Provided by the class we're mixed with |
| 1134 | + _get_test_method: Callable[[], Callable[[], object]] |
1134 | 1135 |
|
1135 | 1136 | def id(self) -> str: |
1136 | | - orig = cast(str, super().id()) # type: ignore[misc] |
| 1137 | + orig = super().id() # type: ignore[misc] |
| 1138 | + assert isinstance(orig, str), ( |
| 1139 | + f"Expected str from super().id(), got {type(orig)}" |
| 1140 | + ) |
1137 | 1141 | # Depends on testtools.TestCase._get_test_method, be nice to support |
1138 | 1142 | # plain unittest. |
1139 | 1143 | fn = self._get_test_method() |
1140 | | - attributes = cast(str, getattr(fn, "__testtools_attrs", None)) |
| 1144 | + attributes = getattr(fn, "__testtools_attrs", None) |
1141 | 1145 | if not attributes: |
1142 | 1146 | return orig |
| 1147 | + assert isinstance(attributes, set), ( |
| 1148 | + f"Expected set for __testtools_attrs, got {type(attributes)}" |
| 1149 | + ) |
1143 | 1150 | return orig + "[" + ",".join(sorted(attributes)) + "]" |
1144 | 1151 |
|
1145 | 1152 |
|
|
0 commit comments