Skip to content

[Markdown] Add toc.unique's counters parameter and DelimiterProcessor.rule_of_3 - #16713

Merged
srittau merged 2 commits into
python:mainfrom
qwist1233-cpu:markdown-counters-rule-of-3
Nov 7, 2026
Merged

srittau merged 2 commits into
python:mainfrom
qwist1233-cpu:markdown-counters-rule-of-3

Conversation

@qwist1233-cpu

Copy link
Copy Markdown
Contributor

Both stubtest errors from today's daily run (#16709), which were the only failures in it.

Markdown 3.11.3 added an optional counters cache to toc.unique() and a static rule_of_3() to DelimiterProcessor; the stubs have neither.

Verified against the installed package rather than from the diff:

>>> inspect.signature(markdown.extensions.toc.unique)
(id: 'str', ids: 'MutableSet[str]', counters: 'dict[str, int] | None' = None) -> 'str'
>>> type(inspect.getattr_static(DelimiterProcessor, 'rule_of_3'))
<class 'staticmethod'>
>>> inspect.signature(DelimiterProcessor.rule_of_3)
(opener: 'int', closer: 'int') -> 'bool'

rule_of_3 goes between get_cached_result and get_match, where it sits at runtime.

  • Without the change, stubtest prints exactly the two errors in the issue.
  • With it: Success: no issues found in 33 modules.
  • tests/mypy_test.py stubs/Markdown — success, 198 files checked (3.10-3.13).

Closes #16709

….rule_of_3

Markdown 3.11.3 gave `unique()` an optional `counters` cache and
`DelimiterProcessor` a static `rule_of_3()`; neither is in the stubs, so
today's daily stubtest run reported both.

Verified against the installed package: `inspect.signature(toc.unique)` is
`(id: str, ids: MutableSet[str], counters: dict[str, int] | None = None) -> str`,
and `rule_of_3` is a `staticmethod` with `(opener: int, closer: int) -> bool`.
stubtest reproduces the two errors from the issue without this change and is
clean with it; `tests/mypy_test.py stubs/Markdown` passes on 3.10-3.13.

Closes python#16709
@github-actions

This comment has been minimized.

@srittau srittau left a comment

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.

This also needs a version bump.

def slugify(value: str, separator: str, unicode: bool = False) -> str: ...
def slugify_unicode(value: str, separator: str) -> str: ...
def unique(id: str, ids: MutableSet[str]) -> str: ...
def unique(id: str, ids: MutableSet[str], counters: dict[str, int] | None = None) -> str: ...

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.

dict is too narrow. This should use a protocol for the two methods that are actually used.

Review feedback: `dict[str, int]` is narrower than the runtime needs.
`unique()` only calls `counters.get(base, 0)` and assigns
`counters[base] = count`, so a two-method protocol covers it.

Also bump METADATA.toml to ~=3.11.3, the release that added both
`counters` and `DelimiterProcessor.rule_of_3`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@qwist1233-cpu

Copy link
Copy Markdown
Contributor Author

Thanks — both done in a6ea62b.

Protocol. unique() only ever does counters.get(base, 0) and counters[base] = count, so that's all the parameter needs:

@type_check_only
class _IdCounters(Protocol):
    def get(self, key: str, default: int, /) -> int: ...
    def __setitem__(self, key: str, value: int, /) -> None: ...

Positional-only, so dict[str, int] and defaultdict[str, int] both still satisfy it; checked with mypy that they pass and that object() is rejected.

Version bump. ~=3.11.2 -> ~=3.11.3; 3.11.3 is the release that added both counters and DelimiterProcessor.rule_of_3.

tests/mypy_test.py stubs/Markdown is still green (198 files, 3.10-3.13) and stubtest reports success.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau
srittau merged commit 6730634 into python:main Nov 7, 2026
136 of 137 checks passed
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.

Daily tests failed on Fri Nov 06 2026

2 participants