SmartRoute
SmartRoute is an instance-scoped routing engine for Python that enables dynamic method dispatch through a plugin-based architecture.
What is SmartRoute?
SmartRoute allows you to organize and dispatch method calls dynamically based on string identifiers (routes). Each object instance gets its own isolated router with independent plugin stacks, making it ideal for building modular, extensible services where behavior can be customized per-instance without global state.
What Does SmartRoute Do?
SmartRoute provides:
Dynamic method dispatch: Call methods by string name (
router.get("method_name"))Instance isolation: Instantiate routers inside
__init__withRouter(self, ...)so each object tracks its own configurationHierarchical routing: Build nested router trees with dotted path access (
root.api.get("users.list"))Plugin system: Extend behavior with composable plugins (logging, validation, etc.)
Plugin inheritance: Child routers automatically inherit parent plugins
Key Features
Instance-scoped routers - Every object gets an isolated router with its own plugin stack
Hierarchical organization - Build router trees with
attach_instance()and dotted path traversalComposable plugins - Hook into decoration and handler execution with
BasePluginPlugin inheritance - Plugins propagate automatically from parent to child routers
Flexible registration - Use
@routedecorator with prefixes, metadata, and explicit namesRuntime configuration - Configure plugins with
routedclass.configure()using target syntaxSmartAsync support - Optional integration with async execution
100% test coverage - Comprehensive test suite with 94 tests covering 898 statements
Quick Example
from smartroute import RoutedClass, Router, route
class Service(RoutedClass):
def __init__(self, label: str):
self.label = label
self.api = Router(self, name="api")
@route("api")
def describe(self):
return f"service:{self.label}"
# Each instance is isolated
first = Service("alpha")
second = Service("beta")
assert first.api.get("describe")() == "service:alpha"
assert second.api.get("describe")() == "service:beta"
Documentation Sections
Getting Started
User Guide
- Basic Usage
- Plugin Development
- Plugin Configuration
- Hierarchical Routers
- Overview
- Managing Hierarchies
- Basic Instance Attachment
- Multiple Routers: Auto-Mapping
- Multiple Routers: Explicit Mapping
- Parent with Multiple Routers
- Branch Routers
- Direct Router Hierarchies with parent_router
- Auto-Detachment
- Parent Tracking
- Plugin Inheritance
- Dotted Path Navigation
- Introspection
- Real-World Examples
- Best Practices
- Common Patterns
- Next Steps
- Best Practices
Reference
- API Reference
- Constructor and slots
- Registration and naming
- Marker discovery
- Handler table and wrapping
- Lookup and execution
- Children (instance hierarchies only)
- Child discovery helpers
- Introspection
- Hooks for subclasses
- Invariants and guarantees
BaseRouter- Internal state
- Global registry
- Attaching plugins
- Runtime flags and data
- Wrapping pipeline
- Entry/plugin application
- Inheritance behaviour
- Filtering
- Description hooks
- Data shapes
- Router Invariants
Router- Re-exports
route()RoutedClassRouter
- Plugin API Reference
Installation
pip install smartroute
For development:
git clone https://github.com/genropy/smartroute.git
cd smartroute
pip install -e ".[all]"
Next Steps
New to SmartRoute? Start with the Quick Start
Have questions? Check the FAQ for common questions and answers
Building plugins? Read the Plugin Development Guide
Need examples? Check the examples directory
Project Status
SmartRoute is currently in beta (v0.8.0). The core API is stable with complete documentation.
Test Coverage: 100% (94 tests, 898 statements)
Python Support: 3.10, 3.11, 3.12
License: MIT
Contributing
Contributions are welcome! Please open an issue or pull request on GitHub.