From d1925f2bb67d9231e7ecc0ae294b33f22d903b08 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Wed, 15 Nov 2023 14:06:24 +0100 Subject: [PATCH] Various improvements to `Markdown` stubs (#10972) --- stubs/Markdown/@tests/stubtest_allowlist.txt | 6 ---- stubs/Markdown/markdown/core.pyi | 2 ++ stubs/Markdown/markdown/extensions/abbr.pyi | 10 +++--- .../markdown/extensions/admonition.pyi | 10 +++--- .../markdown/extensions/attr_list.pyi | 1 + .../markdown/extensions/codehilite.pyi | 19 +++++------ .../markdown/extensions/fenced_code.pyi | 8 ++++- .../markdown/extensions/footnotes.pyi | 33 +++++++++++-------- .../markdown/extensions/legacy_attrs.pyi | 4 ++- .../markdown/extensions/md_in_html.pyi | 10 +++++- stubs/Markdown/markdown/extensions/smarty.pyi | 18 +++++----- stubs/Markdown/markdown/extensions/tables.pyi | 3 +- stubs/Markdown/markdown/extensions/toc.pyi | 24 ++++++++------ stubs/Markdown/markdown/inlinepatterns.pyi | 23 +++++++------ stubs/Markdown/markdown/preprocessors.pyi | 11 +------ stubs/Markdown/markdown/treeprocessors.pyi | 8 ++--- stubs/Markdown/markdown/util.pyi | 13 ++++---- 17 files changed, 111 insertions(+), 92 deletions(-) diff --git a/stubs/Markdown/@tests/stubtest_allowlist.txt b/stubs/Markdown/@tests/stubtest_allowlist.txt index ffc2ed00a..e69de29bb 100644 --- a/stubs/Markdown/@tests/stubtest_allowlist.txt +++ b/stubs/Markdown/@tests/stubtest_allowlist.txt @@ -1,6 +0,0 @@ -markdown.extensions.abbr.ABBR_REF_RE -markdown.extensions.attr_list.AttrListTreeprocessor.run -markdown.extensions.footnotes.DEF_RE -markdown.extensions.footnotes.TABBED_RE -markdown.extensions.legacy_attrs.LegacyAttrs.run -markdown.extensions.toc.TocTreeprocessor.run diff --git a/stubs/Markdown/markdown/core.pyi b/stubs/Markdown/markdown/core.pyi index d8f7152ca..c45625990 100644 --- a/stubs/Markdown/markdown/core.pyi +++ b/stubs/Markdown/markdown/core.pyi @@ -33,6 +33,8 @@ class Markdown: block_level_elements: list[str] registeredExtensions: list[Extension] ESCAPED_CHARS: list[str] + doc_tag: ClassVar[str] + stripTopLevelTags: bool def __init__( self, *, diff --git a/stubs/Markdown/markdown/extensions/abbr.pyi b/stubs/Markdown/markdown/extensions/abbr.pyi index 7df4e4697..682f80540 100644 --- a/stubs/Markdown/markdown/extensions/abbr.pyi +++ b/stubs/Markdown/markdown/extensions/abbr.pyi @@ -1,17 +1,17 @@ from re import Pattern -from typing import Any +from typing import ClassVar from markdown.blockprocessors import BlockProcessor from markdown.extensions import Extension from markdown.inlinepatterns import InlineProcessor -ABBR_REF_RE: Pattern[str] - class AbbrExtension(Extension): ... -class AbbrPreprocessor(BlockProcessor): ... + +class AbbrPreprocessor(BlockProcessor): + RE: ClassVar[Pattern[str]] class AbbrInlineProcessor(InlineProcessor): - title: Any + title: str def __init__(self, pattern: str, title: str) -> None: ... def makeExtension(**kwargs) -> AbbrExtension: ... diff --git a/stubs/Markdown/markdown/extensions/admonition.pyi b/stubs/Markdown/markdown/extensions/admonition.pyi index 686eab089..0225c528e 100644 --- a/stubs/Markdown/markdown/extensions/admonition.pyi +++ b/stubs/Markdown/markdown/extensions/admonition.pyi @@ -1,5 +1,5 @@ from re import Match, Pattern -from typing import Any +from typing import ClassVar from xml.etree.ElementTree import Element from markdown import blockparser @@ -11,9 +11,11 @@ class AdmonitionExtension(Extension): ... class AdmonitionProcessor(BlockProcessor): CLASSNAME: str CLASSNAME_TITLE: str - RE: Pattern[str] - RE_SPACES: Any - def __init__(self, parser: blockparser.BlockParser): ... + RE: ClassVar[Pattern[str]] + RE_SPACES: ClassVar[Pattern[str]] + current_sibling: Element | None + content_indent: int + def __init__(self, parser: blockparser.BlockParser) -> None: ... def parse_content(self, parent: Element, block: str) -> tuple[Element | None, str, str]: ... def get_class_and_title(self, match: Match[str]) -> tuple[str, str | None]: ... diff --git a/stubs/Markdown/markdown/extensions/attr_list.pyi b/stubs/Markdown/markdown/extensions/attr_list.pyi index 28e597271..7602fd771 100644 --- a/stubs/Markdown/markdown/extensions/attr_list.pyi +++ b/stubs/Markdown/markdown/extensions/attr_list.pyi @@ -13,6 +13,7 @@ class AttrListTreeprocessor(Treeprocessor): BLOCK_RE: Pattern[str] INLINE_RE: Pattern[str] NAME_RE: Pattern[str] + def run(self, doc: Element) -> None: ... def assign_attrs(self, elem: Element, attrs: str) -> None: ... def sanitize_name(self, name: str) -> str: ... diff --git a/stubs/Markdown/markdown/extensions/codehilite.pyi b/stubs/Markdown/markdown/extensions/codehilite.pyi index 15a44ee7d..b7458a809 100644 --- a/stubs/Markdown/markdown/extensions/codehilite.pyi +++ b/stubs/Markdown/markdown/extensions/codehilite.pyi @@ -5,19 +5,15 @@ from markdown.treeprocessors import Treeprocessor pygments: bool -def parse_hl_lines(expr): ... +def parse_hl_lines(expr: str) -> list[int]: ... class CodeHilite: - src: Any - lang: Any - linenums: Any - guess_lang: Any - css_class: Any - style: Any - noclasses: Any - tab_length: Any - hl_lines: Any - use_pygments: Any + src: str + lang: str | None + guess_lang: bool + use_pygments: bool + lang_prefix: str + pygments_formatter: Any options: dict[str, Any] def __init__( self, @@ -37,6 +33,7 @@ class CodeHilite: def hilite(self, shebang: bool = True) -> str: ... class HiliteTreeprocessor(Treeprocessor): + config: dict[str, Any] def code_unescape(self, text: str) -> str: ... class CodeHiliteExtension(Extension): diff --git a/stubs/Markdown/markdown/extensions/fenced_code.pyi b/stubs/Markdown/markdown/extensions/fenced_code.pyi index 154d6dd81..39810ab26 100644 --- a/stubs/Markdown/markdown/extensions/fenced_code.pyi +++ b/stubs/Markdown/markdown/extensions/fenced_code.pyi @@ -1,3 +1,4 @@ +from collections.abc import Iterable from re import Pattern from typing import Any, ClassVar @@ -5,11 +6,16 @@ from markdown.core import Markdown from markdown.extensions import Extension from markdown.preprocessors import Preprocessor -class FencedCodeExtension(Extension): ... +class FencedCodeExtension(Extension): + def __init__(self, **kwargs) -> None: ... class FencedBlockPreprocessor(Preprocessor): FENCED_BLOCK_RE: ClassVar[Pattern[str]] + checked_for_deps: bool codehilite_conf: dict[str, Any] + use_attr_list: bool + bool_options: list[str] def __init__(self, md: Markdown, config: dict[str, Any]) -> None: ... + def handle_attrs(self, attrs: Iterable[tuple[str, str]]) -> tuple[str, list[str], dict[str, Any]]: ... def makeExtension(**kwargs) -> FencedCodeExtension: ... diff --git a/stubs/Markdown/markdown/extensions/footnotes.pyi b/stubs/Markdown/markdown/extensions/footnotes.pyi index 604a13ca8..f145ffdeb 100644 --- a/stubs/Markdown/markdown/extensions/footnotes.pyi +++ b/stubs/Markdown/markdown/extensions/footnotes.pyi @@ -1,37 +1,44 @@ from collections import OrderedDict from re import Pattern -from typing import Any +from typing import ClassVar from xml.etree.ElementTree import Element +from markdown.blockparser import BlockParser +from markdown.blockprocessors import BlockProcessor from markdown.core import Markdown from markdown.extensions import Extension from markdown.inlinepatterns import InlineProcessor from markdown.postprocessors import Postprocessor from markdown.treeprocessors import Treeprocessor -FN_BACKLINK_TEXT: Any -NBSP_PLACEHOLDER: Any -DEF_RE: Pattern[str] -TABBED_RE: Pattern[str] -RE_REF_ID: Any +FN_BACKLINK_TEXT: str +NBSP_PLACEHOLDER: str +RE_REF_ID: Pattern[str] class FootnoteExtension(Extension): unique_prefix: int - found_refs: Any - used_refs: Any + found_refs: dict[str, int] + used_refs: set[str] def __init__(self, **kwargs) -> None: ... - parser: Any + parser: BlockParser md: Markdown footnotes: OrderedDict[str, str] def reset(self) -> None: ... def unique_ref(self, reference: str, found: bool = False) -> str: ... - def findFootnotesPlaceholder(self, root: Element): ... + def findFootnotesPlaceholder(self, root: Element) -> tuple[Element, Element, bool] | None: ... def setFootnote(self, id: str, text: str) -> None: ... def get_separator(self) -> str: ... def makeFootnoteId(self, id: str) -> str: ... def makeFootnoteRefId(self, id: str, found: bool = False) -> str: ... def makeFootnotesDiv(self, root: Element) -> Element | None: ... +class FootnoteBlockProcessor(BlockProcessor): + RE: ClassVar[Pattern[str]] + footnotes: FootnoteExtension + def __init__(self, footnotes: FootnoteExtension) -> None: ... + def detectTabbed(self, blocks: list[str]) -> list[str]: ... + def detab(self, block: str) -> str: ... # type: ignore[override] + class FootnoteInlineProcessor(InlineProcessor): footnotes: FootnoteExtension def __init__(self, pattern: str, footnotes: FootnoteExtension) -> None: ... @@ -39,9 +46,9 @@ class FootnoteInlineProcessor(InlineProcessor): class FootnotePostTreeprocessor(Treeprocessor): footnotes: FootnoteExtension def __init__(self, footnotes: FootnoteExtension) -> None: ... - def add_duplicates(self, li, duplicates) -> None: ... - def get_num_duplicates(self, li): ... - def handle_duplicates(self, parent) -> None: ... + def add_duplicates(self, li: Element, duplicates: int) -> None: ... + def get_num_duplicates(self, li: Element) -> int: ... + def handle_duplicates(self, parent: Element) -> None: ... offset: int class FootnoteTreeprocessor(Treeprocessor): diff --git a/stubs/Markdown/markdown/extensions/legacy_attrs.pyi b/stubs/Markdown/markdown/extensions/legacy_attrs.pyi index 410cf9e98..9464f3dff 100644 --- a/stubs/Markdown/markdown/extensions/legacy_attrs.pyi +++ b/stubs/Markdown/markdown/extensions/legacy_attrs.pyi @@ -1,4 +1,5 @@ from re import Pattern +from xml.etree.ElementTree import Element from markdown.extensions import Extension from markdown.treeprocessors import Treeprocessor @@ -6,7 +7,8 @@ from markdown.treeprocessors import Treeprocessor ATTR_RE: Pattern[str] class LegacyAttrs(Treeprocessor): - def handleAttributes(self, el, txt): ... + def run(self, doc: Element) -> None: ... + def handleAttributes(self, el: Element, txt: str) -> str: ... class LegacyAttrExtension(Extension): ... diff --git a/stubs/Markdown/markdown/extensions/md_in_html.pyi b/stubs/Markdown/markdown/extensions/md_in_html.pyi index 7346ac914..b1ea12305 100644 --- a/stubs/Markdown/markdown/extensions/md_in_html.pyi +++ b/stubs/Markdown/markdown/extensions/md_in_html.pyi @@ -1,7 +1,15 @@ +from xml.etree.ElementTree import Element + from markdown.blockprocessors import BlockProcessor from markdown.extensions import Extension +from markdown.postprocessors import RawHtmlPostprocessor + +class MarkdownInHtmlProcessor(BlockProcessor): + def parse_element_content(self, element: Element) -> None: ... + +class MarkdownInHTMLPostprocessor(RawHtmlPostprocessor): + def stash_to_string(self, text: str | Element) -> str: ... -class MarkdownInHtmlProcessor(BlockProcessor): ... class MarkdownInHtmlExtension(Extension): ... def makeExtension(**kwargs) -> MarkdownInHtmlExtension: ... diff --git a/stubs/Markdown/markdown/extensions/smarty.pyi b/stubs/Markdown/markdown/extensions/smarty.pyi index b3706d99a..dcf57107c 100644 --- a/stubs/Markdown/markdown/extensions/smarty.pyi +++ b/stubs/Markdown/markdown/extensions/smarty.pyi @@ -1,4 +1,4 @@ -from collections.abc import Sequence +from collections.abc import Mapping, Sequence from typing import Any from xml.etree.ElementTree import Element @@ -11,18 +11,18 @@ punctClass: str endOfWordClass: str closeClass: str openingQuotesBase: str -substitutions: Any -singleQuoteStartRe: Any -doubleQuoteStartRe: Any +substitutions: Mapping[str, str] +singleQuoteStartRe: str +doubleQuoteStartRe: str doubleQuoteSetsRe: str singleQuoteSetsRe: str decadeAbbrRe: str -openingDoubleQuotesRegex: Any +openingDoubleQuotesRegex: str closingDoubleQuotesRegex: str -closingDoubleQuotesRegex2: Any -openingSingleQuotesRegex: Any -closingSingleQuotesRegex: Any -closingSingleQuotesRegex2: Any +closingDoubleQuotesRegex2: str +openingSingleQuotesRegex: str +closingSingleQuotesRegex: str +closingSingleQuotesRegex2: str remainingSingleQuotesRegex: str remainingDoubleQuotesRegex: str HTML_STRICT_RE: str diff --git a/stubs/Markdown/markdown/extensions/tables.pyi b/stubs/Markdown/markdown/extensions/tables.pyi index 4c68c0583..a6ee99d7a 100644 --- a/stubs/Markdown/markdown/extensions/tables.pyi +++ b/stubs/Markdown/markdown/extensions/tables.pyi @@ -16,6 +16,7 @@ class TableProcessor(BlockProcessor): separator: str def __init__(self, parser: blockparser.BlockParser, config: dict[str, Any]) -> None: ... -class TableExtension(Extension): ... +class TableExtension(Extension): + def __init__(self, **kwargs) -> None: ... def makeExtension(**kwargs) -> TableExtension: ... diff --git a/stubs/Markdown/markdown/extensions/toc.pyi b/stubs/Markdown/markdown/extensions/toc.pyi index 223973c83..4945e2d13 100644 --- a/stubs/Markdown/markdown/extensions/toc.pyi +++ b/stubs/Markdown/markdown/extensions/toc.pyi @@ -27,19 +27,22 @@ def unescape(text: str) -> str: ... def nest_toc_tokens(toc_list: list[_FlatTocToken]) -> list[_TocToken]: ... class TocTreeprocessor(Treeprocessor): - marker: Any - title: Any - base_level: Any + marker: str + title: str + base_level: int slugify: Any sep: Any - use_anchors: Any - anchorlink_class: Any - use_permalinks: Any - permalink_class: Any - permalink_title: Any - header_rgx: Any + toc_class: Any + title_class: str + use_anchors: bool + anchorlink_class: str + use_permalinks: bool + permalink_class: str + permalink_title: str + permalink_leading: bool + header_rgx: Pattern[str] toc_top: int - toc_bottom: Any + toc_bottom: int def __init__(self, md: Markdown, config: dict[str, Any]) -> None: ... def iterparent(self, node: Element) -> Iterator[tuple[Element, Element]]: ... def replace_marker(self, root: Element, elem: Element) -> None: ... @@ -47,6 +50,7 @@ class TocTreeprocessor(Treeprocessor): def add_anchor(self, c: Element, elem_id: str) -> None: ... def add_permalink(self, c: Element, elem_id: str) -> None: ... def build_toc_div(self, toc_list: list[_TocToken]) -> Element: ... + def run(self, doc: Element) -> None: ... class TocExtension(Extension): TreeProcessorClass: type[TocTreeprocessor] diff --git a/stubs/Markdown/markdown/inlinepatterns.pyi b/stubs/Markdown/markdown/inlinepatterns.pyi index ea4f415ac..1c49294b1 100644 --- a/stubs/Markdown/markdown/inlinepatterns.pyi +++ b/stubs/Markdown/markdown/inlinepatterns.pyi @@ -1,6 +1,6 @@ import re from collections.abc import Collection -from typing import Any, ClassVar, NamedTuple +from typing import ClassVar, NamedTuple from xml.etree.ElementTree import Element from markdown import util @@ -53,31 +53,33 @@ class Pattern: class InlineProcessor(Pattern): safe_mode: bool def __init__(self, pattern: str, md: Markdown | None = None) -> None: ... - def handleMatch(self, m: re.Match[str], data) -> tuple[Element, int, int] | tuple[None, None, None]: ... # type: ignore[override] + def handleMatch(self, m: re.Match[str], data: str) -> tuple[Element | str | None, int | None, int | None]: ... # type: ignore[override] class SimpleTextPattern(Pattern): ... class SimpleTextInlineProcessor(InlineProcessor): ... class EscapeInlineProcessor(InlineProcessor): ... class SimpleTagPattern(Pattern): - tag: Any + tag: str def __init__(self, pattern: str, tag: str) -> None: ... class SimpleTagInlineProcessor(InlineProcessor): - tag: Any + tag: str def __init__(self, pattern: str, tag: str) -> None: ... class SubstituteTagPattern(SimpleTagPattern): ... class SubstituteTagInlineProcessor(SimpleTagInlineProcessor): ... class BacktickInlineProcessor(InlineProcessor): - ESCAPED_BSLASH: Any + ESCAPED_BSLASH: str tag: str def __init__(self, pattern: str) -> None: ... -class DoubleTagPattern(SimpleTagPattern): ... class DoubleTagInlineProcessor(SimpleTagInlineProcessor): ... -class HtmlInlineProcessor(InlineProcessor): ... + +class HtmlInlineProcessor(InlineProcessor): + def unescape(self, text: str) -> str: ... + def backslash_unescape(self, text: str) -> str: ... class AsteriskProcessor(InlineProcessor): PATTERNS: ClassVar[list[EmStrongItem]] @@ -92,17 +94,18 @@ class UnderscoreProcessor(AsteriskProcessor): ... class LinkInlineProcessor(InlineProcessor): RE_LINK: ClassVar[re.Pattern[str]] RE_TITLE_CLEAN: ClassVar[re.Pattern[str]] - def getLink(self, data: str, index: int): ... - def getText(self, data: str, index: int): ... + def getLink(self, data: str, index: int) -> tuple[str, str | None, int, bool]: ... + def getText(self, data: str, index: int) -> tuple[str, int, bool]: ... class ImageInlineProcessor(LinkInlineProcessor): ... class ReferenceInlineProcessor(LinkInlineProcessor): NEWLINE_CLEANUP_RE: ClassVar[re.Pattern[str]] - def evalId(self, data: str, index: int, text: str): ... + def evalId(self, data: str, index: int, text: str) -> tuple[str | None, int, bool]: ... def makeTag(self, href: str, title: str, text: str) -> Element: ... class ShortReferenceInlineProcessor(ReferenceInlineProcessor): ... class ImageReferenceInlineProcessor(ReferenceInlineProcessor): ... +class ShortImageReferenceInlineProcessor(ImageReferenceInlineProcessor): ... class AutolinkInlineProcessor(InlineProcessor): ... class AutomailInlineProcessor(InlineProcessor): ... diff --git a/stubs/Markdown/markdown/preprocessors.pyi b/stubs/Markdown/markdown/preprocessors.pyi index 4a4523171..dc5566988 100644 --- a/stubs/Markdown/markdown/preprocessors.pyi +++ b/stubs/Markdown/markdown/preprocessors.pyi @@ -1,5 +1,3 @@ -from typing import Any - from markdown.core import Markdown from . import util @@ -10,11 +8,4 @@ class Preprocessor(util.Processor): def run(self, lines: list[str]) -> list[str]: ... class NormalizeWhitespace(Preprocessor): ... - -class HtmlBlockPreprocessor(Preprocessor): - right_tag_patterns: Any - attrs_pattern: str - left_tag_pattern: Any - attrs_re: Any - left_tag_re: Any - markdown_in_raw: bool +class HtmlBlockPreprocessor(Preprocessor): ... diff --git a/stubs/Markdown/markdown/treeprocessors.pyi b/stubs/Markdown/markdown/treeprocessors.pyi index 21edf41f9..9afc5d801 100644 --- a/stubs/Markdown/markdown/treeprocessors.pyi +++ b/stubs/Markdown/markdown/treeprocessors.pyi @@ -13,10 +13,10 @@ class Treeprocessor(util.Processor): def run(self, root: Element) -> Element | None: ... class InlineProcessor(Treeprocessor): - inlinePatterns: Any - ancestors: Any - def __init__(self, md) -> None: ... - stashed_nodes: Any + inlinePatterns: util.Registry[InlineProcessor] + ancestors: list[str] + def __init__(self, md: Markdown) -> None: ... + stashed_nodes: dict[str, Element | str] parent_map: Any def run(self, tree: Element, ancestors: list[str] | None = None) -> Element: ... diff --git a/stubs/Markdown/markdown/util.pyi b/stubs/Markdown/markdown/util.pyi index 6817e8ed3..e11b66445 100644 --- a/stubs/Markdown/markdown/util.pyi +++ b/stubs/Markdown/markdown/util.pyi @@ -7,18 +7,19 @@ from markdown.core import Markdown _T = TypeVar("_T") -BLOCK_LEVEL_ELEMENTS: Any +BLOCK_LEVEL_ELEMENTS: list[str] STX: str ETX: str -INLINE_PLACEHOLDER_PREFIX: Any -INLINE_PLACEHOLDER: Any +INLINE_PLACEHOLDER_PREFIX: str +INLINE_PLACEHOLDER: str INLINE_PLACEHOLDER_RE: Pattern[str] -AMP_SUBSTITUTE: Any -HTML_PLACEHOLDER: Any +AMP_SUBSTITUTE: str +HTML_PLACEHOLDER: str HTML_PLACEHOLDER_RE: Pattern[str] -TAG_PLACEHOLDER: Any +TAG_PLACEHOLDER: str RTL_BIDI_RANGES: Any +def get_installed_extensions(): ... def deprecated(message: str, stacklevel: int = 2): ... @overload def parseBoolValue(value: str) -> bool: ...