« Back to Search  |  Reader Mode  |  Switch to Full Retro Site View Links: Stay in Reader  |  Retro Mode: ON  |  Original »
GitHub · Extracted in 1446 ms

🔧 Update pre-commit hooks (ruff 0.16.6, mypy 2.3.1) (#424) · executablebooks/markdown-it-py@d9b0526

Source: https://github.com/executablebooks/markdown-it-py/commit/d9b0526122d793a2157be689633114525a761e4e

Repository navigation

Commit d9b0526


Browse files
chrisjsewell authored 🔧 Update pre-commit hooks (ruff 0.16.6, mypy 2.3.1) (#424 ) ## Summary A fresh `pre-commit autoupdate` on current master, replacing #396 (the bot's May branch), whose CI was red because the ruff autofix had inserted an unguarded `from typing_extensions import Self` into `markdown_it/tree.py` while `typing_extensions` is not a runtime dependency. | hook | before | after | |---|---|---| | `astral-sh/ruff-pre-commit` | v0.15.12 | v0.16.6 | | `pre-commit/mirrors-mypy` | v1.20.2 | v2.3.1 | | `pre-commit/pre-commit-hooks` | v6.0.0 | v6.0.0 (unchanged) | Two commits: the hook bump plus the hooks' own auto-fixes, then the manual fixes for what remained. The first commit alone is not runtime-safe (see below), so squash-merge is the right choice here. ## What changed and why **Runtime-safety fix.** ruff's PYI019 autofix rewrites the `self: _NodeType` pattern in `SyntaxTreeNode` to `Self`, importing it from `typing_extensions`. That import is now under `if TYPE_CHECKING:` (the module already has `from __future__ import annotations`, and every use of `Self` is annotation-only). Verified by importing `markdown_it.tree` and calling `SyntaxTreeNode(...).pretty()` with `typing_extensions` blocked via a meta-path finder. Nothing was added to `dependencies`. **Manual lint fixes** (all from ruff 0.16's expanded default rule set, none from rules this repo selects): - `EXE001`: removed the vestigial shebang from `markdown_it/cli/parse.py` (the CLI ships via the console script; `python -m markdown_it.cli.parse` still works). - `PIE810`: `k.startswith(("render", "_"))` in `renderer.py`. - `PYI045`: `OptionsDict.__iter__` now declares `Iterator[str]` (it always returned one), dropping a `type: ignore`. - `PYI034`: `__new__` in a test helper annotated with `Self`. - `RUF036`/`UP045`: `None | X` unions reordered to `X | None`. Type-equivalent. - mypy 2.3: one now-unused `type: ignore[import-untyped]` removed. **Autofix changes worth knowing about** (all type-equivalent or comment-only): - `Token.attrSet(value: str | int | float)` now reads `str | float` (PYI041). Under PEP 484's numeric tower `float` already accepts `int`, so nothing changes for callers, but rendered signatures will look narrower. - Two `# noqa: E731` comments in `fence.py` removed by RUF100: ruff 0.16 no longer flags those lambdas, so the comments were dead, and the hook auto-removes them if restored. - ruff-format 0.16 now formats Python code inside Markdown fences, hence the diffs in `README.md`, `AGENTS.md`, `CHANGELOG.md` and `docs/`. All are formatting-only inside fenced examples (quotes, wrapping, `...` stubs); no prose changed. ## For the maintainers to decide (not changed here) With `pyproject.toml` untouched, ruff 0.16.6 enables **463** rules versus **253** under 0.15.12: the defaults grew substantially (EXE, PIE, PYI, UP007/UP045 and more are now on without being selected). It may be worth pinning an explicit `select` so future ruff releases don't silently change what's enforced. ## Verification - `pre-commit run --all-files`: every hook passes under the new pins. - 993 tests pass. - `import markdown_it, markdown_it.tree, markdown_it.cli.parse` succeeds with `typing_extensions` unavailable. Supersedes #396 .
1 parent 0198992 commit d9b0526

19 files changed

Lines changed: 76 additions & 55 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line number Diff line number Diff line change
@@ -24,14 +24,14 @@ repos:
24 24 - id: trailing-whitespace
25 25
26 26 - repo: https://github.com/astral-sh/ruff-pre-commit
27 - rev: v0.15.12
27 + rev: v0.16.6
28 28 hooks:
29 29 - id: ruff
30 30 args: [--fix]
31 31 - id: ruff-format
32 32
33 33 - repo: https://github.com/pre-commit/mirrors-mypy
34 - rev: v1.20.2
34 + rev: v2.3.1
35 35 hooks:
36 36 - id: mypy
37 37 additional_dependencies: [mdurl, typing-extensions]

‎AGENTS.md‎

Lines changed: 12 additions & 10 deletions
Original file line number Diff line number Diff line change
@@ -158,11 +158,9 @@ from __future__ import annotations
158 158
159 159 from typing import Sequence
160 160
161 +
161 162 def parse_blocks(
162 - state: StateBlock,
163 - start_line: int,
164 - end_line: int,
165 - silent: bool = False
163 + state: StateBlock, start_line: int, end_line: int, silent: bool = False
166 164 ) -> bool:
167 165 """Parse block-level content.
168 166
@@ -293,18 +291,20 @@ HTML Output
293 291 import pytest
294 292 from markdown_it import MarkdownIt
295 293
294 +
296 295 def test_basic_parsing():
297 296 md = MarkdownIt()
298 297 result = md.render("# Heading\n\nParagraph")
299 298 assert "<h1>Heading</h1>" in result
300 299 assert "<p>Paragraph</p>" in result
301 300
301 +
302 302 @pytest.mark.parametrize(
303 303 "input_text,expected",
304 304 [
305 305 ("**bold**", "<strong>bold</strong>"),
306 306 ("*italic*", "<em>italic</em>"),
307 - ]
307 + ],
308 308 )
309 309 def test_emphasis(input_text, expected):
310 310 md = MarkdownIt()
@@ -389,7 +389,7 @@ for token in tokens:
389 389 print(md.get_all_rules())
390 390
391 391 # Enable/disable specific rules
392 -md.disable(['emphasis'])
392 +md.disable(["emphasis"])
393 393 result = md.render("*text*") # Won't be emphasized
394 394 ```
395 395
@@ -401,13 +401,13 @@ result = md.render("*text*") # Won't be emphasized
401 401 2. Create rule function in appropriate `rules_*/` directory
402 402 3. Rule signature for block rules:
403 403 ```python
404 - def rule_name(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
405 - ...
404 + def rule_name(
405 + state: StateBlock, startLine: int, endLine: int, silent: bool
406 + ) -> bool: ...
406 407 ```
407 408 4. Rule signature for inline rules:
408 409 ```python
409 - def rule_name(state: StateInline, silent: bool) -> bool:
410 - ...
410 + def rule_name(state: StateInline, silent: bool) -> bool: ...
411 411 ```
412 412 5. Register the rule in the appropriate parser's `__init__` method
413 413 6. Add tests for the new rule
@@ -434,11 +434,13 @@ result = md.render("*text*") # Won't be emphasized
434 434 ```python
435 435 from markdown_it import MarkdownIt
436 436
437 +
437 438 def render_custom_link(self, tokens, idx, options, env):
438 439 tokens[idx].attrSet("target", "_blank")
439 440 tokens[idx].attrSet("rel", "noopener noreferrer")
440 441 return self.renderToken(tokens, idx, options, env)
441 442
443 +
442 444 md = MarkdownIt()
443 445 md.add_render_rule("link_open", render_custom_link)
444 446 ```

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line number Diff line number Diff line change
@@ -267,6 +267,7 @@ It can then be activated by:
267 267
268 268 ```python
269 269 from markdown_it import MarkdownIt
270 +
270 271 md = MarkdownIt().enable("linkify")
271 272 md.options["linkify"] = True
272 273 ```
@@ -284,6 +285,7 @@ It can be activated by:
284 285
285 286 ```python
286 287 from markdown_it import MarkdownIt
288 +
287 289 md = MarkdownIt().enable("smartquotes")
288 290 md.options["typographer"] = True
289 291 ```
@@ -304,6 +306,7 @@ This plugin can be activated by:
304 306 ```python
305 307 from markdown_it import MarkdownIt
306 308 from markdown_it.extensions.tasklists import tasklists_plugin
309 +
307 310 md = MarkdownIt().use(tasklists_plugin)
308 311 ```
309 312

‎README.md‎

Lines changed: 4 additions & 4 deletions
Original file line number Diff line number Diff line change
@@ -69,12 +69,12 @@ from mdit_py_plugins.front_matter import front_matter_plugin
69 69 from mdit_py_plugins.footnote import footnote_plugin
70 70
71 71 md = (
72 - MarkdownIt('commonmark', {'breaks':True,'html':True})
72 + MarkdownIt("commonmark", {"breaks": True, "html": True})
73 73 .use(front_matter_plugin)
74 74 .use(footnote_plugin)
75 - .enable('table')
75 + .enable("table")
76 76 )
77 -text = ("""
77 +text = """
78 78 ---
79 79 a: 1
80 80 ---
@@ -86,7 +86,7 @@ a | b
86 86 A footnote [^1]
87 87
88 88 [^1]: some details
89 -""")
89 +"""
90 90 tokens = md.parse(text)
91 91 html_text = md.render(text)
92 92

‎docs/architecture.md‎

Lines changed: 14 additions & 7 deletions
Original file line number Diff line number Diff line change
@@ -105,31 +105,36 @@ with the same signature:
105 105
106 106 ```python
107 107 def function(renderer, tokens, idx, options, env):
108 - return htmlResult
108 + return htmlResult
109 109 ```
110 110
111 111 In many cases that allows easy output change even without parser intrusion.
112 112 For example, let's replace images with vimeo links to player's iframe:
113 113
114 114 ```python
115 115 import re
116 +
116 117 md = MarkdownIt("commonmark")
117 118
118 -vimeoRE = re.compile(r'^https?:\/\/(www\.)?vimeo.com\/(\d+)($|\/)')
119 +vimeoRE = re.compile(r"^https?:\/\/(www\.)?vimeo.com\/(\d+)($|\/)")
120 +
119 121
120 122 def render_vimeo(self, tokens, idx, options, env):
121 123 token = tokens[idx]
122 124
123 125 if vimeoRE.match(token.attrs["src"]):
124 -
125 126 ident = vimeoRE.match(token.attrs["src"])[2]
126 127
127 - return ('<div class="embed-responsive embed-responsive-16by9">\n' +
128 - ' <iframe class="embed-responsive-item" src="//player.vimeo.com/video/' +
129 - ident + '"></iframe>\n' +
130 - '</div>\n')
128 + return (
129 + '<div class="embed-responsive embed-responsive-16by9">\n'
130 + + ' <iframe class="embed-responsive-item" src="//player.vimeo.com/video/'
131 + + ident
132 + + '"></iframe>\n'
133 + + "</div>\n"
134 + )
131 135 return self.image(tokens, idx, options, env)
132 136
137 +
133 138 md = MarkdownIt("commonmark")
134 139 md.add_render_rule("image", render_vimeo)
135 140 print(md.render("![](https://www.vimeo.com/123)"))
@@ -140,12 +145,14 @@ Here is another example, how to add `target="_blank"` to all links:
140 145 ```python
141 146 from markdown_it import MarkdownIt
142 147
148 +
143 149 def render_blank_link(self, tokens, idx, options, env):
144 150 tokens[idx].attrSet("target", "_blank")
145 151
146 152 # pass token to default renderer.
147 153 return self.renderToken(tokens, idx, options, env)
148 154
155 +
149 156 md = MarkdownIt("commonmark")
150 157 md.add_render_rule("link_open", render_blank_link)
151 158 print(md.render("[a]\n\n[a]: b"))

‎docs/conf.py‎

Lines changed: 1 addition & 0 deletions
Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@
57 57 "Path",
58 58 "Ellipsis",
59 59 "NotRequired",
60 + "Self",
60 61 )
61 62 ]
62 63

‎docs/plugins.md‎

Lines changed: 5 additions & 2 deletions
Original file line number Diff line number Diff line change
@@ -13,14 +13,16 @@ These can be enabled individually:
13 13
14 14 ```python
15 15 from markdown_it import MarkdownIt
16 -md = MarkdownIt("commonmark").enable('table')
16 +
17 +md = MarkdownIt("commonmark").enable("table")
17 18 ```
18 19
19 20 or as part of a configuration:
20 21
21 22 ```python
22 23 from markdown_it import MarkdownIt
23 -md = MarkdownIt("gfm-like") # tables, strikethrough, linkify
24 +
25 +md = MarkdownIt("gfm-like") # tables, strikethrough, linkify
24 26 md = MarkdownIt("gfm-like2") # + task lists, alerts, single-tilde strikethrough
25 27 ```
26 28
@@ -48,6 +50,7 @@ They can be chained and loaded *via*:
48 50 ```python
49 51 from markdown_it import MarkdownIt
50 52 from mdit_py_plugins import plugin1, plugin2
53 +
51 54 md = MarkdownIt().use(plugin1, keyword=value).use(plugin2, keyword=value)
52 55 html_string = md.render("some *Markdown*")
53 56 ```

‎docs/using.md‎

Lines changed: 1 addition & 1 deletion
Original file line number Diff line number Diff line change
@@ -298,7 +298,7 @@ with the same signature:
298 298
299 299 ```python
300 300 def function(renderer, tokens, idx, options, env):
301 - return htmlResult
301 + return htmlResult
302 302 ```
303 303
304 304 +++

‎markdown_it/cli/parse.py‎

Lines changed: 0 additions & 1 deletion
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
1 -#!/usr/bin/env python
2 1 """
3 2 CLI interface to markdown-it-py
4 3

‎markdown_it/common/utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line number Diff line number Diff line change
@@ -102,7 +102,7 @@ def replaceEntityPattern(match: str, name: str) -> str:
102 102 if name in entities:
103 103 return entities[name]
104 104
105 - code: None | int = None
105 + code: int | None = None
106 106 if pat := DIGITAL_ENTITY_BASE10_RE.fullmatch(name):
107 107 code = int(pat.group(1), 10)
108 108 elif pat := DIGITAL_ENTITY_BASE16_RE.fullmatch(name):