Skip to content

docs: fix blog code blocks that fail the docs lint - #2587

Closed
hassaanch23 wants to merge 1 commit into
567-labs:mainfrom
hassaanch23:fix/docs-blog-code-block-lint
Closed

hassaanch23 wants to merge 1 commit into
567-labs:mainfrom
hassaanch23:fix/docs-blog-code-block-lint

Conversation

@hassaanch23

Copy link
Copy Markdown
Contributor

Why

tests/docs/test_posts.py lints every code block under docs/blog/posts with black via pytest-examples. 34 of them fail on main, which is why weekly-tests and the release job's test step are both red:

34 failed, 228 passed

This fixes 16 of the 34, taking it to 18 failed, 244 passed.

Two were genuinely broken Python

A reader copying either of these gets a SyntaxError:

bad-schemas-could-break-llms.md — missing the c:

- lass AnswerWithIntermediateCalculations(BaseModel):
+class AnswerWithIntermediateCalculations(BaseModel):

jinja-proposal.md — missing comma, so the two strings concatenate and the following dict key is a syntax error:

-        "topic": "jason and now his phone number is 123-456-7890"
+        "topic": "jason and now his phone number is 123-456-7890",
         "banned_words": ["jason"],

The rest is the formatter's own output

Once those two parse, black can format them. Everything else here is what pytest-examples' formatter produces — joined lines that now fit the width, and blank lines black adds or removes between definitions. I ran only the format step, not --update-examples: the latter also calls run_print_update, which executes the examples and fails without provider API keys (it takes the suite to 240 failures).

In jinja-proposal.md the formatter also restored #> on an output comment that had drifted to # >. #> — with the trailing space — is pytest-examples' comment_prefix; # > isn't matched as an output marker, so this is a repair.

One file deliberately left alone

introducing-structured-outputs.md is excluded. Its block has a bare #> marker standing for an empty printed line, and that form can't survive formatting: black strips the trailing space, the result no longer matches comment_prefix, and black then rewrites it to # > — which breaks the marker and leaves it inconsistent with the #> ... lines directly beneath it:

            # >          <- rewritten
            #> {"
            #> {"name

Writing # > into the docs to satisfy the linter seemed worse than leaving the block failing, so it's in the group below.

The remaining 18 need a decision I didn't want to make alone

These are published posts, and each needs a judgement about intent:

  • empty ```python blocks where install instructions appear to have been lost — multimodal-gemini.md and generating-pdf-citations.md both introduce a snippet with prose ("let's set up our environment with the necessary libraries:") and then show an empty block
  • pseudo-code fenced as python — e.g. client.create(..., response_model=type[T]) -> T in version-1.md, a type signature rather than a statement
  • fragments that were never standalone modules — e.g. a bare @field_validator in chain-of-density.md

The options are to complete the snippet, re-fence as ```text (only fences starting with py/{.py are collected by find_examples), or exclude older posts from the lint entirely. Happy to follow up with whichever you'd prefer — I didn't want to rewrite published posts on my own judgement.

Verification

$ pytest tests/docs/test_posts.py -q
before:  34 failed, 228 passed
after:   18 failed, 244 passed

No source code touched — docs only.

`tests/docs/test_posts.py` lints every code block under `docs/blog/posts` with
black via pytest-examples. 34 of them fail on `main`, which is why `weekly-tests`
and the `release` job's test step are both red.

This fixes 16 of the 34. Two were genuinely broken Python -- a reader copying
them gets a SyntaxError:

  bad-schemas-could-break-llms.md   " lass AnswerWithIntermediateCalculations"
                                    -> "class ..."  (missing the c)

  jinja-proposal.md                 "topic": "...7890"     <- no comma
                                    "banned_words": [...]
                                    two adjacent string literals, so the dict
                                    key after them is a syntax error

Once those parse, black can format them, so the rest of the change is what
pytest-examples' own formatter produces: joined lines that now fit, and blank
lines black adds or removes between definitions.

In jinja-proposal.md the formatter also restored `#> ` on an output comment that
had drifted to `# >`. `#> ` (with the trailing space) is pytest-examples'
`comment_prefix`; `# >` is not matched as an output marker.

Deliberately not touched: introducing-structured-outputs.md. Its block contains
a bare `#> ` marker standing for an empty printed line, and that form cannot
survive formatting -- black strips the trailing space, the result no longer
matches `comment_prefix`, and black then rewrites it to `# >`, which both breaks
the marker and leaves it inconsistent with the `#> ...` lines under it. Writing
`# >` into the docs to satisfy the linter seemed worse than leaving it, so it is
in the group below.

The remaining 18 need a call I did not want to make unilaterally, since they are
published posts:

  - empty ```python blocks where install instructions appear to have been lost
    (multimodal-gemini.md, generating-pdf-citations.md) -- the prose introduces
    a snippet that is not there
  - pseudo-code fenced as python, e.g.
    `client.create(..., response_model=type[T]) -> T` in version-1.md, which is
    a type signature rather than a statement
  - fragments that were never standalone modules, e.g. a bare `@field_validator`
    in chain-of-density.md

For those the options are to complete the snippet, re-fence as ```text (only
fences starting with `py`/`{.py` are collected), or exclude older posts from the
lint. Happy to follow up with whichever you prefer.
@jxnl

jxnl commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator

Consolidated into #2597 for Instructor 1.17.0: #2597 . The documentation change is included (overlapping typo edits are deduplicated), with contributor attribution retained where applicable. The combined documentation suite passes 610 checks with 89 existing skips. Closing this source PR as superseded; the consolidation PR remains draft pending full release validation and review.

@jxnl jxnl closed this Sep 5, 2026
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