SmartSwitch is a Python library for intelligent rule-based function dispatch, combining type checking with runtime value conditions.
- Development Status: Beta (
Development Status :: 4 - Beta) - Version: 0.10.0
- Has Implementation: Yes (complete with tests and documentation)
- Coverage: 91% (156 comprehensive tests)
- Plugin System: v0.10.0 with
on_decorate()hook and metadata sharing - SmartAsync Integration: v0.10.0 with bidirectional sync/async support
SmartSwitch provides a clean, Pythonic API for:
- Handler Registry: Named function lookup and dispatch
- Plugin System: Extensible middleware for validation, logging, etc.
- SmartAsync Integration: Bidirectional sync/async handler wrapping
- Flexible Access: Decorator, get() method, and dict-like access
Key Dependencies:
smartasync>=0.5.0- Bidirectional sync/async wrappersmartseeds>=0.1.0- Smart options and kwargs extraction
The library is optimized for real-world use cases where dispatch overhead is negligible compared to actual work (API calls, database queries, business logic).
- Owner: genropy
- Repository: https://github.com/genropy/smartswitch
- Documentation: https://smartswitch.readthedocs.io (when live)
- License: MIT
smartswitch/
├── .github/workflows/ # CI/CD pipelines
│ ├── test.yml # Multi-OS tests + coverage
│ ├── docs.yml # Documentation build
│ └── publish.yml # PyPI release automation
├── docs/ # MkDocs documentation
│ ├── index.md # ✅ Complete
│ ├── installation.md # ✅ Complete
│ ├── quickstart.md # ✅ Complete
│ └── guide/basic.md # ✅ Complete
├── src/smartswitch/
│ ├── __init__.py # Package exports
│ └── core.py # Main Switcher class
├── tests/
│ ├── test_smartswitch.py # Basic tests
│ └── test_complete.py # 22 comprehensive tests
├── temp/ # Temporary files (not committed)
├── pyproject.toml # Package configuration
├── mkdocs.yml # Documentation config
├── README.md # Project overview
└── CLAUDE.md # This file
- Code, comments, and commit messages: English
- Documentation: English (primary)
- Communication with user: Italian (per user preference)
- NEVER include Claude as co-author in commits
- ALWAYS remove "Co-Authored-By: Claude noreply@anthropic.com" line
- Use conventional commit messages following project style
- All temporary files MUST be in
temp/directory temp/.gitignoreignores all files except itself- Never commit temporary files to repository
SmartSwitch follows We-Birds documentation standards:
- Test-First Documentation:
~/.genro/standards/documentation-standards.md - Architecture & Diagrams:
~/.genro/standards/documentation-architecture-standards.md
Key Requirements:
- All docs must be derived from tests (no hallucination)
- Architecture page MUST exist at
docs/appendix/architecture.md - Architecture page MUST start with Mermaid diagrams (visual-first)
- MkDocs MUST have Mermaid support and
toc.integrate - Tech reviews generate
tech_report/tech_report_NNN.md(private)
- Keep it simple: Minimal dependencies (only smartasync and smartseeds from genro-libs ecosystem)
- Performance matters: Current optimizations include signature caching, pre-compiled type checks, manual kwargs building
- Test thoroughly: Maintain 90%+ coverage (current: 91%)
- Document clearly: Every feature should have examples in docs
- Bidirectional async: Handlers work in both sync and async contexts via SmartAsync
# Run tests with coverage
pytest tests/ -v --cov=smartswitch
# Expected: 22/22 tests passed, 95% coverage# Check code style
ruff check src/smartswitch/
black --check src/smartswitch/
mypy src/smartswitch/# Build docs locally
mkdocs build
# Serve docs for preview
mkdocs serveThe project has complete CI/CD configured:
- GitHub Actions: test.yml, docs.yml, publish.yml
- Codecov: Target 90% coverage (.codecov.yml)
- Read the Docs: Auto-builds from .readthedocs.yaml
- PyPI Publishing: Automated via GitHub Actions on tag push
Codecov:
- Connect repository at https://codecov.io
- Add
CODECOV_TOKENas GitHub secret
Read the Docs:
- Import project from https://readthedocs.org
- Configuration already in
.readthedocs.yaml
✅ All core documentation is complete:
- User Guide: installation, quickstart, basic usage
- Core Features: named-handlers, api-discovery, typerules, valrules, best-practices
- Plugin System: development guide, middleware pattern, logging (v0.4.0+)
- Examples: 8+ real-world examples
- API Reference: complete with docstrings
- Architecture: comprehensive diagrams and explanations
Note: Plugin documentation was reorganized in v0.6.0 from scattered locations into dedicated docs/plugins/ directory.
- Consider adding async support for async handlers
- Add more examples for common use cases (API routing, event handling, etc.)
- Performance benchmarks documentation
When ready to release:
-
Update version:
pyproject.toml→version = "x.y.z"src/smartswitch/__init__.py→__version__ = "x.y.z"
-
Verify tests and docs:
pytest tests/ -v --cov=smartswitch mkdocs build
-
Create and push tag:
git tag -a vx.y.z -m "Release x.y.z" git push origin vx.y.z -
GitHub Actions will automatically:
- Run tests on multiple OS/Python versions
- Build package
- Publish to PyPI
Handler not registered by name: Functions decorated with typerule/valrule were not added to _spells registry, making them unretrievable by name.
- Fix: Added
self._spells[func.__name__] = funcin decorator - File:
src/smartswitch/core.py - Impact: Named handler lookup now works correctly
- Dispatch overhead: ~1-2 microseconds
- Good for: Functions doing real work (>1ms) - I/O, business logic, etc.
- Not ideal for: Ultra-fast functions (<10μs) called millions of times
- Optimizations applied:
- Signature caching (inspect.signature called once)
- Pre-compiled type checks
- Manual kwargs building (avoids bind_partial overhead)
__slots__for memory efficiency
❌ DON'T:
- Add external dependencies to core library
- Commit temporary files outside
temp/ - Include Claude as co-author in commits
- Break backward compatibility without major version bump
- Skip tests when adding features
✅ DO:
- Keep core library dependency-free
- Put temporary work files in
temp/ - Maintain high test coverage (90%+)
- Document all public APIs
- Follow semantic versioning
Author: Giovanni Porcari softwell@softwell.it License: MIT Python: 3.10+