+
Skip to content

Convert dataclasses to Pydantic models in models_registry.py #101

@tunahorse

Description

@tunahorse

Summary

Convert the current dataclasses in src/tunacode/utils/models_registry.py to Pydantic models for better validation, maintainability, and error handling.

Current Anti-Patterns

The current implementation has several anti-patterns:

  • Manual type checking instead of proper validation framework
  • Silent error handling that hides problems
  • Complex parsing logic that's hard to maintain
  • Scattered hardcoded data without central management

Changes Required

1. Update Dataclasses to Pydantic Models

  • Convert ModelCapabilities, ModelCost, ModelLimits, ModelInfo, and ProviderInfo to Pydantic BaseModel classes
  • Add proper validation rules (e.g., non-negative costs, positive limits)
  • Maintain existing methods and properties

2. Update Parsing Logic

  • Simplify _parse_data() method to use Pydantic's automatic validation
  • Remove manual type checking throughout the parsing logic
  • Add proper error handling for validation failures

3. Update Fallback Data

  • Ensure hardcoded fallback data validates with new Pydantic models
  • Add validation for model costs and limits

4. Add Tests

  • Create tests for validation scenarios
  • Test error handling with malformed data
  • Test backward compatibility

Benefits

  • Automatic data validation and type coercion
  • Better error messages for invalid data
  • Cleaner, more maintainable parsing logic
  • Schema generation for documentation
  • Improved type safety throughout the codebase

Files to Modify

  • src/tunacode/utils/models_registry.py - Main conversion
  • Test files for models_registry functionality

Additional Context

Pydantic 2.11.7 is already available in the project as a dependency of pydantic-ai. This conversion would eliminate several anti-patterns in the current codebase and make the models registry more robust and maintainable.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载