Repository navigation
fix(test.py): satisfy ty type checker on sys.stdout/stderr.reconfigure - #2544
Conversation
The Astral `ty` type checker (run via `bash lint`) doesn't honor the existing `# type: ignore[union-attr]` mypy comment and flags both `sys.stdout.reconfigure(...)` and `sys.stdout.reconfigure(...)` as unresolved-attribute errors — `reconfigure` is on `io.TextIOWrapper` but not the abstract `TextIO` type that `sys.stdout`/`sys.stderr` are typed as. Switch to `getattr(...,"reconfigure",None) + callable(...)` which is type-safe across mypy / pyright / ty without per-checker ignore comments, and remove the now-redundant `AttributeError` catch (the `callable` check makes it impossible). No behavior change — same line-buffering path, same `ValueError` handling for invalid kwargs. Co-Authored-By: Claude Opus 4.7 <[email protected]>
📝 WalkthroughWalkthrough
ChangesBuffering Configuration Safety
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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 `@test.py`:
- Around line 48-51: The try/except around the call to _reconfigure_stdout
should catch KeyboardInterrupt first and re-raise it, then handle ValueError;
update the block in test.py (the try that calls
_reconfigure_stdout(line_buffering=True)) to add an except KeyboardInterrupt:
raise before the existing except ValueError: pass, and make the identical change
for the other occurrence of the same pattern in the file (the similar try that
covers _reconfigure_stdout at the second location).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
| try: | ||
| _reconfigure_stdout(line_buffering=True) | ||
| except ValueError: | ||
| pass |
There was a problem hiding this comment.
Handle KeyboardInterrupt before other exceptions.
Both try-catch blocks violate the coding guideline requiring all try-catch blocks to handle KeyboardInterrupt before other exceptions. The guideline states: "All Python try-catch blocks must handle KeyboardInterrupt before other exceptions". This pattern is consistently followed throughout the codebase (see lines 205-210, 227-230, 257-259). As per coding guidelines, all Python try-catch blocks must handle KeyboardInterrupt before other exceptions.
🔧 Proposed fix to add KeyboardInterrupt handling
if callable(_reconfigure_stdout):
try:
_reconfigure_stdout(line_buffering=True)
+ except KeyboardInterrupt:
+ raise
except ValueError:
pass
if not sys.stderr.isatty():
_reconfigure_stderr = getattr(sys.stderr, "reconfigure", None)
if callable(_reconfigure_stderr):
try:
_reconfigure_stderr(line_buffering=True)
+ except KeyboardInterrupt:
+ raise
except ValueError:
passAlso applies to: 55-58
🤖 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 `@test.py` around lines 48 - 51, The try/except around the call to
_reconfigure_stdout should catch KeyboardInterrupt first and re-raise it, then
handle ValueError; update the block in test.py (the try that calls
_reconfigure_stdout(line_buffering=True)) to add an except KeyboardInterrupt:
raise before the existing except ValueError: pass, and make the identical change
for the other occurrence of the same pattern in the file (the similar try that
covers _reconfigure_stdout at the second location).
Summary
The Stop hook's
bash lint(which runs the Astraltytype checker) flagged two errors intest.pyleft by an earlier session:reconfigureexists onio.TextIOWrapper(the concrete class) but not on the abstractTextIOtype thatsys.stdout/sys.stderrare declared as. The existing# type: ignore[union-attr]comment is mypy-syntax and isn't honored byty.Change
Use
getattr(sys.std*, "reconfigure", None) + callable(...)— type-safe across mypy / pyright / ty without per-checker ignore comments. Drop the now-redundantAttributeErrorcatch (thecallablecheck makes it impossible).ValueErrorhandling kept for invalid kwargs at runtime.Verification
bash lint—tynow passes (diagnostics count 8 → 6, remaining 6 are pre-existingos.systemdeprecation warnings inci/scripts, unrelated)Summary by CodeRabbit