Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change: Add validation decorators for core components #112

Open
mathysgrapotte opened this issue Feb 19, 2025 · 1 comment
Open

change: Add validation decorators for core components #112

mathysgrapotte opened this issue Feb 19, 2025 · 1 comment

Comments

@mathysgrapotte
Copy link
Owner

Is your change request related to a problem? Please describe.

Data type mismatches between pipeline stages cause cryptic failures late in execution.

Describe the solution you'd like

  1. Implement @validate_io decorator in utils/validation.py
  2. Decorate critical methods in encoders/splitters/loaders
  3. Add type checks for inputs/outputs with context-aware errors
@mathysgrapotte
Copy link
Owner Author

example implementation for context :

from functools import wraps
from typing import Type, Tuple, Optional
from stimulus.exceptions import StimulusError

def validate_input_types(*types: Type[object]):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            for i, (arg, expected_type) in enumerate(zip(args[1:], types)):  # Skip self
                if not isinstance(arg, expected_type):
                    raise StimulusError(
                        f"Invalid type for argument {i} in {func.__name__}"
                    ).add_context(
                        expected_type=expected_type.__name__,
                        actual_type=type(arg).__name__,
                        argument_index=i
                    )
            return func(*args, **kwargs)
        return wrapper
    return decorator

# Usage in encoder
class TextEncoder:
    @validate_input_types(str)
    def encode(self, text: str) -> torch.Tensor:
        return self._encoding_logic(text)

@mathysgrapotte mathysgrapotte moved this to Todo - mid issues in Stimulus v1.0 Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

1 participant