Skip to content

chore(lint): flag new rST markup in non-rendered docstrings - #21010

Closed
dd-apm-ecosystems[bot] wants to merge 2 commits into
mainfrom
automated/lint-rst-in-private-docstrings-2026-10-14
Closed

dd-apm-ecosystems[bot] wants to merge 2 commits into
mainfrom
automated/lint-rst-in-private-docstrings-2026-10-14

Conversation

@dd-apm-ecosystems

Copy link
Copy Markdown

Description

Observed in 1 of the ~100+ human-authored PRs reviewed over the past 30 days (PR #19821, reviewed by the automated reviewer chatgpt-codex-connector[bot], with the fix accepted by the human PR author/maintainer litianningdatadog): a newly added private-module inline comment used reStructuredText double-backtick literal markup ( add_*_metric ) even though the comment lives in a non-Sphinx-rendered file. The author replied "Fixed in afe7423: the comment now reads plain add_*_metric" — a human maintainer explicitly acting on the bot's suggestion by pushing a follow-up commit, which is the acceptance bar this mission requires for bot-originated feedback.

This is not a one-off false alarm: the same chatgpt-codex-connector[bot] review comment pattern ("Use plain prose in the private ... docstring" / "Remove rST markup from the private ... docstring") recurred across at least 15 additional PRs in the same 30-day window (e.g. #20671, #20892, #20917, #20763, #20773, #20615, #20721, #20688, #20716, #20611, #20589, #19771, #20772, #20700, #20935), each time citing the same documented convention in AGENTS.md ("Docstrings and Comments": only docstrings Sphinx renders via a docs/*.rst .. automodule:: directive use reStructuredText; everything else — private modules, .pyi stubs, tests, inline comments — is plain prose). Several of those additional occurrences were likewise fixed by the human PR author in a follow-up commit (e.g. #20892: "Fixed in a3b2181" / "Fixed in 812c585" by a MEMBER; #20917: "Fixed in c50379a"; #20763: "Fixed in a1f0d8e"; #20615: "Fixed in d69c01c"). A single confirmed, maintainer-accepted, mechanically-detectable occurrence would already be sufficient justification per this mission's guardrails — the recurrence across many more PRs in-window is additional supporting evidence that this pattern is both real and worth automating, and likely recurs outside this window too.

This is a strong automation candidate: it is purely syntactic (a regex over comment/docstring text, no business logic), has a very low false-positive rate when scoped correctly (see Risks), and is already being caught reliably by a pattern-based AI reviewer — itself a signal that the rule is mechanically well-defined.

No existing lint/CI check covers this: ruff's pydocstyle rules (pyproject.toml) check docstring presence/structure, not rST-vs-prose markup content, and there is no flake8/pylint rule for it either. Rather than introduce a new general-purpose tool, this PR follows the repository's own precedent for this exact kind of policy check — scripts/check_no_new_aidev_anchors.py + .github/workflows/check-no-new-aidev-anchors.yml (a small, diff-scoped Python script wired into a PR-only GitHub Actions workflow) — and adds a sibling script/workflow pair built the same way, since that is the most idiomatic convention already established in this repo for "new-code-only, mechanically-detectable policy" checks.

Testing

  • Added tests/internal/test_check_no_rst_in_private_docstrings.py, mirroring the existing tests/internal/test_check_no_new_aidev_anchors.py test style, covering: rendered-module detection via real docs/*.rst .. automodule:: directives, .pyi stubs always being treated as non-rendered, the double-backtick regex (including that it does not match 3+ backtick Markdown fences), comment/docstring span extraction via tokenize, and that only added lines are flagged (a line with a pre-existing violation is not flagged if untouched by the diff).
  • Ran the checker script directly against synthetic diffs in a sandbox checkout of this repo:
    • A new file with """Does ``x`` thing.""" in a private module (ddtrace/internal/...) → correctly flagged.
    • The same content added to ddtrace/contrib/bottle.py (a Sphinx-automodule'd file) → correctly NOT flagged.
    • A pre-existing violation in a file that also gets an unrelated new line added elsewhere → correctly NOT flagged (proves diff-scoping works, not just file-scoping).
    • Ran against the real repository's full tests/*.py tree: confirmed there are ~1171 pre-existing double-backtick occurrences across 210 files today — all of which are correctly ignored by this checker because it only inspects lines added in a diff, confirming it will not break on existing code.
  • Ran ruff format --check and ruff check against both new Python files: clean.

Risks

  • False positives: Low, but not zero. The checker currently treats a module as "rendered" only when a docs/*.rst file names it directly via .. automodule::. A small number of public classes are documented via .. autoclass::/.. autofunction:: instead, while their code physically lives in a private-looking file — e.g. ddtrace.trace.Tracer (defined in ddtrace/_trace/tracer.py), ddtrace.trace.Span, and ddtrace.propagation.http.HTTPPropagator (all in docs/api.rst). A PR that adds correct rST to one of those specific docstrings would be incorrectly flagged. This does not affect any existing code (only new/changed lines are checked), and is a narrow, enumerable edge case; a reasonable follow-up would teach the script to also resolve autoclass/autofunction targets back to their defining file. Noted here rather than silently fixed, per the instruction to keep this change scoped and reviewable.
  • Scope: Enforcement is strictly diff-scoped — only lines added in a PR vs. its merge-base with the PR's base branch are inspected, exactly like the existing check-no-new-aidev-anchors check. It cannot fail on pre-existing code, so there is no risk of blocking unrelated PRs or requiring a repo-wide cleanup.
  • Rollback: Revert the three added files (script, test, and — once a maintainer adds it, see Additional Notes — the workflow). Since the check only runs as its own required PR check, reverting is a clean, isolated operation with no interaction with other CI jobs.

Additional Notes

Local verification: python scripts/check_no_rst_in_private_docstrings.py --base-ref origin/main from a repo checkout with the target branch checked out. The script requires no extra dependencies beyond the stdlib (tokenize, re, subprocess, argparse).

CI integration — IMPORTANT, needs a maintainer action: This automation's GitHub App integration does not hold the workflows permission scope, so it could not create .github/workflows/check-no-rst-in-private-docstrings.yml directly (GitHub rejects any Contents-API write under .github/workflows/ without that scope, 403 "Resource not accessible by integration"). A maintainer needs to add the following file by hand (copy-paste, no changes needed) for this check to actually run in CI; until then, this PR only adds the script and its tests, and the check is not yet wired into the merge-blocking pipeline:

name: check-no-rst-in-private-docstrings
on:
  # PR-only: this inspects the diff against the PR base, mirroring the
  # check-no-new-aidev-anchors workflow. No paths filter: a new violation
  # can land in any .py/.pyi file.
  pull_request:

permissions:
  contents: read

jobs:
  check-no-rst-in-private-docstrings:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
          # Full history so merge-base against the PR base cannot miss a
          # common ancestor (same reason the aidev-anchors check does this).
          fetch-depth: 0
      - uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
        with:
          python-version: "3.12"
      - name: Fetch PR base
        run: git fetch origin "${{ github.base_ref }}"
      - name: Check for new rST markup in non-rendered docstrings/comments
        run: python scripts/check_no_rst_in_private_docstrings.py --base-ref "origin/${{ github.base_ref }}"

This mirrors .github/workflows/check-no-new-aidev-anchors.yml exactly (same checkout/setup-python pins, same pull_request-only trigger, same fetch-depth: 0 for a reliable merge-base) — only the job name and the invoked script differ.

Citations: The accepted bot-originated finding that motivated this change is #19821 (comment) (chatgpt-codex-connector[bot]) with the maintainer's accepting reply at the same thread ("Fixed in afe7423..."). Additional same-pattern, maintainer-accepted occurrences in-window include PR #20892 (#20892 (comment) and #discussion_r4272725723, fixed by MEMBER elihschiff), #20917 (#20917 (comment), fixed by Yun-Kim), #20763 (#20763 (comment), fixed by Yun-Kim), and #20615 (#20615 (comment), fixed by MEMBER gnufede).

This change has no customer-facing effect (CI-only), so it does not need a release note; please apply the changelog/no-changelog label.

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against main using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

scripts/check_no_rst_in_private_docstrings.py                           @DataDog/python-guild
tests/internal/test_check_no_rst_in_private_docstrings.py               @DataDog/apm-core-python

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

There are 163 dependency direction violations that already exist on the base branch and have not been changed by this PR.

Show existing violations (showing 5 of 163 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=121)
ddtrace.llmobs._integrations.vertexai -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=119)
ddtrace.llmobs._integrations.vllm -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=119)
ddtrace.internal.test_visibility.api -×-> ddtrace.trace  (product:ci_visibility -> product:tracing, score=119)
ddtrace.internal.ci_visibility.git_client -×-> ddtrace.trace  (product:ci_visibility -> product:tracing, score=119)

To see all violations, download the layers-base.json and layers-pr.json artifacts from this CI job and run:

uv run --script scripts/import-analysis/layers.py compare layers-base.json layers-pr.json

@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Pipelines  Tests

❌ Errors

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 1 Pipeline job failed

Changelog | Validate changelog

View more details · View in GitHub Actions

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 804a915 | Docs | View more details | Give us feedback!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be either a CI job too or at least integrated in one? 🤨

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general question: who owns these scripts? Like, sure, they seem cool and all, but who maintains them? I'm slightly concerned that we may soon be adding a deluge of bespoke, "hand"-rolled scripts where better options exist (proper static analysis/AST Grep/etc.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me add a skill to encourage proper placement of these rules?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants