BUG-HUNT: [error-handling] Broad exception clause in _load_static_base #3100

Open
opened 2026-04-05 06:14:07 +00:00 by freemo · 1 comment
Owner

Metadata

  • Branch: fix/broad-exception-load-static-base
  • Commit Message: fix(tui): replace broad Exception clause with ImportError in _load_static_base
  • Milestone: (none — backlog)
  • Parent Epic: #362

Subtasks

  • Replace except Exception with except ImportError in _load_static_base (src/cleveragents/tui/permissions/screen.py, lines 27–38)
  • Add a logging.warning(...) call inside the except block to surface the error for debugging
  • Tests (Behave): Add/update scenario covering ImportError path in _load_static_base
  • Verify coverage >=97% via nox -s coverage_report
  • Run nox (all default sessions), fix any errors

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • except Exception in _load_static_base has been narrowed to except ImportError.
  • The fallback path is logged at WARNING level so unexpected import failures are visible.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.
  • All nox stages pass.
  • Coverage >= 97%.

Bug Report: [error-handling] — Broad exception clause in _load_static_base

Severity Assessment

  • Impact: Can mask unexpected errors, making it harder to debug issues.
  • Likelihood: Unlikely in a normal environment, but possible with a corrupted installation.
  • Priority: Medium

Location

  • File: cleveragents-core/src/cleveragents/tui/permissions/screen.py
  • Function/Class: _load_static_base
  • Lines: 27–38

Description

The function _load_static_base uses a broad except Exception clause to handle errors when importing textual.widgets.Static. This violates the project's fail-fast and explicit error-handling principles (CONTRIBUTING.md): exceptions should only be caught when they can be meaningfully handled, and broad catches mask unexpected failures.

Evidence

def _load_static_base() -> type[Any]:
    try:
        return importlib.import_module("textual.widgets").Static
    except Exception:  # pragma: no cover

        class _FallbackStatic:
            def __init__(self, *args: object, **kwargs: object) -> None:
                self._text = ""

            def update(self, text: str) -> None:
                self._text = text

        return _FallbackStatic

Expected Behavior

Only ImportError (and its subclass ModuleNotFoundError) should be caught, and the error should be logged at WARNING level so that unexpected failures are surfaced rather than silently swallowed.

Actual Behavior

Any exception — including AttributeError, TypeError, or other runtime errors — is silently caught and replaced with the fallback, hiding important diagnostic information.

Suggested Fix

import logging

_logger = logging.getLogger(__name__)

def _load_static_base() -> type[Any]:
    try:
        return importlib.import_module("textual.widgets").Static
    except ImportError:  # pragma: no cover
        _logger.warning(
            "textual.widgets.Static could not be imported; using _FallbackStatic.",
            exc_info=True,
        )

        class _FallbackStatic:
            def __init__(self, *args: object, **kwargs: object) -> None:
                self._text = ""

            def update(self, text: str) -> None:
                self._text = text

        return _FallbackStatic

Backlog note: This issue was discovered during autonomous operation
on milestone v3.7.0. It does not block milestone completion and has been
placed in the backlog for human review and future milestone assignment.


Automated by CleverAgents Bot
Supervisor: Bug Hunting | Agent: ca-new-issue-creator

## Metadata - **Branch**: `fix/broad-exception-load-static-base` - **Commit Message**: `fix(tui): replace broad Exception clause with ImportError in _load_static_base` - **Milestone**: *(none — backlog)* - **Parent Epic**: #362 ## Subtasks - [ ] Replace `except Exception` with `except ImportError` in `_load_static_base` (`src/cleveragents/tui/permissions/screen.py`, lines 27–38) - [ ] Add a `logging.warning(...)` call inside the except block to surface the error for debugging - [ ] Tests (Behave): Add/update scenario covering `ImportError` path in `_load_static_base` - [ ] Verify coverage >=97% via `nox -s coverage_report` - [ ] Run `nox` (all default sessions), fix any errors ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - `except Exception` in `_load_static_base` has been narrowed to `except ImportError`. - The fallback path is logged at WARNING level so unexpected import failures are visible. - A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation. - The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly. - The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. - All nox stages pass. - Coverage >= 97%. --- ## Bug Report: [error-handling] — Broad exception clause in `_load_static_base` ### Severity Assessment - **Impact**: Can mask unexpected errors, making it harder to debug issues. - **Likelihood**: Unlikely in a normal environment, but possible with a corrupted installation. - **Priority**: Medium ### Location - **File**: `cleveragents-core/src/cleveragents/tui/permissions/screen.py` - **Function/Class**: `_load_static_base` - **Lines**: 27–38 ### Description The function `_load_static_base` uses a broad `except Exception` clause to handle errors when importing `textual.widgets.Static`. This violates the project's fail-fast and explicit error-handling principles (CONTRIBUTING.md): exceptions should only be caught when they can be meaningfully handled, and broad catches mask unexpected failures. ### Evidence ```python def _load_static_base() -> type[Any]: try: return importlib.import_module("textual.widgets").Static except Exception: # pragma: no cover class _FallbackStatic: def __init__(self, *args: object, **kwargs: object) -> None: self._text = "" def update(self, text: str) -> None: self._text = text return _FallbackStatic ``` ### Expected Behavior Only `ImportError` (and its subclass `ModuleNotFoundError`) should be caught, and the error should be logged at WARNING level so that unexpected failures are surfaced rather than silently swallowed. ### Actual Behavior Any exception — including `AttributeError`, `TypeError`, or other runtime errors — is silently caught and replaced with the fallback, hiding important diagnostic information. ### Suggested Fix ```python import logging _logger = logging.getLogger(__name__) def _load_static_base() -> type[Any]: try: return importlib.import_module("textual.widgets").Static except ImportError: # pragma: no cover _logger.warning( "textual.widgets.Static could not be imported; using _FallbackStatic.", exc_info=True, ) class _FallbackStatic: def __init__(self, *args: object, **kwargs: object) -> None: self._text = "" def update(self, text: str) -> None: self._text = text return _FallbackStatic ``` > **Backlog note:** This issue was discovered during autonomous operation > on milestone v3.7.0. It does not block milestone completion and has been > placed in the backlog for human review and future milestone assignment. --- **Automated by CleverAgents Bot** Supervisor: Bug Hunting | Agent: ca-new-issue-creator
freemo added this to the v3.8.0 milestone 2026-04-05 06:32:13 +00:00
Author
Owner

Issue triaged by project owner:

  • State: Verified
  • Priority: Backlog — low-impact error-handling improvement in TUI module; unlikely to manifest in normal operation
  • Milestone: v3.8.0
  • MoSCoW: Could Have — this is a code quality improvement (narrowing broad exception to ImportError). The TUI module is not in scope until v3.7.0+, and this specific issue has low likelihood of causing real problems. Desirable but not necessary for any current milestone.
  • Parent Epic: #362 (Security & Safety Hardening)

Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: ca-project-owner

Issue triaged by project owner: - **State**: Verified - **Priority**: Backlog — low-impact error-handling improvement in TUI module; unlikely to manifest in normal operation - **Milestone**: v3.8.0 - **MoSCoW**: Could Have — this is a code quality improvement (narrowing broad exception to `ImportError`). The TUI module is not in scope until v3.7.0+, and this specific issue has low likelihood of causing real problems. Desirable but not necessary for any current milestone. - **Parent Epic**: #362 (Security & Safety Hardening) --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: ca-project-owner
freemo removed this from the v3.8.0 milestone 2026-04-07 00:19:34 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Blocks
#362 Epic: Security & Safety Hardening
cleveragents/cleveragents-core
Reference
cleveragents/cleveragents-core#3100
No description provided.