python
Scope
Covers the Python language and its standard toolchain: uv for environments,
dependencies and scripts; ruff for lint and format; pyproject.toml for project
metadata, dependency groups, build backend and packaging; pytest for tests;
static typing with mypy or pyright; asyncio; and the runtime traps that a type
checker and a linter both miss.
Not covered: web frameworks — FastAPI request handling, dependency injection and
response models are the fastapi skill's job; Django request handling, ORM
models and migrations are the framework's own concern. Also not covered: data
analysis and notebooks (pandas, numpy, Jupyter). Model training and fine-tuning:
use the ml-training skill; calling a hosted model: the ai-engineering skill.
For the discipline of driving code with tests rather than the pytest API, use the
test-driven-development skill. For diagnosing a specific observed failure, use
the debugging skill. For reviewing a diff, use the code-review skill.
Paths below are relative to this skill's directory.
Core rules
- Read
pyproject.tomlbefore changing anything. On an existing project its declared Python version, line length, type checker, test layout and rule set are the truth; the defaults here apply only when a field is absent or the project is new. Nothing annoys a maintainer faster than a tool swap nobody asked for. - uv is the environment manager. Mutate dependencies with
uv add/uv removeand run everything throughuv run. Hand-editing the dependency tables skips resolution and leavesuv.lockstale;source .venv/bin/activateleaves the agent and CI in different environments. uv pip installis an escape hatch for a non-project virtualenv, never a way to add a project dependency — it writes nothing topyproject.toml.- Development tooling goes in
[dependency-groups], not[project.optional-dependencies]. Groups are local-only; extras are published with the wheel, so an extra makes your linter part of your public API. - Commit
uv.lockfor applications and for anything deployed. Libraries may leave it uncommitted, but then CI has no reproducible run — decide once and say which. - A single file that needs third-party packages gets PEP 723 inline metadata
(
uv init --script, thenuv add --script), not a requirements file and not a README telling people topip installfirst. Thedependencieskey must be present even when empty, and inline metadata makesuv runignore any surrounding project. - ruff replaces flake8, black, isort, pydocstyle and pyupgrade — all of them, not
some.
ruff checkandruff formatare two separate commands; running only the first leaves the file unformatted. - Select ruff rules explicitly (
E,F,UP,B,SIM,Iis a good start) and widen a group at a time.select = ["ALL"]silently adopts every rule added in the next release, which turns a version bump into a lint failure. - Fix only the diagnostics on lines this change touched. Pre-existing violations elsewhere get reported to the user, not swept up — "while I was here" is how a two-line fix becomes an unreviewable diff.
- Never let
ruff check --fixand--unsafe-fixesrun unattended in the same pass. Apply safe fixes, read the diff, then consider unsafe ones; unsafe fixes may change behaviour by design. - Every
# noqaand every# type: ignorenames its code and its reason (# noqa: E501 — URL in docstring). A bare suppression is invisible to the next reader and to--extend-select RUF100. - Type the boundaries first: public function signatures, class attributes, and anything crossing a process or module edge. Local variables the checker can infer do not need annotations.
- A dependency that several unrelated classes satisfy is a
typing.Protocol, not a base class. Inheritance forces every implementation — including test stubs — to import your module. - Structured payloads get a
TypedDict, dataclass or Pydantic model.dict[str, Any]disables checking for everything downstream of it, which is worse than no annotation because it looks done. - Turn strictness up per module, in batches, and keep the suite green between
batches (
references/typing.md). Flipping--stricton a large repo produces thousands of errors and one giant unreviewable commit. - In
pyproject.toml,[tool.pytest]needs pytest 9.0+;[tool.pytest.ini_options]works from 6.0. Put the table under the wrong name for your pinned version and pytest ignores the whole thing — markers,filterwarningsandasyncio_modeincluded — without warning. - Fixture scope above
functionmust be immutable or reset. Asession-scoped object that tests mutate makes the suite order-dependent, which is why a file passes alone and fails in the full run. unittest.mock.patchreplaces a name in the namespace that resolves it, so the target follows the import style of the module under test:from httpx import getmeans patchingmymodule.get, whileimport httpxmeanshttpx.getis the same object either way. Mocks for awaited callables areAsyncMock, notMock; and a patch whose context exits before a background task runs asserts nothing.- Keep a strong reference to every
asyncio.create_taskresult, or create it insideasyncio.TaskGroup. The loop holds only weak references, so a fire-and-forget task can be garbage collected mid-flight. - Prefer
asyncio.TaskGroupoverasyncio.gather. When a child fails, the group cancels its siblings;gatherpropagates the first exception and leaves the rest running. Usegatheronly when you need positional results orreturn_exceptions=True. - No blocking call inside
async def— nottime.sleep, notrequests, not a synchronous DB driver, notopen().read()on a slow mount. One blocking call stalls every other task on the loop. Wrap unavoidable ones inasyncio.to_thread. - If you catch
asyncio.CancelledError, re-raise it after cleanup. Swallowing it breaksTaskGroupandasyncio.timeout, which are built on cancellation. - Finish every change with the gate:
uv run ruff format,uv run ruff check, the project's type checker on the touched paths, thenuv run pytest. Report the commands and their output, not "should be fine".
Workflows
start-or-migrate-a-project
- Classify the work: single file with dependencies → PEP 723 script; multi-file
but not distributed →
uv init; installable package →uv init --packagewith asrc/layout. The rest of this list is the package case. - Existing project? Inventory what is being replaced first:
requirements*.txt,setup.py/setup.cfg,Pipfile,poetry.lock,.flake8,[tool.black],[tool.isort],mypy.ini,tox.ini. Migration steps per source are inreferences/uv-projects.md. - Bring runtime dependencies over with
uv add -r requirements.txt(oruv addper package, reviewing each pin — an==from 2023 is a decision nobody re-made), then dev tooling withuv add --group dev ruff mypyanduv add --group test pytest pytest-cov. - Set
requires-pythonto the oldest version you actually support. ruff inferstarget-versionfrom it, so this one field decides which pyupgrade rewrites fire. - Configure ruff and the type checker (
references/ruff.md,references/typing.md), and pytest under the table name your pinned pytest version reads (rule 16, detail inreferences/pytest.md). - Delete the replaced files in the same change. Two dependency sources in one tree is the failure mode: CI installs one, the developer installs the other.
- Distributable? Choose the build backend and fill in packaging metadata via
references/packaging.md. - Gate — reproducible from a clean checkout:
uv sync --all-groups, thenuv run ruff format --check,uv run ruff check, the type checker, anduv run pytest, all from a fresh clone with no pre-existing.venv.
add-types
- Read the current state: which checker the project configures, whether this module is excluded, and how strict the surrounding modules already are.
- Annotate the boundaries — public signatures and class attributes — and let inference handle locals.
- Replace duck-typed parameters with a
Protocol, and untyped mappings with aTypedDictor dataclass (rules 13–14, patterns inreferences/typing.md). - Pick the syntax for your floor: PEP 695 (
def f[T](...),class C[T],type Alias = ...) needs Python 3.12+; below that useTypeVar. Say which you chose and why. - Remove this module from the exclusion list and raise strictness for it alone — a per-module override, not a global flag.
- Run the checker on the touched paths first for fast feedback, fix, then widen the run to the package.
- Any suppression left behind is
# type: ignore[<code>] — <reason>. If the count is growing rather than shrinking, the batch is too big: stop and split it. - Gate — narrower than before, and green: the checker passes on the touched
paths at the new strictness, the exclusion entry is gone,
uv run pyteststill passes, and every remaining ignore has a code and a reason.
write-or-fix-tests
- Confirm pytest is reading your config at all:
uv run pytest --helplists registered settings, and the startup header names theconfigfileit picked. A silently ignored table is the first thing to rule out (rule 16). - Put shared setup in a fixture in the nearest
conftest.py, and default tofunctionscope. Widen scope only for genuinely immutable or externally managed resources, and say what makes it safe. - Order-dependent failure? Run
uv run pytest -p no:randomlyversus the plain run, anduv run pytest --lf. Then look for shared mutable state: a non-function-scoped fixture, a module-level singleton, a mutable default argument, or a class attribute (references/runtime-traps.md). - Cover the same behaviour across inputs with
@pytest.mark.parametrizeandpytest.param(..., id=...); a failing case should name itself in the output. - Patch at the lookup site, use
AsyncMockfor awaited dependencies, and assert on calls (assert_called_once_with) rather than on the mock's existence (rule 18, detail inreferences/pytest.md). - Async tests need an async plugin and a declared mode; mixing
asyncio_mode = "auto"with explicit@pytest.mark.asynciois fine, missing both silently skips or errors the coroutine. - Gate — the failure is reproduced and then gone: the previously failing
invocation now passes, the full
uv run pytestpasses, and if the bug was isolation you show the ordering that used to break it.
debug-async
- Map the call path: is it fully async, or does an
async defreach a synchronous library? Mixed paths are where the loop stalls. - Grep the async functions for blocking calls —
time.sleep,requests.,subprocess.run, sync DB drivers, plain file IO on network mounts — and move them behindasyncio.to_threador an async client. - Audit task lifetime: every
create_taskresult is stored or created inside aTaskGroup; nothing awaits a coroutine it never scheduled; no coroutine is left un-awaited (ruffRUF006,ASYNCandBrules catch several of these). - Audit cancellation:
except asyncio.CancelledErrorre-raises,except Exceptiondoes not accidentally cover it (it derives fromBaseException), and cleanup lives infinally. - Replace ad-hoc timeout logic with
asyncio.timeout, and expectTimeoutErrorrather thanCancelledErrorat the boundary. - Producer/consumer pipelines: the
asyncio.Queuehas amaxsize, producer and consumers share oneTaskGroup, and on 3.13+ the producer ends the run withqueue.shutdown()(consumers stop onQueueShutDown) instead of oneNonesentinel per consumer. - Turn on
asynciodebug mode (PYTHONASYNCIODEBUG=1orasyncio.run(main(), debug=True)) to get slow-callback warnings that point at the blocking frame. Details inreferences/asyncio.md. - Gate — the symptom is explained, not just gone: name the task, the blocking frame or the swallowed cancellation, and show the run where the symptom no longer appears.
publish
- Confirm the metadata contract in
references/packaging.md:[project]fields, PyPI name versus import name,readme,license, classifiers, andpy.typedif the package ships types. - Decide the version source — static in
pyproject.toml, or derived from git tags — and make sure exactly one place owns it. - Build and inspect before uploading:
uv build, then check the wheel actually contains the package (a missingsrc/mapping produces an empty wheel that installs cleanly and imports nothing). - Rehearse on TestPyPI, install from there into a throwaway environment, and import the package.
- Publish from CI with Trusted Publishing (OIDC) rather than a long-lived API token stored in secrets.
- Gate — installable and typed:
uv run --isolated --no-project --with dist/<pkg>-<ver>-py3-none-any.whl python -c "import <pkg>"succeeds against the built artifact [verified], and the type checker sees the annotations the wheel actually ships.
Topic router
| Topic | Read when | File |
|---|---|---|
| uv projects, groups, lockfile, tools, workspaces, PEP 723 scripts, migration from pip/Poetry | Creating or migrating a project, or making a script self-contained | references/uv-projects.md |
| ruff rule selection, per-file ignores, formatter settings, the fix loop, noqa discipline | Configuring lint/format or working through a backlog of diagnostics | references/ruff.md |
Protocol, TypedDict, PEP 695 generics, narrowing, Self, override, mypy and pyright config, strictness ratchet | Adding or tightening annotations | references/typing.md |
| pytest config table versions, fixtures and scope, parametrization, isolation, mocking, async tests, plugins | Writing tests or chasing an order-dependent failure | references/pytest.md |
Task lifetime, TaskGroup, producer/consumer queues and Queue.shutdown, cancellation, timeouts, blocking-call detection, debug mode | Anything with async def in it | references/asyncio.md |
pyproject.toml metadata, src layout, build backends, versioning, py.typed, publishing | Making a package installable or releasing it | references/packaging.md |
| Mutable defaults, late-binding closures, class-attribute sharing, exception chaining, resource leaks, equality and caching traps | Reviewing or debugging code that passes lint and the type checker but misbehaves | references/runtime-traps.md |
Environment
uvis the only required tool; it provisions Python itself (uv python install 3.13), so no system interpreter orpyenvis needed. Install withcurl -LsSf https://astral.sh/uv/install.sh | sh.- ruff, mypy or pyright, and pytest are project dependencies, declared in
[dependency-groups]and invoked asuv run ruff,uv run mypy,uv run pytest. Do not rely on globally installed copies: their versions differ from the project's and their diagnostics will not match CI. uvx <tool>runs a tool without adding it to the project — useful for a one-off check, wrong for anything CI depends on.- When a command here does not exist in the project, say so and name the command the project does use, rather than substituting a global tool.


