FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[ty] implement rendering of `.. code:: lang` in docstrings by Gankra · Pull Request #21665 · astral-sh/ruff · GitHub

Repository navigation

[ty] implement rendering of .. code:: lang in docstrings - #21665

Merged
Gankra merged 7 commits into
mainfrom
gankra/rst-more
Nov 28, 2025
Merged

Gankra merged 7 commits into
mainfrom
gankra/rst-more

Conversation

Gankra commented Nov 27, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor

Summary

We now handle:

  • .. warning:: (and friends) by bolding the line and rendering the block as normal (non-code) text
  • .. code:: (and friends) by treating it the same as :: (fully deleted if seen, introduce a code block)
  • .. code:: lang (and friends) by letting it set the language on the codefence
  • .. versionchanged:: 1.2.3 (and friends) by rendering it like warning but with the version included and italicized
  • .. dsfsdf-unknown:: (lang) by assuming it's the same as .. code:: (lang)

Test Plan

Snapshots added/updated. I also deleted a bunch of useless checks on plaintext rendering. It's important for some edge-case tests but not for the vast majority of tests.

Gankra added the server Related to the Ruff or ty LSP server label Nov 27, 2025
Gankra added the ty The ty type checker label Nov 27, 2025
Gankra changed the title implement handling of .. code:: lang in docstrings [ty] implement rendering of .. code:: lang in docstrings Nov 27, 2025

Gankra commented Nov 27, 2025

Copy link
Copy Markdown
Contributor Author

My awful son only gets worse

astral-sh-bot Bot commented Nov 27, 2025 •
edited
Loading

Copy link
Copy Markdown

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

Copy link
Copy Markdown
Member

Potentially some more sphinx specific candidates #21640

astral-sh-bot Bot commented Nov 27, 2025 •
edited
Loading

Copy link
Copy Markdown

mypy_primer results

Changes were detected when running on open source projects
scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- src/scikit_build_core/_logging.py:153:13: warning[unsupported-base] Unsupported class base with type `<class 'Mapping[str, Style]'> | <class 'Mapping[str, Divergent]'>`
- Found 42 diagnostics
+ Found 41 diagnostics

pydantic (https://github.com/pydantic/pydantic)
- pydantic/fields.py:943:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:943:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:983:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:983:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1026:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:1026:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1066:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:1066:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1109:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:1109:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1148:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:1148:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1188:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
+ pydantic/fields.py:1188:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
- pydantic/fields.py:1567:13: error[invalid-argument-type] Argument is incorrect: Expected `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`, found `Top[dict[Unknown, Unknown]] | (((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) & ~Top[dict[Unknown, Unknown]]) | None`
+ pydantic/fields.py:1567:13: error[invalid-argument-type] Argument is incorrect: Expected `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`, found `Top[dict[Unknown, Unknown]] | (((dict[str, Divergent], /) -> None) & ~Top[dict[Unknown, Unknown]]) | None`

No memory usage changes detected ✅

Copy link
Copy Markdown
Member

.. versionadded::, .. versionchanged:: and .. deprecated:: all appear a huge amount in CPython's online documentation (see https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#describing-changes-between-versions). They might also pop up in CPython docstrings as well from time to time -- not sure? CPython's docstrings aren't really meant to be ReST because they're intended to be viewed directly in a terminal, but sometimes ReST creeps in.

Gankra commented Nov 27, 2025

Copy link
Copy Markdown
Contributor Author

Comment thread crates/ty_ide/src/docstring.rs Outdated
Comment on lines +289 to +290
// Special directives that should be plaintext
#[allow(clippy::used_underscore_binding)]

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The comment suggests that it applies to the clippy allow, when it doesn't.

I suggest using a different name for _line. I assumed the variable would be unused, when, in fact, it's a tricky work around lifetimes. Maybe tmp_line or owned_line or make line a Cow?

If you really want to use _line, use expect

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

realtalk this was me getting Extremely flustered at the _special_case_temp_owned_thing idiom being flagged by clippy after 10 years of me using it. I have abandoned the underscore :(

Comment thread crates/ty_ide/src/docstring.rs Outdated
in_literal = true;
in_any_code = true;
block_indent = line_indent;
// TODO: I hope people don't have literal blocks about markdown code fence syntax

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Doesn't markdown allow you to use an arbitrary number of backticks?

A code fence is a sequence of at least three consecutive backtick characters (`) or tildes (~). (Tildes and backticks cannot be mixed.) A fenced code block begins with a code fence, preceded by up to three spaces of indentation.

So, we could just use a silly amount of backticks :) https://spec.commonmark.org/0.31.2/#fenced-code-blocks

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is genius.

"#);

assert_snapshot!(docstring.render_markdown(), @r#"
Check out this great example code:

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why do we preseve the colon here but not in literal_own_line

Gankra Nov 28, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#literal-blocks

The handling of the :: marker is smart:

  • If it occurs as a paragraph of its own, that paragraph is completely left out of the document.
  • If it is preceded by whitespace, the marker is removed.
  • If it is preceded by non-whitespace, the marker is replaced by a single colon.

When translating to markdown this amounts to "check if there's a space immediately before the ::, if there is, delete it. Otherwise, turn it into :".

Gankra enabled auto-merge (squash) November 28, 2025 13:24
Gankra merged commit 5e1b2ee into main Nov 28, 2025
40 checks passed
Gankra deleted the gankra/rst-more branch November 28, 2025 13:27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the Ruff or ty LSP server ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RST to markdown on warning directive

3 participants


Back | FazBrowse Home | New Git URL