|
| 1 | +# Contributing to Langbase Python SDK |
| 2 | + |
| 3 | +Thank you for your interest in contributing to the Langbase Python SDK! We welcome contributions from the community. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- Python 3.7 or higher |
| 10 | +- pip package manager |
| 11 | +- git |
| 12 | + |
| 13 | +### Development Setup |
| 14 | + |
| 15 | +1. **Fork and clone the repository** |
| 16 | + ```bash |
| 17 | + git clone https://github.com/langbase/langbase-python-sdk |
| 18 | + cd langbase-python-sdk |
| 19 | + ``` |
| 20 | + |
| 21 | +2. **Create a virtual environment** |
| 22 | + ```bash |
| 23 | + python3 -m venv .venv |
| 24 | + source .venv/bin/activate # On Windows: .venv\Scripts\activate |
| 25 | + ``` |
| 26 | + |
| 27 | +3. **Install the package in development mode** |
| 28 | + ```bash |
| 29 | + pip install -e . |
| 30 | + ``` |
| 31 | + |
| 32 | +4. **Install development dependencies** |
| 33 | + ```bash |
| 34 | + pip install -r requirements-dev.txt |
| 35 | + ``` |
| 36 | + |
| 37 | +5. **Install pre-commit hooks** |
| 38 | + ```bash |
| 39 | + pre-commit install |
| 40 | + ``` |
| 41 | + |
| 42 | +## Before You Commit |
| 43 | + |
| 44 | +**IMPORTANT**: All code must pass quality checks before committing. Run these commands: |
| 45 | + |
| 46 | +### 1. Format Your Code |
| 47 | +```bash |
| 48 | +# Auto-format with Black (required) |
| 49 | +black langbase/ tests/ examples/ |
| 50 | + |
| 51 | +# Sort imports with isort (required) |
| 52 | +isort langbase/ tests/ examples/ |
| 53 | +``` |
| 54 | + |
| 55 | +### 2. Run Linting Checks |
| 56 | +```bash |
| 57 | +# Run Ruff linter (auto-fixes many issues) |
| 58 | +ruff check --fix langbase/ tests/ |
| 59 | + |
| 60 | +# Check without auto-fix to see what changed |
| 61 | +ruff check langbase/ tests/ |
| 62 | +``` |
| 63 | + |
| 64 | +### 3. Type Checking |
| 65 | +```bash |
| 66 | +# Run mypy for type checking |
| 67 | +mypy langbase/ --strict |
| 68 | +``` |
| 69 | + |
| 70 | +### 4. Run Tests |
| 71 | +```bash |
| 72 | +# Run all tests |
| 73 | +pytest |
| 74 | + |
| 75 | +# Run with coverage |
| 76 | +pytest --cov=langbase |
| 77 | + |
| 78 | +# Run specific test file |
| 79 | +pytest tests/test_pipes.py |
| 80 | + |
| 81 | +# Run in verbose mode |
| 82 | +pytest -v |
| 83 | +``` |
| 84 | + |
| 85 | +### 5. Run All Checks at Once |
| 86 | +```bash |
| 87 | +# This runs all pre-commit hooks (black, isort, ruff, mypy) |
| 88 | +pre-commit run --all-files |
| 89 | +``` |
| 90 | + |
| 91 | +## Quick Checklist |
| 92 | + |
| 93 | +Before pushing your changes, ensure: |
| 94 | + |
| 95 | +- [ ] ✅ Code is formatted with `black` |
| 96 | +- [ ] ✅ Imports are sorted with `isort` |
| 97 | +- [ ] ✅ No linting errors from `ruff` |
| 98 | +- [ ] ✅ Type checking passes with `mypy` |
| 99 | +- [ ] ✅ All tests pass with `pytest` |
| 100 | +- [ ] ✅ New features have tests |
| 101 | +- [ ] ✅ New features have type hints |
| 102 | +- [ ] ✅ Documentation is updated if needed |
| 103 | + |
| 104 | +## Making Changes |
| 105 | + |
| 106 | +### 1. Create a Feature Branch |
| 107 | +```bash |
| 108 | +git checkout -b feature/your-feature-name |
| 109 | +``` |
| 110 | + |
| 111 | +### 2. Make Your Changes |
| 112 | +- Write clean, readable code |
| 113 | +- Add type hints to all functions |
| 114 | +- Follow existing code patterns |
| 115 | +- Add docstrings to public functions |
| 116 | + |
| 117 | +### 3. Add Tests |
| 118 | +- Write tests for new features |
| 119 | +- Ensure existing tests still pass |
| 120 | +- Aim for good test coverage |
| 121 | + |
| 122 | +### 4. Update Documentation |
| 123 | +- Update README.md if adding new features |
| 124 | +- Update docstrings |
| 125 | +- Add examples if applicable |
| 126 | + |
| 127 | +### 5. Commit Your Changes |
| 128 | +```bash |
| 129 | +# Stage your changes |
| 130 | +git add . |
| 131 | + |
| 132 | +# Commit with a descriptive message |
| 133 | +git commit -m "📖 DOC: Improved contribution docs" |
| 134 | +``` |
| 135 | + |
| 136 | +Follow conventional commit format: |
| 137 | +- `📦 NEW:` New feature |
| 138 | +- `🐛 BUG:` Bug fix |
| 139 | +- `📖 Docs:` Documentation changes |
| 140 | +- `👌🏻 IMP:` Improvements |
| 141 | + |
| 142 | +### 6. Push and Create PR |
| 143 | +```bash |
| 144 | +git push origin feature/your-feature-name |
| 145 | +``` |
| 146 | + |
| 147 | +Then create a Pull Request on GitHub. |
| 148 | + |
| 149 | +## Code Style Guide |
| 150 | + |
| 151 | +### Type Hints |
| 152 | +All functions should have type hints: |
| 153 | +```python |
| 154 | +def process_data(input_text: str, max_length: int = 100) -> Dict[str, Any]: |
| 155 | + """Process input text and return results.""" |
| 156 | + ... |
| 157 | +``` |
| 158 | + |
| 159 | +### Docstrings |
| 160 | +Use Google-style docstrings: |
| 161 | +```python |
| 162 | +def my_function(param1: str, param2: int) -> bool: |
| 163 | + """ |
| 164 | + Brief description of function. |
| 165 | + |
| 166 | + Args: |
| 167 | + param1: Description of param1 |
| 168 | + param2: Description of param2 |
| 169 | + |
| 170 | + Returns: |
| 171 | + Description of return value |
| 172 | + |
| 173 | + Raises: |
| 174 | + ValueError: When invalid input provided |
| 175 | + """ |
| 176 | + ... |
| 177 | +``` |
| 178 | + |
| 179 | +### Error Handling |
| 180 | +Use specific exceptions and helpful error messages: |
| 181 | +```python |
| 182 | +if not api_key: |
| 183 | + raise ValueError( |
| 184 | + "API key is required. Set LANGBASE_API_KEY environment variable " |
| 185 | + "or pass api_key parameter." |
| 186 | + ) |
| 187 | +``` |
| 188 | + |
| 189 | +## Testing Guidelines |
| 190 | + |
| 191 | +### Writing Tests |
| 192 | +- Use pytest for all tests |
| 193 | +- Use descriptive test names |
| 194 | +- Test both success and error cases |
| 195 | +- Use fixtures for common setup |
| 196 | + |
| 197 | +Example: |
| 198 | +```python |
| 199 | +def test_pipe_run_with_invalid_name_raises_error(langbase_client): |
| 200 | + """Test that running a pipe with invalid name raises appropriate error.""" |
| 201 | + with pytest.raises(NotFoundError) as exc_info: |
| 202 | + langbase_client.pipes.run(name="non-existent-pipe") |
| 203 | + |
| 204 | + assert "404" in str(exc_info.value) |
| 205 | +``` |
| 206 | + |
| 207 | +## Need Help? |
| 208 | + |
| 209 | +- Check existing issues and PRs |
| 210 | +- Read the [documentation](https://langbase.com/docs) |
| 211 | +- Ask in our [Discord community](https://discord.gg/langbase) |
| 212 | +- Open an issue for bugs or feature requests |
| 213 | + |
| 214 | +## License |
| 215 | + |
| 216 | +By contributing, you agree that your contributions will be licensed under the MIT License. |
0 commit comments