docs: set PCAPKIT_SPHINX before importing pcapkit, not after - #520
Conversation
`pcapkit/utilities/logging.py:73` computes `SPHINX_TYPE_CHECKING` from
`os.environ` at *import* time, and `conf.py` imported `pcapkit` six lines before
setting the variable. Since importing `pcapkit` already pulls in the schema
modules, every `if SPHINX_TYPE_CHECKING:` TypedDict was left undefined and
autodoc could not document it.
- move the assignment above `import pcapkit`, with a comment saying why the
order is load-bearing
Measured both orderings against `pcapkit.protocols.schema.application.httpv2`,
whose `FrameFlags` sits under that guard at line 36:
set after import -> SPHINX_TYPE_CHECKING = False, has FrameFlags = False
set before import -> SPHINX_TYPE_CHECKING = True, has FrameFlags = True
This clears 96 `autodoc: failed to import` warnings and restores 89 documented
members to the built HTML. All 89 names exist; none were stale.
Cross-review: GOOD TO GOReviewed at head The circular-import objection is falsified, four independent waysThis was the reason to doubt the change — that the post-import placement of
Confirmed
Two things this PR's description gets wrong, and I am correcting them1. The warning totals are not reproducible. The description cites 1164 → 1057 with ~342 2. It is not "0 new problems". The reviewer reconciled its 156 → 83 delta exactly by category: 109 resolved (96 The 36 new ones are worth understanding rather than ignoring: making these TypedDicts visible to autodoc for the first time means they now collide by bare name with pre-existing identically-named classes elsewhere — @JarryShaw — the naming collision is the one judgement call here. Net 73 fewer warnings and 96 previously-undocumented TypedDicts now rendering is clearly the better state, but if you would rather the duplicates were disambiguated first (via |
|
✅ GOOD TO MERGE, one judgement call for you — fixes 96 autodoc import failures (96 → 0) but surfaces 33 pre-existing |
#520 made the `if SPHINX_TYPE_CHECKING:` TypedDicts importable during the build, which exposed that four of them are documented twice on the same page -- once under "Header Schemas" beside the option they belong to, and again in the "Type Stubs" appendix. Before #520 both occurrences failed to import, so nothing collided. - add `:no-index:` to the appendix copy of `RPLFlags`, `MPLFlags` and `DFFFlags` in `hopopt.rst` and `ipv6_opts.rst`, and of `Flags` in `tcp.rst`, keeping the inline "Header Schemas" occurrence canonical Measured with sphinx-build 9.1.0: 83 warnings -> 50, `duplicate object description` 36 -> 3, `failed to import` stays 0, no new warning appears. The 3 remaining duplicates pre-date #520 and are untouched. Also repairs cross references, which resolved to the appendix via `#id0` rather than to the canonical name anchor.
One line, 96 warnings, 89 documented members restored to the built HTML.
The defect
pcapkit/utilities/logging.py:73computes the flag at import time:docs/source/conf.pydidimport pcapkitat line 23 and only setos.environ['PCAPKIT_SPHINX'] = '1'at line 29. Importingpcapkitalready pulls in the schema modules, so everyif SPHINX_TYPE_CHECKING:TypedDict was never defined — and the.rstpages that autodoc them emittedautodoc: failed to import ….Measured, both orderings
Against
pcapkit/protocols/schema/application/httpv2.py, whoseFrameFlagssits under that guard at line 36:And re-executing
conf.py's own import prologue with the fix applied:Scope
14 modules carry
if SPHINX_TYPE_CHECKING:, holding roughly 87 TypedDict classes between them —FrameFlags,StreamID, the 40+*MessageFlagsinschema/internet/mh.py,TCIType,EPBFlagsand the rest. All of those names exist; none were stale, so this is purely a build-configuration defect rather than a documentation gap.Alongside the 96
failed to importwarnings, this also resolves the 2 genuinesphinx_autodoc_typehints.forward_referencewarnings forTCITypeandLocator, which had the same cause.Why a comment rather than just a moved line
The ordering is load-bearing and nothing said so — the previous arrangement reads as deliberate, with the assignment grouped next to the other module-level setup. The comment records that
logging.pyreads the variable at import time, so the next person tidying the imports does not undo it.Not in scope
This does not touch the remaining nitpicky warnings. For context on why the raw count is not a defect count: of 1164 total warnings on a nitpicky build, 342 are
aenumtargets that are unresolvable by construction —conf.py:92-105already documents whyaenumis excluded fromintersphinx_mapping, since itsobjects.invcarries zeropy:objects — and a further 43 arepcap/pcapfiletargets for optional dependencies not installed locally. The genuinely broken repository references number 22 and are tracked separately.