python-conventions
Python rules for tooling, glue and hook scripts: when Python at all, standard library only for hooks, uv with pyproject for projects, src layout, ruff, typed signatures, specific exceptions and unitte
SKILL.mdskills/python/python-conventions/SKILL.md
GitHubPython conventions
Python is for tooling, glue and hook scripts, not the default application language.
When to use
- Writing or changing a Python script or package.
- Setting up a Python project.
- Writing a hook that runs inside an agent harness.
Rules
When Python at all
- Reach for Python for tooling, glue and anything a library makes clearly shorter. Prefer the repository's primary language otherwise.
- A hook script is standard library only. A hook needing a dependency is the wrong shape, because it must run on every machine with no setup step.
Project layout and tooling
uvmanages the environment and the lock file. Commit bothpyproject.tomlanduv.lock.src/<package>/layout, so tests import the installed package, not the working directory.requires-pythonis set and matches what CI runs.rufffor linting and formatting. No black, isort or flake8 on top.- Dependencies get a lower bound and no upper bound unless one is known to break.
Style
- Type hints on every function signature, including
-> None. pathlib.Pathoveros.path. A dataclass over a dictionary passed around.- f-strings for formatting. No
%and no.format. - No mutable default arguments, no wildcard imports, no module level side effects on import.
- A comprehension where it stays readable, a loop where it does not.
Errors
- Raise a specific exception, never a bare
Exception. One base exception per package when callers need to catch it. - A bare
except:orexcept Exception:carries a comment naming why, and either re-raises or logs. - Never swallow an exception silently. The one documented exception is a hook script that catches everything, prints one line and exits zero on purpose.
Logging
loggingconfigured once in the entry point, never at import time in a library module.- No
printin library code. A CLI prints to stdout deliberately, diagnostics go to stderr. - Never log a secret, a token or a request body.
Tests
unittestfor a standalone script with no dependencies,pytestfor a project withuv, tests next to the package undertests/.- Parametrise instead of copying a test. Fixtures for setup, not module level state.
- A hook script gets tests that feed it a payload on stdin and assert the decision, with no network and no git repository required unless the hook needs one.
License header
- Every source file carries the license header after the shebang, matching the repository's license.
Guardrails
- Python justified over the repository's primary language.
- Hook scripts standard library only, project scripts managed by
uv. ruff checkandruff formatclean, type hints complete.- Specific exceptions used, nothing swallowed silently outside the hook pattern.
- Logging configured in the entry point, no secret ever logged.
- Tests cover behaviour, named for the behaviour, deterministic.
References
lintfor runningruffas part of the repository's checks.testing-conventionsfor when a test is written, what earns one and test form.