Skip to content

Commit 8f8b4d6

Browse files
authored
docs: add CLAUDE.md orientation file and pr-workflow skill (#1672)
1 parent 0e94c25 commit 8f8b4d6

2 files changed

Lines changed: 337 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
name: pr-workflow
3+
description: Create pull requests for python-zeroconf/python-zeroconf. Use when creating PRs, submitting changes, or preparing contributions.
4+
allowed-tools: Read, Bash, Glob, Grep
5+
---
6+
7+
# python-zeroconf PR Workflow
8+
9+
When creating a pull request for `python-zeroconf/python-zeroconf`,
10+
follow these steps. Repo-wide conventions live in
11+
[CLAUDE.md](../../../CLAUDE.md); this skill summarises the parts
12+
that matter at PR-creation time.
13+
14+
## 1. Create branch from origin/master
15+
16+
The default branch is `master`, not `main`. `origin` already
17+
points at `python-zeroconf/python-zeroconf` — there is no fork in
18+
this workflow. Always re-fetch first so the branch is based on
19+
the latest `master`:
20+
21+
```bash
22+
git fetch origin
23+
git checkout -b <branch-name> origin/master
24+
```
25+
26+
If you accidentally branch from `main`, `gh pr create` will fail
27+
because the base branch does not exist.
28+
29+
## 2. There is no PR template
30+
31+
`python-zeroconf` does not ship a `.github/PULL_REQUEST_TEMPLATE.md`
32+
— PR bodies are free-form. Aim for a body that looks roughly like:
33+
34+
```
35+
## Summary
36+
<1–3 sentence prose description of what changed and why>
37+
38+
## Details
39+
<bullets explaining the non-obvious parts: RFC sections cited,
40+
performance characteristics measured, edge cases handled>
41+
42+
## Test plan
43+
- [ ] <how you verified the change>
44+
- [ ] <new tests added under tests/ — point at the file>
45+
```
46+
47+
Cite the relevant RFC section (RFC 6762 / RFC 6763) for any
48+
behaviour change that affects packet contents or timing —
49+
reviewers shouldn't have to reverse-engineer why a constant moved
50+
or a probe interval changed.
51+
52+
## 3. Commit message conventions
53+
54+
Commit messages are linted by `commitlint` with
55+
`@commitlint/config-conventional`, _and_ by `commitizen` in
56+
pre-commit (`stages: [commit-msg]`). Both must pass.
57+
58+
- **Conventional Commits prefix is required.** Pick from:
59+
`feat`, `fix`, `perf`, `refactor`, `docs`, `test`, `build`,
60+
`ci`, `chore`, `style`, `revert`. The `feat`/`fix`/`perf`
61+
prefixes show up in the release-notes; `chore*` and `ci*` are
62+
excluded by semantic-release (`exclude_commit_patterns` in
63+
`pyproject.toml`), so use those for housekeeping.
64+
- **Imperative-mood subject.** "fix: handle empty answer", not
65+
"fix: handled empty answer".
66+
- **No header length cap.** The commitlint config sets
67+
`header-max-length: [0, "always", Infinity]`, so a slightly
68+
longer subject is fine if it earns the space; don't pad.
69+
- **No `Co-Authored-By` trailers from automated agents.**
70+
- **One logical change per commit.** Let pre-commit run (ruff
71+
lint + format, mypy, flake8, codespell, cython-lint,
72+
pyupgrade). If a hook auto-fixes something, re-stage and
73+
re-commit; the commit-msg hook re-runs on the new commit.
74+
75+
## 4. Cython / `.pxd` discipline
76+
77+
If the PR touches any module listed in `TO_CYTHONIZE`
78+
(`build_ext.py`):
79+
80+
- Update the sibling `.pxd` in the same commit if you changed a
81+
`cdef class` layout or a `cpdef`/`cdef` signature.
82+
- Do not hand-edit the in-tree `.c` files; the build regenerates
83+
them, and they're excluded from sdist (`exclude = ["**/*.c"]`
84+
in `pyproject.toml`).
85+
- Verify the extension still builds locally:
86+
`REQUIRE_CYTHON=1 poetry install` (re-installs in-place,
87+
failing loudly if Cython rejects anything).
88+
- Verify it still works without the extension:
89+
`SKIP_CYTHON=1 poetry install && poetry run pytest tests/`.
90+
91+
## 5. Push and create the PR
92+
93+
```bash
94+
git push -u origin <branch-name>
95+
gh pr create --repo python-zeroconf/python-zeroconf --base master \
96+
--title "<conventional-commit subject>" \
97+
--body-file /tmp/pr-body.md
98+
```
99+
100+
Always pass the body via `--body-file`, never `--body "..."` with
101+
shell-escaping — Markdown backticks, asterisks, and angle
102+
brackets must pass through verbatim.
103+
104+
The PR title should match the commit subject (same Conventional
105+
Commits prefix). If the PR ends up squash-merged, the title
106+
becomes the merged commit message, so it has to satisfy
107+
commitlint on its own.
108+
109+
## 6. After the PR is open
110+
111+
CI runs three jobs:
112+
113+
- `lint` — `pre-commit/action`. If pre-commit passed locally
114+
this passes too.
115+
- `commitlint` — `wagoid/commitlint-github-action`. Validates
116+
every commit on the PR; if you amended after pushing, force-
117+
push the branch so the rewritten commits get linted.
118+
- `test` — the full pytest matrix across CPython 3.9–3.14,
119+
3.14t (free-threaded), and PyPy 3.9 / 3.10, on Linux + macOS +
120+
Windows. The free-threaded entry is the canary for unguarded
121+
shared-state bugs; failures there are often genuine even when
122+
the GIL-enabled rows pass.
123+
124+
CodSpeed also runs on PRs (`CodSpeedHQ/action`) and posts a
125+
benchmark delta as a check. A regression there is signal — if
126+
the PR is a perf change, the comment is the evidence; if not, a
127+
red CodSpeed check usually means the hot path picked up an extra
128+
Python-level branch and wants a second look.

‎CLAUDE.md‎

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# Notes for LLM contributors
2+
3+
A short orientation file for an LLM working in this repo. Skim
4+
before making changes; keep edits consistent with what's described
5+
here. Read [README.rst](README.rst) for the user-facing intro.
6+
7+
## What this project is
8+
9+
`python-zeroconf` is a pure-Python implementation of multicast DNS
10+
service discovery (mDNS / DNS-SD, RFC 6762 + RFC 6763). It is the
11+
mDNS engine behind Home Assistant and a long list of other Python
12+
projects that need to announce or discover services on the local
13+
network. Public API is exported from the top-level `zeroconf`
14+
package; an async API lives at `zeroconf.asyncio`.
15+
16+
There is no external protocol owner — the on-the-wire format is
17+
the mDNS / DNS-SD RFCs. Behaviour changes that affect packet
18+
contents or timing should cite the relevant RFC section.
19+
20+
Hot paths (`_cache`, `_dns`, `_history`, `_listener`,
21+
`_record_update`, `_updates`, `_protocol/{incoming,outgoing}`,
22+
`_handlers/*`, `_services/*`, `_utils/{ipaddress,time}`) are
23+
Cythonized at build time for throughput. They keep working as
24+
pure Python — `SKIP_CYTHON=1` disables the extension build — but
25+
production wheels ship compiled and CodSpeed benchmarks track that
26+
path. The authoritative list of cythonized modules lives in
27+
`build_ext.py` (`TO_CYTHONIZE`).
28+
29+
## Code style
30+
31+
- **Docstrings: terse, default to single-line.** A docstring is
32+
the function's _contract_, not its narrative. Almost every
33+
docstring should be one line — `"""Summary."""` — describing
34+
what the function does and what the caller can pass. Multi-line
35+
is the exception, only justified when there is non-obvious
36+
caller-visible behaviour the type signature and parameter names
37+
don't already convey.
38+
39+
**What does NOT belong in docstrings or comments:**
40+
- Rationale / motivation / "why we used to do X" — that's the
41+
PR description and the commit message. Git already remembers.
42+
- Cross-references to issue numbers ("closes #N", "follow-up
43+
to #M") — the PR body carries those.
44+
- Restatement of the function body in prose. If the next line
45+
of the docstring is just describing what the next line of
46+
code does, delete the docstring line.
47+
- Test docstrings retelling the production-side story. A test
48+
docstring should name what the test pins, in one sentence —
49+
not re-explain the bug, the fix, or the surrounding flow.
50+
51+
- **Comments**: same bar. Default to writing no comments. Add
52+
one only when the _why_ is non-obvious: a hidden constraint, a
53+
subtle invariant, a workaround for a specific bug, behaviour
54+
that would surprise a reader. RFC citations are useful when the
55+
reason for a timing constant or framing decision is "the spec
56+
says so" — leave those in. If removing the comment wouldn't
57+
confuse a future reader, don't write it.
58+
59+
**Don't remove existing comments** unless the code they
60+
describe is gone — the original author left them for a reason.
61+
62+
- **Don't pad commits, docstrings, or comments with cross-
63+
references** to old codepaths or issue numbers unless there's
64+
a clear reason a future reader needs that link.
65+
66+
- **Method order**: public API at the top, private helpers
67+
(`_underscore_prefixed`) at the bottom. Modules whose names
68+
start with `_` (`_cache`, `_dns`, `_handlers/`, etc.) are
69+
internal; the supported surface is what `zeroconf/__init__.py`
70+
and `zeroconf/asyncio.py` re-export.
71+
72+
- **Line length**: 110 (ruff `line-length = 110`).
73+
`requires-python = ">=3.9"`, `target-version = "py39"` for
74+
ruff; pyupgrade runs `--py39-plus`.
75+
76+
- **Imports**: ruff/isort sorted, `profile = "black"`,
77+
`known_first_party = ["zeroconf", "tests"]`. Prefer
78+
`from __future__ import annotations` so modern type syntax
79+
works on 3.9.
80+
81+
- **Generated `.c` files are not lint-targets.** `*.c` files
82+
next to each cythonized module are Cython output — never hand-
83+
edit them. They are excluded from sdist (`exclude = ["**/*.c"]`
84+
in `pyproject.toml`) and regenerated by the build.
85+
86+
## Commit / PR conventions
87+
88+
- **Conventional Commits are enforced.** CI runs commitlint with
89+
`@commitlint/config-conventional`, and pre-commit runs
90+
commitizen on the commit message. The header has no length cap
91+
(`header-max-length = [0, "always", Infinity]`), but the
92+
_type_ prefix is required: `feat:`, `fix:`, `chore:`, `ci:`,
93+
`docs:`, `refactor:`, `test:`, `perf:`, `build:`, etc.
94+
`semantic-release` excludes `chore*` and `ci*` from the
95+
changelog, so use those prefixes for housekeeping and reserve
96+
`feat`/`fix`/`perf` for user-visible changes.
97+
- **No `Co-Authored-By` trailers from automated agents.** Project
98+
preference.
99+
- Imperative-mood subject after the type prefix ("fix: handle
100+
empty answer", not "fix: handled empty answer").
101+
- There is no `.github/PULL_REQUEST_TEMPLATE.md` in this repo —
102+
the PR body is free-form. The `pr-workflow` skill (under
103+
`.claude/skills/pr-workflow/`) walks through the conventions
104+
that do apply: conventional-commit subject, RFC citations for
105+
protocol-affecting changes, a test-plan section.
106+
- Pre-commit runs ruff (lint + format), mypy, flake8, codespell,
107+
cython-lint, and pyupgrade. Run pre-commit locally before
108+
pushing; the CI `lint` job is just `pre-commit/action`, so a
109+
green local pre-commit run = a green CI lint job.
110+
111+
## Running tests
112+
113+
```bash
114+
poetry run pytest --durations=20 --timeout=60 -v tests
115+
```
116+
117+
…or `make test`, which runs the same command. Test discovery
118+
defaults from `pyproject.toml` already pass `--cov=zeroconf`
119+
and `pythonpath = ["src"]`. `pytest-asyncio` is used in the
120+
default per-test mode (no auto mode); async tests are marked
121+
explicitly with `@pytest.mark.asyncio`.
122+
123+
CodSpeed benchmarks live under `tests/benchmarks/` and run in CI
124+
through `CodSpeedHQ/action`. Ad-hoc microbenchmarks for manual
125+
profiling live under `bench/` — those don't run in CI.
126+
127+
The CI matrix includes CPython 3.9 – 3.14, the free-threaded
128+
3.14t build, and PyPy 3.9 / 3.10. Don't add anything that breaks
129+
on the free-threaded build (no module-level mutable globals
130+
mutated from multiple threads without locks; no
131+
`PyDict_Next`-style escape hatches in Cython).
132+
133+
## Build conventions
134+
135+
- **Cython is optional but expected in wheels.** `build_ext.py`
136+
cythonizes every module listed in `TO_CYTHONIZE`. The build is
137+
driven by `poetry-core` (`generate-setup-file = true`,
138+
`script = "build_ext.py"`); `BuildExt.build_extensions`
139+
swallows build failures so source installs fall back to pure
140+
Python. `SKIP_CYTHON=1` skips the Cython step entirely;
141+
`REQUIRE_CYTHON=1` re-raises so a missing extension fails the
142+
build loudly (CI wheel builds use this).
143+
- **Modules that get Cythonized ship a sibling `.pxd`** for type
144+
declarations. When changing the signature of a Cythonized
145+
function — or adding a new attribute to a `cdef class` — update
146+
the `.pxd` in the same commit, or the extension will pick up a
147+
stale declaration and the in-tree `.c` will be regenerated with
148+
the wrong layout.
149+
- Adding a new module to `TO_CYTHONIZE` is a deliberate decision:
150+
the module must be hot enough to matter, must not rely on
151+
Python-only constructs that Cython refuses (the existing
152+
`PERF401`, `PYI032`, `PYI041` ruff ignores exist because Cython
153+
rejects closures and PEP 604 unions in `cpdef`), and must stay
154+
free-threading-safe.
155+
- `compiler_directives = {"language_level": "3"}`. The build
156+
pipeline does not currently set `freethreading_compatible`,
157+
but the test matrix exercises 3.14t, so any new Cython module
158+
needs to keep working there.
159+
160+
## Useful entry points
161+
162+
| Path | What |
163+
| -------------------------------- | ------------------------------------------------------------------------------ |
164+
| `src/zeroconf/__init__.py` | Public package — re-exports `Zeroconf`, `ServiceBrowser`, `ServiceInfo`, etc. |
165+
| `src/zeroconf/asyncio.py` | Async API: `AsyncZeroconf`, `AsyncServiceBrowser`, `AsyncZeroconfServiceTypes` |
166+
| `src/zeroconf/_core.py` | `Zeroconf` core — socket setup, send/recv loop, registration/probing |
167+
| `src/zeroconf/_engine.py` | Asyncio engine driving the listener |
168+
| `src/zeroconf/_listener.py` | Cython-accelerated packet listener |
169+
| `src/zeroconf/_cache.py` | DNS record cache (Cythonized) |
170+
| `src/zeroconf/_dns.py` | DNS record / question classes (Cythonized) |
171+
| `src/zeroconf/_history.py` | Outgoing-question history for known-answer suppression |
172+
| `src/zeroconf/_record_update.py` | Record-update dataclass passed to listeners |
173+
| `src/zeroconf/_protocol/` | `DNSIncoming` / `DNSOutgoing` wire codec (Cythonized) |
174+
| `src/zeroconf/_handlers/` | Query / answer / multicast queueing (Cythonized) |
175+
| `src/zeroconf/_services/` | `ServiceBrowser`, `ServiceInfo`, `ServiceRegistry`, types |
176+
| `src/zeroconf/_updates.py` | `RecordUpdateListener` base class (Cythonized) |
177+
| `src/zeroconf/_utils/` | `ipaddress`, `time`, `net`, `name`, `asyncio` helpers |
178+
| `src/zeroconf/const.py` | Timeouts, intervals, multicast group constants |
179+
| `src/zeroconf/_exceptions.py` | Public exception hierarchy |
180+
| `tests/` | Pytest suite |
181+
| `tests/benchmarks/` | CodSpeed benchmarks |
182+
| `bench/` | Manual microbenchmarks (not run in CI) |
183+
| `build_ext.py` | `TO_CYTHONIZE` list + `poetry-core` build hook |
184+
185+
## Things not to do
186+
187+
- **Don't hand-edit the generated `.c` files** next to Cythonized
188+
modules. They are build output; modify the `.py` (and `.pxd`)
189+
and let Cython regenerate.
190+
- **Don't change a Cythonized module's `cdef class` layout or a
191+
`cpdef`/`cdef` signature without updating its `.pxd`** — the
192+
extension build will silently pick up a stale declaration and
193+
the resulting wheel will crash at import time.
194+
- **Don't add `Co-Authored-By` trailers from automated agents
195+
to commits** in this repo.
196+
- **Don't introduce a commit message that violates Conventional
197+
Commits.** The commitlint job will fail the PR.
198+
- **Don't tighten timings or constants in `const.py` without an
199+
RFC citation in the commit message.** mDNS interop with
200+
Avahi / Bonjour / Windows hinges on those numbers.
201+
- **Don't bypass `BuildExt`'s exception swallowing in
202+
`build_ext.py` without thought.** Pure-Python fallback is a
203+
feature for source installs on platforms without a compiler
204+
(and for the PyPy matrix entries, which never load the C
205+
extensions).
206+
- **Don't break the free-threaded test matrix entry (`3.14t`).**
207+
CPython 3.14t exercises this code without the GIL; module-
208+
level mutable state and unguarded cross-thread Cython attribute
209+
access will surface as flakiness there before anywhere else.

0 commit comments

Comments
 (0)