python-stack
Python implementation guidance for project structure, type safety, dependency management, testing, and operational correctness.
SKILL.md.claude/skills/python-stack/SKILL.md
GitHubFocus areas
- Use type hints consistently (PEP 484/526/604). Prefer
str | NoneoverOptional[str]in Python 3.10+. - Structure projects with clear package boundaries:
src/layout or flat layout, consistent__init__.pyusage. - Prefer
pyproject.tomloversetup.py/setup.cfgfor project metadata and build configuration. - Use virtual environments (
venv,uv, orpoetry) — never install into the system Python. - Handle errors explicitly: catch specific exceptions, avoid bare
except:, use custom exception hierarchies when the domain warrants it.
Dependency management
- Pin dependencies for applications (lock files via
pip-compile,poetry.lock, oruv.lock). - Use version ranges for libraries.
- Prefer standard library when it covers the need adequately.
- Audit transitive dependencies when adding new packages.
Testing
- Use
pytestas the default test runner. - Prefer fixtures over setup/teardown methods.
- Use
unittest.mockorpytest-mockfor mocking — mock at boundaries, not internals. - Structure tests to mirror source layout:
tests/parallel tosrc/. - Use
parametrizefor data-driven tests rather than duplicating test functions.
Async
- Use
asynciofor I/O-bound concurrency. Do not use threads for I/O unless interacting with blocking libraries. - Prefer
async/awaitover callback patterns. - Use
asyncio.TaskGroup(3.11+) orasyncio.gatherfor concurrent tasks. - Avoid mixing sync and async code — use
asyncio.to_threadwhen calling blocking functions from async context.
Guardrails
- Avoid mutable default arguments (
def f(x=[])is a classic bug). - Avoid global mutable state.
- Prefer
dataclassesorpydanticmodels over raw dicts for structured data. - Use
pathlib.Pathoveros.pathfor file system operations. - Prefer f-strings over
%formatting or.format(). - Watch for import cycles — restructure packages rather than using lazy imports as a workaround.
- Use
loggingmodule, notprint(), for operational output. - Be explicit about encoding when reading/writing files (
open(..., encoding="utf-8")).