-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Improve Type Annotations and Static Type Checking
Problem
The codebase currently has several areas where type hints could be improved to enhance code quality, IDE support, and catch potential bugs at development time. The current type hints are basic and could be more specific.
Areas Needing Improvement
-
Return Types
- Many functions return
Dict[str, Any]orList[Any]which could be more specific - Return types like
Callableshould usetyping.Callablewith proper parameter and return type annotations
- Many functions return
-
Missing Type Parameters
- Generic types like
List,Dict,Optional, etc. should use square bracket notation (e.g.,List[str]instead of justlist)
- Generic types like
-
Model Adapters
- The ModelAdapter base class and its implementations could benefit from more specific type hints
- Return types for methods like generate() could be more precise
-
Configuration
- Configuration dictionaries could be replaced with proper
TypedDictor dataclasses - Environment variable parsing could be more type-safe
- Configuration dictionaries could be replaced with proper
-
Async Functions
- Async functions should use
typing.Awaitablewhere appropriate - Return types should properly reflect async nature (e.g.,
-> Awaitable[Dict[str, Any]])
- Async functions should use
Proposed Changes
- Add a
py.typedfile to mark the package as type-checked - Add
mypyorpyrightconfiguration - Add type stubs for third-party libraries if needed
- Convert configuration dictionaries to dataclasses or TypedDict
- Add proper return type annotations to all public APIs
- Add type checking to CI/CD pipeline
Example Improvements
# Before
def get_model_adapter(model_name: str, config=None):
# ...
# After
from typing import Dict, Optional, Type, TypeVar, Union
from typing_extensions import Protocol
T = TypeVar('T', bound=ModelAdapter)
class ModelConfig(Protocol):
api_key: Optional[str]
timeout: int
# Other common config options
def get_model_adapter(
model_name: str,
config: Optional[Dict[str, Any]] = None
) -> ModelAdapter:
# ...Resources:
- https://google.github.io/styleguide/pyguide.html#3192-typing-imports
- https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html
- https://typing-extensions.readthedocs.io/en/latest/#
- https://dropbox.tech/application/our-journey-to-type-checking-4-million-lines-of-python
- https://realpython.com/python-type-checking/
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels