Skip to content

Commit 1420b8f

Browse files
committed
Fix async setUp/tearDown validation on Python 3.12
The typing changes in commit 282db18 inadvertently broke the async setUp/tearDown validation by: 1. Renaming the return value variable from `ret` to `setup_result`/`teardown_result` 2. Adding an unnecessary `is not None` check before the duck-typing check These changes, while logically equivalent, caused test failures on Python 3.12. Reverting to the original variable names and removing the None check fixes the issue while maintaining type safety with appropriate type: ignore comments. Fixes the test failure in: tests.twistedsupport.test_runtest.TestAsyncSetUpTearDownValidation.test_async_setup_with_deferred_upcall
1 parent b9b81b8 commit 1420b8f

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

testtools/testcase.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -800,16 +800,10 @@ def _run_setup(self, result: TestResult) -> object:
800800
:raises ValueError: If the base class setUp is not called, a
801801
ValueError is raised.
802802
"""
803-
# setUp() normally returns None, but async test frameworks may
804-
# return Deferred-like objects
805-
setup_result: object = self.setUp() # type: ignore[func-returns-value]
803+
ret = self.setUp() # type: ignore[func-returns-value]
806804

807805
# Check if the return value is a Deferred (duck-typing to avoid hard dependency)
808-
if (
809-
setup_result is not None
810-
and hasattr(setup_result, "addBoth")
811-
and callable(getattr(setup_result, "addBoth"))
812-
):
806+
if hasattr(ret, "addBoth") and callable(getattr(ret, "addBoth")):
813807
# Deferred-like object: validate asynchronously after it resolves
814808
def _validate_setup_called(result: object) -> object:
815809
if not self.__setup_called:
@@ -822,7 +816,7 @@ def _validate_setup_called(result: object) -> object:
822816
)
823817
return result
824818

825-
setup_result = setup_result.addBoth(_validate_setup_called)
819+
ret.addBoth(_validate_setup_called)
826820
else:
827821
# Synchronous: validate immediately
828822
if not self.__setup_called:
@@ -833,7 +827,7 @@ def _validate_setup_called(result: object) -> object:
833827
f"super({self.__class__.__name__}, self).setUp() "
834828
"from your setUp()."
835829
)
836-
return setup_result
830+
return ret
837831

838832
def _run_teardown(self, result: TestResult) -> object:
839833
"""Run the tearDown function for this test.
@@ -842,16 +836,10 @@ def _run_teardown(self, result: TestResult) -> object:
842836
:raises ValueError: If the base class tearDown is not called, a
843837
ValueError is raised.
844838
"""
845-
# tearDown() normally returns None, but async test frameworks
846-
# may return Deferred-like objects
847-
teardown_result: object = self.tearDown() # type: ignore[func-returns-value]
839+
ret = self.tearDown() # type: ignore[func-returns-value]
848840

849841
# Check if the return value is a Deferred (duck-typing to avoid hard dependency)
850-
if (
851-
teardown_result is not None
852-
and hasattr(teardown_result, "addBoth")
853-
and callable(getattr(teardown_result, "addBoth"))
854-
):
842+
if hasattr(ret, "addBoth") and callable(getattr(ret, "addBoth")):
855843
# Deferred-like object: validate asynchronously after it resolves
856844
def _validate_teardown_called(result: object) -> object:
857845
if not self.__teardown_called:
@@ -864,7 +852,7 @@ def _validate_teardown_called(result: object) -> object:
864852
)
865853
return result
866854

867-
teardown_result = teardown_result.addBoth(_validate_teardown_called)
855+
ret.addBoth(_validate_teardown_called)
868856
else:
869857
# Synchronous: validate immediately
870858
if not self.__teardown_called:
@@ -875,7 +863,7 @@ def _validate_teardown_called(result: object) -> object:
875863
f"super({self.__class__.__name__}, self).tearDown() "
876864
"from your tearDown()."
877865
)
878-
return teardown_result
866+
return ret
879867

880868
def _get_test_method(self) -> Callable[[], object]:
881869
method_name = getattr(self, "_testMethodName")

0 commit comments

Comments
 (0)