|
| 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