Remove redundant chat cancellation handlers#1944
Conversation
There was a problem hiding this comment.
Sorry @chernistry, you have reached your weekly rate limit of 2500000 diff characters.
Please try again later or upgrade to continue using Sourcery
Sonar insights (advisory, no merge-block)Snapshot of
Run This comment is a soft signal. The Sonar scan runs on push to |
📝 WalkthroughWalkthroughThis PR removes redundant ChangesCancellation handling simplification and regression test
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
Review-bot acknowledgement summary
All must-address findings are resolved or acknowledged. |
|
bernstein doctor observe for PR #1944 ( sonar -- WARN (project bernstein)
code-scanning -- OK (23 open alert(s))
Skipped backends (credentials not configured)
See docs/observability/unified-doctor.md for backend setup notes. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/unit/core/chat/test_driver_cancellation_handlers.py`:
- Around line 22-29: The current conditional in the test only detects
CancelledError when written as an attribute (e.g., asyncio.CancelledError);
update the boolean check to accept either an ast.Attribute whose .attr ==
"CancelledError" OR an ast.Name whose .id == "CancelledError" so both
"asyncio.CancelledError" and "from asyncio import CancelledError" are matched;
modify the condition around handler.type (used in the if that tests
isinstance(handler, ast.ExceptHandler) and handler.type.attr ==
"CancelledError") to allow (isinstance(handler.type, ast.Attribute) and
handler.type.attr == "CancelledError") or (isinstance(handler.type, ast.Name)
and handler.type.id == "CancelledError").
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: f46d4086-5c07-4c63-abb7-94507a018edb
📒 Files selected for processing (4)
src/bernstein/core/chat/drivers/discord.pysrc/bernstein/core/chat/drivers/slack.pysrc/bernstein/core/chat/drivers/telegram.pytests/unit/core/chat/test_driver_cancellation_handlers.py
| if ( | ||
| isinstance(handler, ast.ExceptHandler) | ||
| and isinstance(handler.type, ast.Attribute) | ||
| and handler.type.attr == "CancelledError" | ||
| and len(handler.body) == 1 | ||
| and isinstance(handler.body[0], ast.Raise) | ||
| and handler.body[0].exc is None | ||
| ): |
There was a problem hiding this comment.
🧹 Nitpick | 🔵 Trivial | ⚡ Quick win
Handle both asyncio.CancelledError and direct CancelledError imports.
Line 24 checks isinstance(handler.type, ast.Attribute), which matches asyncio.CancelledError but would miss from asyncio import CancelledError. All three drivers currently import asyncio as a module, so the test works. For robustness against future changes, consider:
🔍 Proposed enhancement to handle both import patterns
for handler in ast.walk(tree):
+ # Match both asyncio.CancelledError (ast.Attribute) and CancelledError (ast.Name)
+ is_cancelled_error = False
+ if isinstance(handler.type, ast.Attribute) and handler.type.attr == "CancelledError":
+ is_cancelled_error = True
+ elif isinstance(handler.type, ast.Name) and handler.type.id == "CancelledError":
+ is_cancelled_error = True
+
if (
isinstance(handler, ast.ExceptHandler)
- and isinstance(handler.type, ast.Attribute)
- and handler.type.attr == "CancelledError"
+ and is_cancelled_error
and len(handler.body) == 1
and isinstance(handler.body[0], ast.Raise)
and handler.body[0].exc is None
):🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@tests/unit/core/chat/test_driver_cancellation_handlers.py` around lines 22 -
29, The current conditional in the test only detects CancelledError when written
as an attribute (e.g., asyncio.CancelledError); update the boolean check to
accept either an ast.Attribute whose .attr == "CancelledError" OR an ast.Name
whose .id == "CancelledError" so both "asyncio.CancelledError" and "from asyncio
import CancelledError" are matched; modify the condition around handler.type
(used in the if that tests isinstance(handler, ast.ExceptHandler) and
handler.type.attr == "CancelledError") to allow (isinstance(handler.type,
ast.Attribute) and handler.type.attr == "CancelledError") or
(isinstance(handler.type, ast.Name) and handler.type.id == "CancelledError").
Summary
Tests
Refs #1785
Summary by CodeRabbit
Refactor
Tests