Build Python Library
Implements pip-installable Python libraries by scaffolding the project structure, building core abstractions with adapters, and verifying each module with pytest before proceeding.
Workflow
- Scaffold the project — create src layout, pyproject.toml, package structure, and py.typed marker. See SCAFFOLDING.md.
- Define the public API — write
__init__.py with explicit exports and __all__. Only types and functions users need. See SCAFFOLDING.md.
- Implement core abstractions — build protocols/ABCs, data classes, and shared types that all concrete implementations depend on. See PATTERNS.md.
- Test core abstractions — write pytest tests for core types and protocols. Run
pytest and verify zero failures before proceeding. See TESTING.md.
- Implement concrete modules — build one module at a time (proxy wrappers, provider clients, adapters). See PATTERNS.md.
- Test each module before the next — write tests for the module just implemented. Run
pytest and verify zero failures before starting the next module. See TESTING.md.
- Install and verify — run
pip install -e ".[dev]" and verify the package imports correctly from a clean script. See TESTING.md.
- Run full test suite — run
pytest -v and verify all tests pass, then run python -c "from packagename import ..." to verify public API. See TESTING.md.
Self-review checklist
Before delivering, verify ALL:
Golden rules
Hard rules. Never violate these.
- Src layout always. Every project uses
src/packagename/ structure. Never place the package at the repository root. This prevents accidental imports from the source directory instead of the installed package.
- Test before you proceed. Never implement module N+1 until module N has passing tests. Run
pytest after each module. This catches integration errors early instead of at the end.
- Explicit public API.
__init__.py must define __all__ and import only the names users need. Internal modules start with underscore (_internal.py) or live in a _private/ subdirectory. Never expose implementation details.
- Type hints on every signature. Every function parameter and return type must have a type annotation. Use
Protocol for duck-typed interfaces, not ABC unless you need shared implementation.
- Optional dependencies in extras. Third-party framework imports (langchain, chromadb, openai) must be in
[project.optional-dependencies] groups and imported lazily with try/except at usage point, not at module top level.
- Proxy delegates everything. Wrapper classes must implement
__getattr__ to forward unknown attribute access to the wrapped object. Never enumerate and re-implement every method — the proxy must work with future methods the wrapped library adds.
Reference files
| File |
Contents |
| SCAFFOLDING.md |
Project structure template, pyproject.toml template, init.py patterns, py.typed setup |
| PATTERNS.md |
Proxy/wrapper pattern, provider-agnostic interfaces, async/sync dual support, lazy imports |
| TESTING.md |
pytest configuration, fixture patterns, mocking external services, test-per-module workflow, install verification |
1---2name: build-python-library3description: Implements pip-installable Python libraries from scratch using src layout, pyproject.toml, and pytest. Use when the user asks to build, create, implement, or scaffold a Python library, package, pip package, middleware, SDK, or wrapper. Covers project scaffolding, proxy/wrapper patterns, provider-agnostic interfaces, async/sync support, and test infrastructure.4---56# Build Python Library78Implements pip-installable Python libraries by scaffolding the project structure, building core abstractions with adapters, and verifying each module with pytest before proceeding.910## Workflow11121. **Scaffold the project** — create src layout, pyproject.toml, package structure, and py.typed marker. See [SCAFFOLDING.md](SCAFFOLDING.md).132. **Define the public API** — write `__init__.py` with explicit exports and `__all__`. Only types and functions users need. See [SCAFFOLDING.md](SCAFFOLDING.md).143. **Implement core abstractions** — build protocols/ABCs, data classes, and shared types that all concrete implementations depend on. See [PATTERNS.md](PATTERNS.md).154. **Test core abstractions** — write pytest tests for core types and protocols. Run `pytest` and verify zero failures before proceeding. See [TESTING.md](TESTING.md).165. **Implement concrete modules** — build one module at a time (proxy wrappers, provider clients, adapters). See [PATTERNS.md](PATTERNS.md).176. **Test each module before the next** — write tests for the module just implemented. Run `pytest` and verify zero failures before starting the next module. See [TESTING.md](TESTING.md).187. **Install and verify** — run `pip install -e ".[dev]"` and verify the package imports correctly from a clean script. See [TESTING.md](TESTING.md).198. **Run full test suite** — run `pytest -v` and verify all tests pass, then run `python -c "from packagename import ..."` to verify public API. See [TESTING.md](TESTING.md).2021## Self-review checklist2223Before delivering, verify ALL:2425- [ ] Project uses src layout: `src/packagename/` not `packagename/` at root26- [ ] `pyproject.toml` exists with `[build-system]`, `[project]`, and `[project.optional-dependencies]`27- [ ] `py.typed` marker file exists in the package directory28- [ ] `__init__.py` has explicit `__all__` listing only public names29- [ ] Every public function and class has a type-hinted signature (all parameters and return type)30- [ ] Every module has a corresponding test file (`src/pkg/foo.py` → `tests/test_foo.py`)31- [ ] `pytest -v` passes with zero failures32- [ ] `pip install -e ".[dev]"` succeeds33- [ ] `python -c "from packagename import ..."` imports all public API names without error34- [ ] No third-party imports in core modules that lack a corresponding optional dependency group35- [ ] Proxy/wrapper classes delegate unknown attributes via `__getattr__` to the wrapped object3637## Golden rules3839Hard rules. Never violate these.40411. **Src layout always.** Every project uses `src/packagename/` structure. Never place the package at the repository root. This prevents accidental imports from the source directory instead of the installed package.422. **Test before you proceed.** Never implement module N+1 until module N has passing tests. Run `pytest` after each module. This catches integration errors early instead of at the end.433. **Explicit public API.** `__init__.py` must define `__all__` and import only the names users need. Internal modules start with underscore (`_internal.py`) or live in a `_private/` subdirectory. Never expose implementation details.444. **Type hints on every signature.** Every function parameter and return type must have a type annotation. Use `Protocol` for duck-typed interfaces, not `ABC` unless you need shared implementation.455. **Optional dependencies in extras.** Third-party framework imports (langchain, chromadb, openai) must be in `[project.optional-dependencies]` groups and imported lazily with try/except at usage point, not at module top level.466. **Proxy delegates everything.** Wrapper classes must implement `__getattr__` to forward unknown attribute access to the wrapped object. Never enumerate and re-implement every method — the proxy must work with future methods the wrapped library adds.4748## Reference files4950| File | Contents |51|------|----------|52| [SCAFFOLDING.md](SCAFFOLDING.md) | Project structure template, pyproject.toml template, __init__.py patterns, py.typed setup |53| [PATTERNS.md](PATTERNS.md) | Proxy/wrapper pattern, provider-agnostic interfaces, async/sync dual support, lazy imports |54| [TESTING.md](TESTING.md) | pytest configuration, fixture patterns, mocking external services, test-per-module workflow, install verification |