How about preventing this function from being bound as a method from the very beginning? You can wrap the function you're testing in a staticmethod
.
This signals to Python that the function doesn't receive the instance as its first argument. As a result, you can call it directly, which simplifies your helper method and makes the type Callable
perfectly accurate.
Here is a example:
from typing import Callable, Any, Optional
import sys
# Assume this is your external library in mymodule.py
class mymodule:
@staticmethod
def myfunction(s: str) -> str:
return f"processed: {s}"
# Your test framework
class MyTestCase:
function_under_test: Optional[Callable[[str], Any]] = None
def assert_something(self, input_str: str, expected_result: Any) -> None:
if self.function_under_test is None:
raise AssertionError(
"To use this helper method, you must set the function_under_test "
"class variable within your test class."
)
result = self.function_under_test(input_str)
assert result == expected_result
print(f"Assertion passed for input '{input_str}'!")
class FunctionATest(MyTestCase):
function_under_test = staticmethod(mymodule.myfunction)
def test_whatever(self) -> None:
self.assert_something("foo bar baz", "processed: foo bar baz")