Skip to content

Commit f85d890

Browse files
committed
Replace cast and Any with assertions and explicit types
1 parent f033a7b commit f85d890

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

testtools/testcase.py

Lines changed: 14 additions & 7 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 Any, Protocol, TypeVar, cast
26+
from typing import Protocol, TypeVar, cast
2727
from unittest.case import SkipTest
2828

2929
T = TypeVar("T")
@@ -271,9 +271,9 @@ def __init__(self, *args: object, **kwargs: object) -> None:
271271
test_method = self._get_test_method()
272272
if runTest is None:
273273
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
277277
if getattr(test_method, "__unittest_expecting_failure__", False):
278278
setattr(self, self._testMethodName, _expectedFailure(test_method))
279279
# Used internally for onException processing - used to gather extra
@@ -1130,16 +1130,23 @@ class WithAttributes:
11301130
testtools.testcase.MyTest/test_bar[foo]
11311131
"""
11321132

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]]
11341135

11351136
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+
)
11371141
# Depends on testtools.TestCase._get_test_method, be nice to support
11381142
# plain unittest.
11391143
fn = self._get_test_method()
1140-
attributes = cast(str, getattr(fn, "__testtools_attrs", None))
1144+
attributes = getattr(fn, "__testtools_attrs", None)
11411145
if not attributes:
11421146
return orig
1147+
assert isinstance(attributes, set), (
1148+
f"Expected set for __testtools_attrs, got {type(attributes)}"
1149+
)
11431150
return orig + "[" + ",".join(sorted(attributes)) + "]"
11441151

11451152

0 commit comments

Comments
 (0)