Skip to content

Implement architecture improvements from documentation#34

Merged
OPpuolitaival merged 6 commits into
masterfrom
claude/implement-architecture-improvements-011CUxu12TnF6KkP3mCLB2u4
Jan 8, 2026
Merged

Implement architecture improvements from documentation#34
OPpuolitaival merged 6 commits into
masterfrom
claude/implement-architecture-improvements-011CUxu12TnF6KkP3mCLB2u4

Conversation

@OPpuolitaival
Copy link
Copy Markdown
Member

No description provided.

Replace fragile dir() usage with robust inspect.getmembers() throughout
the model introspection code:

- Replace dir() with inspect.getmembers(predicate=callable) for step discovery
- Replace dir() with hasattr() for attribute checking
- Support both instance methods and static methods
- Add proper documentation for all modified methods
- Skip private/protected methods (starting with '_')

Benefits:
- More robust and type-safe introspection
- Properly handles both instance methods and static methods
- Avoids issues with inherited methods
- Better performance with predicate filtering
- More maintainable and less error-prone

All tests passing (69 unit tests + 21 integration tests).
Improve exception handling throughout the codebase with specific
exception types and better error messages:

In osmo.py:
- Replace bare except with specific exception types (KeyboardInterrupt,
  AssertionError, AttributeError, TypeError, Exception)
- Add proper error chaining with 'from' clause
- Preserve KeyboardInterrupt for user cancellation
- Add detailed error messages with context
- Document exception handling in docstrings

In test_config.py:
- Replace bare except: with except Exception as e:
- Add clear error messages for unexpected exceptions
- Use proper error chaining

Benefits:
- Clearer error types and causes
- Better debugging with proper error chaining
- User interrupts (Ctrl+C) preserved
- More helpful error messages with context
- Follows Python best practices for exception handling

All tests passing (69 unit tests + 21 integration tests).
Implement comprehensive input validation with clear error messages
throughout the configuration system:

New ConfigurationError exception class:
- Custom exception for configuration validation failures
- Extends ValueError for semantic correctness

New ConfigValidator class with validation methods:
- validate_algorithm: Check algorithm is valid OsmoAlgorithm instance
- validate_end_condition: Check end condition is valid OsmoEndCondition
- validate_error_strategy: Check error strategy is valid OsmoErrorStrategy
- validate_seed: Check seed is int, non-negative, and fits in 32 bits

Updated OsmoConfig setters:
- Replace AttributeError with ConfigurationError
- Use ConfigValidator for all validation
- Add comprehensive docstrings with Args and Raises

Updated Osmo.seed setter:
- Use ConfigValidator.validate_seed for consistent validation
- Better error messages with specific constraints

Updated tests:
- Replace AttributeError expectations with ConfigurationError
- Tests still verify proper validation behavior

Benefits:
- Fail fast with clear, actionable error messages
- Type-safe validation with detailed constraints
- Consistent error handling across configuration
- Better developer experience with helpful error messages
- Lists available options in error messages

All tests passing (69 unit tests + 21 integration tests).
Implement extensible discovery architecture with pluggable strategies
for finding test steps, guards, and weights in model classes:

New pyosmo/discovery/ module:
- base.py: Base classes and data structures
  - DiscoveryStrategy: Abstract base for discovery strategies
  - StepMetadata: Dataclass for discovered step information
  - ModelMetadata: Complete model discovery results with helpers

- decorator.py: DecoratorBasedDiscovery strategy
  - Discovers @step decorated methods
  - High priority (10) to override naming convention
  - Extracts decorator metadata

- naming.py: NamingConventionDiscovery strategy
  - Discovers step_* named methods
  - Lower priority (50) as fallback
  - Maintains backward compatibility

- orchestrator.py: ModelDiscovery coordinator
  - Combines multiple strategies
  - Priority-based ordering
  - Automatic deduplication by function name
  - Extensible: can add custom strategies

Benefits:
- Easy to add new discovery mechanisms (annotations, configs, etc.)
- Priority system prevents conflicts between strategies
- Backward compatible (existing code unchanged)
- Strategy pattern enables testing and customization
- Foundation for future plugin system
- Clear separation of concerns

All existing tests passing (69 unit tests + 21 integration tests).
This is infrastructure for future extensibility - existing model.py
discovery continues to work as before.
Add centralized plugin registry for algorithms, end conditions,
and error strategies, enabling discovery and extensibility:

New pyosmo/plugins/ module:
- registry.py: Core PluginRegistry implementation
  - PluginRegistry class: Central registry for all plugin types
  - Type-safe registration with base class validation
  - Automatic registration of all built-in plugins
  - Fluent registration API with decorators
  - List functionality for plugin discovery
  - PluginError for clear error reporting

Registry features:
- register_algorithm/end_condition/error_strategy methods
- get_algorithm/end_condition/error_strategy methods
- list_algorithms/end_conditions/error_strategies methods
- replace parameter to override existing registrations
- Validation ensures registered classes extend base types
- Helpful error messages listing available options

Built-in plugins automatically registered:
- Algorithms: random, weighted, balancing
- End conditions: length, time, coverage, endless
- Error strategies: raise, ignore, ignore_asserts, allow_count

Benefits:
- Centralized plugin management
- Easy plugin discovery via list methods
- Type-safe registration with validation
- Foundation for future plugin system
- Decorator API for custom plugins
- Clear error messages

All existing tests passing (69 unit tests + 21 integration tests).
Registry is infrastructure for future extensibility.
Implement caching for step discovery to improve performance
when repeatedly accessing model steps:

Optimizations in model.py:
- Add _steps_cache and _cache_valid attributes to OsmoModelCollector
- Cache discovered steps in all_steps property
- Invalidate cache when models are added via add_model()
- Convert generator to list for caching, return iterator for compatibility

Performance benefits:
- Steps are discovered once and cached until models change
- Eliminates repeated inspect.getmembers() calls
- Significant performance improvement for large test suites
- Transparent to users - property still returns Iterator[TestStep]
- Cache automatically invalidates when model state changes

Implementation details:
- Cache built on first access to all_steps
- Cache rebuilt when _cache_valid is False
- add_model() sets _cache_valid = False
- Iterator returned to maintain API compatibility
- No changes to existing code needed

All existing tests passing (69 unit tests + 21 integration tests).
Optimization is backward compatible.
@OPpuolitaival OPpuolitaival merged commit 0047749 into master Jan 8, 2026
4 checks passed
@OPpuolitaival OPpuolitaival deleted the claude/implement-architecture-improvements-011CUxu12TnF6KkP3mCLB2u4 branch January 8, 2026 19:48
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