Skip to content

[ty] Fix docstring rendering for literal blocks after doctests#22676

Merged
MichaReiser merged 1 commit intomainfrom
claude/fix-issue-2497-j5GU2
Jan 23, 2026
Merged

[ty] Fix docstring rendering for literal blocks after doctests#22676
MichaReiser merged 1 commit intomainfrom
claude/fix-issue-2497-j5GU2

Conversation

@MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Jan 18, 2026

Summary

When a doctest ended on a blank line, in_doctest was not reset to false. This could cause issues when a docstring contained a doctest followed by a literal block (::) with blank lines inside it.

In that scenario, a blank line inside the literal block would incorrectly trigger the doctest-ending logic (because in_doctest was still true from the earlier doctest), prematurely closing the literal block and causing subsequent code content to be rendered as regular text with   indentation instead of being inside the code block.

Fixes astral-sh/ty#2497

Test Plan

Added test

@MichaReiser MichaReiser force-pushed the claude/fix-issue-2497-j5GU2 branch 2 times, most recently from 888b63d to 4dee0e1 Compare January 18, 2026 12:25
@MichaReiser MichaReiser marked this pull request as ready for review January 18, 2026 12:32
@MichaReiser MichaReiser marked this pull request as draft January 18, 2026 12:32
@MichaReiser MichaReiser added bug Something isn't working server Related to the LSP server ty Multi-file analysis & type inference labels Jan 18, 2026
@MichaReiser MichaReiser changed the title Fix issue 2497 [ty] Fix issue 2497 Jan 18, 2026
@MichaReiser MichaReiser changed the title [ty] Fix issue 2497 [ty] Correctly detect end of doctests Jan 19, 2026
@MichaReiser MichaReiser changed the title [ty] Correctly detect end of doctests [ty] Fix docstring rendering for literal blocks after doctests Jan 19, 2026
block_indent = 0;
in_any_code = false;
in_literal = false;
in_doctest = false;
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not a 100% sure about this change but opening a doctest only sets in_any_code and in_doctest to true. Ending a doctest should, therefore, set in_doctest and in_any_code to false.

It also seems that in_literal and in_doctest are exclusive to each other. It could make sense to use an enum over Text, Doctest, Literal with an is_code() as state variable instead, to make this clear.

@MichaReiser MichaReiser marked this pull request as ready for review January 19, 2026 14:31
@MichaReiser MichaReiser force-pushed the claude/fix-issue-2497-j5GU2 branch 2 times, most recently from 2c146e6 to d270098 Compare January 19, 2026 14:48
@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 19, 2026

Typing conformance results

No changes detected ✅

@astral-sh-bot

This comment was marked as outdated.

@MichaReiser
Copy link
Member Author

Uff, thanks RustRover for making all the formatting changes. Love it

@MichaReiser MichaReiser force-pushed the claude/fix-issue-2497-j5GU2 branch 3 times, most recently from d649ea5 to 6c2fc11 Compare January 19, 2026 15:08
When a doctest ended on a blank line, `in_doctest` was not reset to
`false`. This could cause issues when a docstring contained a doctest
followed by a literal block (::) with blank lines inside it.

In that scenario, a blank line inside the literal block would
incorrectly trigger the doctest-ending logic (because `in_doctest` was
still `true` from the earlier doctest), prematurely closing the literal
block and causing subsequent code content to be rendered as regular
text with ` ` indentation instead of being inside the code block.

Fixes astral-sh/ty#2497
@MichaReiser MichaReiser force-pushed the claude/fix-issue-2497-j5GU2 branch from 6c2fc11 to 863995f Compare January 19, 2026 17:25
@MichaReiser
Copy link
Member Author

I'll merge this. Seems easy enough to revert if it's wrong and there's a new test

@MichaReiser MichaReiser merged commit d301e67 into main Jan 23, 2026
45 checks passed
@MichaReiser MichaReiser deleted the claude/fix-issue-2497-j5GU2 branch January 23, 2026 07:57
carljm added a commit that referenced this pull request Jan 30, 2026
* main: (62 commits)
  [`refurb`] Do not add `abc.ABC` if already present (`FURB180`) (#22234)
  [ty] Add a new `assert-type-unspellable-subtype` diagnostic (#22815)
  [ty] Avoid duplicate syntax errors for `await` outside functions (#22826)
  [ty] Fix unary operator false-positive for constrained TypeVars (#22783)
  [ty] Fix binary operator false-positive for constrained TypeVars (#22782)
  [ty] Fix false-positive `unsupported-operator` for "symmetric" TypeVars (#22756)
  [`pydocstyle`] Clarify which quote styles are allowed (`D300`) (#22825)
  [ty] Use distributed versions of AND and OR on constraint sets (#22614)
  [ty] Add support for dict literals and dict() calls as default values for parameters with TypedDict types (#22161)
  Document `-` stdin convention in CLI help text (#22817)
  [ty] Make `infer_subscript_expression_types` a method on `Type` (#22731)
  [ty] Simplify `OverloadLiteral::spans` and `OverloadLiteral::parameter_span` (#22823)
  [ty] Require both `*args` and `**kwargs` when calling a `ParamSpec` callable (#22820)
  [ty] Handle tagged errors in conformance (#22746)
  Add `--color` cli option to force colored output (#22806)
  Identify notebooks by LSP didOpen instead of `.ipynb` file extension (#22810)
  [ty] Fix docstring rendering for literal blocks after doctests (#22676)
  [ty] Update salsa to fix out-of-order query validation (#22498)
  [ty] Inline cycle initial and recovery functions (#22814)
  [ty] Pass the generic context through the decorator (#22544)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spaces/tabs being rendered as HTML code in Markdown code blocks

2 participants

Comments