Skip to content

Fix exception handling and validator anti-patterns#92

Merged
Miyamura80 merged 1 commit intomainfrom
claude/fix-todo-issues-PWUug
Jan 27, 2026
Merged

Fix exception handling and validator anti-patterns#92
Miyamura80 merged 1 commit intomainfrom
claude/fix-todo-issues-PWUug

Conversation

@Miyamura80
Copy link
Copy Markdown
Owner

Summary

Addresses five high-priority code quality issues: removes overly broad exception handling, fixes Pydantic validator anti-patterns, eliminates circular import risk, and improves exception re-instantiation safety.

Changes

  • Remove circular import in common/flags.py: Deleted setup_logging() call at import time to prevent circular dependency issues
  • Fix validator anti-pattern in common/global_config.py: Replaced @field_validator decorators that ignored input parameter v with default_factory lambdas for is_local and running_on fields
  • Narrow exception handling in src/utils/logging_config.py: Changed broad Exception catch to specific TypeError when re-instantiating exceptions; improved fallback message formatting
  • Narrow exception handling in utils/llm/dspy_langfuse.py:
    • Line 280: Changed Exception to (ValueError, KeyError, TypeError) for cost calculation errors
    • Line 321: Changed Exception to (TypeError, ValueError, ZeroDivisionError, AttributeError) for token calculation errors
    • Line 438: Changed Exception to (TypeError, AttributeError, RecursionError) for output serialization errors
  • Update TODO.md: Marked all five high-priority items as completed

Testing

  • Tests pass (make test)
  • Linting passes (make ci)

Related Issues

Closes #

- Replace broad Exception catches with specific exception types
- Fix unsafe exception re-instantiation in logging_config.py
- Replace validator anti-pattern with default_factory in global_config.py
- Remove setup_logging() call at import time in flags.py to avoid circular imports

https://claude.ai/code/session_01NXo5yv7Qpp2HxBe6APW1Go
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Jan 27, 2026

Greptile Overview

Greptile Summary

This PR systematically addresses five high-priority code quality issues by removing overly broad exception handling, fixing Pydantic validator anti-patterns, and eliminating a circular import risk.

Key improvements:

  • Removed setup_logging() call at import time in common/flags.py to prevent circular dependency with common.global_config (which imports from src.utils.logging_config, which in turn imports global_config)
  • Fixed validator anti-pattern in common/global_config.py by replacing @field_validator decorators that ignored their input parameter v with proper default_factory lambdas for runtime environment fields
  • Narrowed exception handling in src/utils/logging_config.py from catching all Exception to specific TypeError when re-instantiating exceptions, with improved fallback message formatting
  • Narrowed three broad exception handlers in utils/llm/dspy_langfuse.py to catch only specific exception types that can realistically occur in each context (cost calculation, token calculation, output serialization)

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • All changes are focused refactoring improvements that address specific code quality issues without altering functionality. The circular import fix prevents potential initialization issues, the validator fix uses proper Pydantic patterns, and the narrowed exception handling improves code clarity while maintaining error recovery behavior.
  • No files require special attention

Important Files Changed

Filename Overview
TODO.md Updated checklist to mark five high-priority code quality items as completed
common/flags.py Removed setup_logging() call at import time to prevent circular dependency with common.global_config
common/global_config.py Replaced @field_validator decorators with default_factory lambdas for is_local and running_on fields to fix validator anti-pattern
src/utils/logging_config.py Narrowed exception handling from Exception to TypeError and improved fallback message with exception type prefix
utils/llm/dspy_langfuse.py Narrowed three broad exception handlers to specific exception types for cost calculation, token calculation, and output serialization

@Miyamura80 Miyamura80 merged commit 3e22fe1 into main Jan 27, 2026
11 checks passed
@github-actions github-actions Bot deleted the claude/fix-todo-issues-PWUug branch January 27, 2026 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants