diff --git a/tests/gateway/test_compression_session_id_persistence.py b/tests/gateway/test_compression_session_id_persistence.py index 2d5bb9413204..f3468cadff3d 100644 --- a/tests/gateway/test_compression_session_id_persistence.py +++ b/tests/gateway/test_compression_session_id_persistence.py @@ -67,6 +67,13 @@ def _session_id_assignments_followed_by_save(source: str) -> list[tuple[int, boo for i, stmt in enumerate(body): if self._is_session_id_assign(stmt): results.append((stmt.lineno, self._block_has_save_after(body, i))) + # Recurse into the stmt itself when it is a control-flow node + # whose body/orelse/finalbody may carry assignments that are + # not also reachable as iter_child_nodes children of the stmt + # (e.g. an ``else`` block whose statements are all assigns). + if isinstance(stmt, (ast.If, ast.For, ast.While, ast.With, + ast.Try, ast.AsyncWith, ast.AsyncFor)): + self._walk_node(stmt) for child in ast.iter_child_nodes(stmt): if isinstance(child, (ast.If, ast.For, ast.While, ast.With, ast.Try, ast.AsyncWith, ast.AsyncFor)):