From 19650767da0bcd01b8e9dc98b4b873522edbf6f9 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Thu, 2 Nov 2023 18:24:17 +0100 Subject: [PATCH] Various improvements to `Markdown` stubs (#10963) --- stubs/Markdown/@tests/stubtest_allowlist.txt | 5 -- stubs/Markdown/markdown/blockparser.pyi | 6 +-- stubs/Markdown/markdown/blockprocessors.pyi | 7 ++- stubs/Markdown/markdown/core.pyi | 26 +++++----- .../Markdown/markdown/extensions/__init__.pyi | 4 +- stubs/Markdown/markdown/extensions/abbr.pyi | 4 +- .../markdown/extensions/admonition.pyi | 10 ++-- .../markdown/extensions/attr_list.pyi | 11 +++-- .../markdown/extensions/codehilite.pyi | 13 +++-- .../Markdown/markdown/extensions/def_list.pyi | 2 +- stubs/Markdown/markdown/extensions/extra.pyi | 2 +- .../markdown/extensions/fenced_code.pyi | 10 ++-- .../markdown/extensions/footnotes.pyi | 42 +++++++--------- .../markdown/extensions/legacy_attrs.pyi | 2 +- .../markdown/extensions/legacy_em.pyi | 2 +- .../markdown/extensions/md_in_html.pyi | 2 +- stubs/Markdown/markdown/extensions/meta.pyi | 2 +- stubs/Markdown/markdown/extensions/nl2br.pyi | 2 +- .../markdown/extensions/sane_lists.pyi | 7 +-- stubs/Markdown/markdown/extensions/smarty.pyi | 20 +++++--- stubs/Markdown/markdown/extensions/tables.pyi | 7 +-- stubs/Markdown/markdown/extensions/toc.pyi | 43 ++++++++++------ .../markdown/extensions/wikilinks.pyi | 8 +-- stubs/Markdown/markdown/inlinepatterns.pyi | 49 +++++++++---------- stubs/Markdown/markdown/postprocessors.pyi | 14 ++---- stubs/Markdown/markdown/preprocessors.pyi | 4 +- stubs/Markdown/markdown/serializers.pyi | 6 ++- stubs/Markdown/markdown/treeprocessors.pyi | 11 +++-- stubs/Markdown/markdown/util.pyi | 33 +++++++++---- 29 files changed, 189 insertions(+), 165 deletions(-) diff --git a/stubs/Markdown/@tests/stubtest_allowlist.txt b/stubs/Markdown/@tests/stubtest_allowlist.txt index 1651202c5..ffc2ed00a 100644 --- a/stubs/Markdown/@tests/stubtest_allowlist.txt +++ b/stubs/Markdown/@tests/stubtest_allowlist.txt @@ -1,11 +1,6 @@ markdown.extensions.abbr.ABBR_REF_RE markdown.extensions.attr_list.AttrListTreeprocessor.run -markdown.extensions.codehilite.CodeHilite.__init__ -markdown.extensions.fenced_code.FencedBlockPreprocessor.__init__ markdown.extensions.footnotes.DEF_RE -markdown.extensions.footnotes.FootnotePreprocessor markdown.extensions.footnotes.TABBED_RE markdown.extensions.legacy_attrs.LegacyAttrs.run markdown.extensions.toc.TocTreeprocessor.run -markdown.extensions.toc.slugify -markdown.postprocessors.UnescapePostprocessor # deprecated diff --git a/stubs/Markdown/markdown/blockparser.pyi b/stubs/Markdown/markdown/blockparser.pyi index 69fd02de7..9fd9f5e85 100644 --- a/stubs/Markdown/markdown/blockparser.pyi +++ b/stubs/Markdown/markdown/blockparser.pyi @@ -2,8 +2,8 @@ from collections.abc import Iterable from typing import Any, TypeVar from xml.etree.ElementTree import Element, ElementTree -from . import Markdown -from .util import Registry +from markdown import blockprocessors, util +from markdown.core import Markdown _T = TypeVar("_T") @@ -13,7 +13,7 @@ class State(list[_T]): def isstate(self, state: _T) -> bool: ... class BlockParser: - blockprocessors: Registry + blockprocessors: util.Registry[blockprocessors.BlockProcessor] state: State[Any] # TODO: possible to get rid of Any? md: Markdown def __init__(self, md: Markdown) -> None: ... diff --git a/stubs/Markdown/markdown/blockprocessors.pyi b/stubs/Markdown/markdown/blockprocessors.pyi index b563bc12a..0b9461c82 100644 --- a/stubs/Markdown/markdown/blockprocessors.pyi +++ b/stubs/Markdown/markdown/blockprocessors.pyi @@ -3,13 +3,12 @@ from re import Match, Pattern from typing import Any, ClassVar from xml.etree.ElementTree import Element -from markdown import Markdown - -from .blockparser import BlockParser +from markdown.blockparser import BlockParser +from markdown.core import Markdown logger: Logger -def build_block_parser(md: Markdown, **kwargs: Any): ... +def build_block_parser(md: Markdown, **kwargs: Any) -> BlockParser: ... class BlockProcessor: parser: BlockParser diff --git a/stubs/Markdown/markdown/core.pyi b/stubs/Markdown/markdown/core.pyi index 4b195b339..7a1405b4c 100644 --- a/stubs/Markdown/markdown/core.pyi +++ b/stubs/Markdown/markdown/core.pyi @@ -3,7 +3,7 @@ from typing import Any, ClassVar, Protocol from typing_extensions import Literal, Self from xml.etree.ElementTree import Element -from .blockparser import BlockParser +from . import blockparser, inlinepatterns, postprocessors, preprocessors, treeprocessors from .extensions import Extension from .util import HtmlStash, Registry @@ -20,11 +20,11 @@ class _ReadableStream(Protocol): def close(self) -> object: ... class Markdown: - preprocessors: Registry - inlinePatterns: Registry - treeprocessors: Registry - postprocessors: Registry - parser: BlockParser + preprocessors: Registry[preprocessors.Preprocessor] + inlinePatterns: Registry[inlinepatterns.InlineProcessor] + treeprocessors: Registry[treeprocessors.Treeprocessor] + postprocessors: Registry[postprocessors.Postprocessor] + parser: blockparser.BlockParser htmlStash: HtmlStash output_formats: ClassVar[dict[Literal["xhtml", "html"], Callable[[Element], str]]] output_format: Literal["xhtml", "html"] @@ -41,17 +41,17 @@ class Markdown: output_format: Literal["xhtml", "html"] | None = ..., tab_length: int | None = ..., ) -> None: ... - def build_parser(self) -> Markdown: ... - def registerExtensions(self, extensions: Sequence[Extension | str], configs: Mapping[str, Mapping[str, Any]]) -> Markdown: ... - def build_extension(self, ext_name: str, configs: Mapping[str, str]) -> Extension: ... - def registerExtension(self, extension: Extension) -> Markdown: ... + def build_parser(self) -> Self: ... + def registerExtensions(self, extensions: Sequence[Extension | str], configs: Mapping[str, dict[str, Any]]) -> Self: ... + def build_extension(self, ext_name: str, configs: Mapping[str, Any]) -> Extension: ... + def registerExtension(self, extension: Extension) -> Self: ... def reset(self) -> Self: ... - def set_output_format(self, format: Literal["xhtml", "html"]) -> Markdown: ... - def is_block_level(self, tag: str) -> bool: ... + def set_output_format(self, format: Literal["xhtml", "html"]) -> Self: ... + def is_block_level(self, tag: object) -> bool: ... def convert(self, source: str) -> str: ... def convertFile( self, input: str | _ReadableStream | None = None, output: str | _WritableStream | None = None, encoding: str | None = None - ) -> Markdown: ... + ) -> Self: ... def markdown( text: str, diff --git a/stubs/Markdown/markdown/extensions/__init__.pyi b/stubs/Markdown/markdown/extensions/__init__.pyi index cd02ff3ed..2ae632654 100644 --- a/stubs/Markdown/markdown/extensions/__init__.pyi +++ b/stubs/Markdown/markdown/extensions/__init__.pyi @@ -1,4 +1,4 @@ -from collections.abc import Mapping +from collections.abc import Iterable, Mapping from typing import Any from markdown.core import Markdown @@ -10,5 +10,5 @@ class Extension: def getConfigs(self) -> dict[str, Any]: ... def getConfigInfo(self) -> list[tuple[str, str]]: ... def setConfig(self, key: str, value: Any) -> None: ... - def setConfigs(self, items: Mapping[str, Any]) -> None: ... + def setConfigs(self, items: Mapping[str, Any] | Iterable[tuple[str, Any]]) -> None: ... def extendMarkdown(self, md: Markdown) -> None: ... diff --git a/stubs/Markdown/markdown/extensions/abbr.pyi b/stubs/Markdown/markdown/extensions/abbr.pyi index aafd0af49..7df4e4697 100644 --- a/stubs/Markdown/markdown/extensions/abbr.pyi +++ b/stubs/Markdown/markdown/extensions/abbr.pyi @@ -12,6 +12,6 @@ class AbbrPreprocessor(BlockProcessor): ... class AbbrInlineProcessor(InlineProcessor): title: Any - def __init__(self, pattern, title) -> None: ... + def __init__(self, pattern: str, title: str) -> None: ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> AbbrExtension: ... diff --git a/stubs/Markdown/markdown/extensions/admonition.pyi b/stubs/Markdown/markdown/extensions/admonition.pyi index a7cf51085..686eab089 100644 --- a/stubs/Markdown/markdown/extensions/admonition.pyi +++ b/stubs/Markdown/markdown/extensions/admonition.pyi @@ -1,6 +1,8 @@ -from re import Pattern +from re import Match, Pattern from typing import Any +from xml.etree.ElementTree import Element +from markdown import blockparser from markdown.blockprocessors import BlockProcessor from markdown.extensions import Extension @@ -11,6 +13,8 @@ class AdmonitionProcessor(BlockProcessor): CLASSNAME_TITLE: str RE: Pattern[str] RE_SPACES: Any - def get_class_and_title(self, match): ... + def __init__(self, parser: blockparser.BlockParser): ... + 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]: ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> AdmonitionExtension: ... diff --git a/stubs/Markdown/markdown/extensions/attr_list.pyi b/stubs/Markdown/markdown/extensions/attr_list.pyi index 238d943d0..28e597271 100644 --- a/stubs/Markdown/markdown/extensions/attr_list.pyi +++ b/stubs/Markdown/markdown/extensions/attr_list.pyi @@ -1,10 +1,11 @@ from re import Pattern +from xml.etree.ElementTree import Element from markdown.extensions import Extension from markdown.treeprocessors import Treeprocessor -def get_attrs(str): ... -def isheader(elem): ... +def get_attrs(str: str) -> list[tuple[str, str]]: ... +def isheader(elem: Element) -> bool: ... class AttrListTreeprocessor(Treeprocessor): BASE_RE: str @@ -12,9 +13,9 @@ class AttrListTreeprocessor(Treeprocessor): BLOCK_RE: Pattern[str] INLINE_RE: Pattern[str] NAME_RE: Pattern[str] - def assign_attrs(self, elem, attrs) -> None: ... - def sanitize_name(self, name): ... + def assign_attrs(self, elem: Element, attrs: str) -> None: ... + def sanitize_name(self, name: str) -> str: ... class AttrListExtension(Extension): ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> AttrListExtension: ... diff --git a/stubs/Markdown/markdown/extensions/codehilite.pyi b/stubs/Markdown/markdown/extensions/codehilite.pyi index df9948b65..15a44ee7d 100644 --- a/stubs/Markdown/markdown/extensions/codehilite.pyi +++ b/stubs/Markdown/markdown/extensions/codehilite.pyi @@ -1,4 +1,3 @@ -from _typeshed import Incomplete from typing import Any from markdown.extensions import Extension @@ -22,25 +21,25 @@ class CodeHilite: options: dict[str, Any] def __init__( self, - src: Incomplete | None = ..., + src: str, *, - linenums: Incomplete | None = ..., + linenums: bool | None = None, guess_lang: bool = ..., css_class: str = ..., - lang: Incomplete | None = ..., + lang: str | None = ..., style: str = ..., noclasses: bool = ..., tab_length: int = ..., - hl_lines: Incomplete | None = ..., + hl_lines: list[int] = ..., use_pygments: bool = ..., **options: Any, ) -> None: ... def hilite(self, shebang: bool = True) -> str: ... class HiliteTreeprocessor(Treeprocessor): - def code_unescape(self, text): ... + def code_unescape(self, text: str) -> str: ... class CodeHiliteExtension(Extension): def __init__(self, **kwargs) -> None: ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> CodeHiliteExtension: ... diff --git a/stubs/Markdown/markdown/extensions/def_list.pyi b/stubs/Markdown/markdown/extensions/def_list.pyi index 48ef08d6a..345ea5077 100644 --- a/stubs/Markdown/markdown/extensions/def_list.pyi +++ b/stubs/Markdown/markdown/extensions/def_list.pyi @@ -10,4 +10,4 @@ class DefListProcessor(BlockProcessor): class DefListIndentProcessor(ListIndentProcessor): ... class DefListExtension(Extension): ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> DefListExtension: ... diff --git a/stubs/Markdown/markdown/extensions/extra.pyi b/stubs/Markdown/markdown/extensions/extra.pyi index 8d761845c..7262914db 100644 --- a/stubs/Markdown/markdown/extensions/extra.pyi +++ b/stubs/Markdown/markdown/extensions/extra.pyi @@ -7,4 +7,4 @@ extensions: Any class ExtraExtension(Extension): def __init__(self, **kwargs) -> None: ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> ExtraExtension: ... diff --git a/stubs/Markdown/markdown/extensions/fenced_code.pyi b/stubs/Markdown/markdown/extensions/fenced_code.pyi index 7a84ab0e4..154d6dd81 100644 --- a/stubs/Markdown/markdown/extensions/fenced_code.pyi +++ b/stubs/Markdown/markdown/extensions/fenced_code.pyi @@ -1,7 +1,7 @@ -from _typeshed import Incomplete from re import Pattern -from typing import ClassVar +from typing import Any, ClassVar +from markdown.core import Markdown from markdown.extensions import Extension from markdown.preprocessors import Preprocessor @@ -9,7 +9,7 @@ class FencedCodeExtension(Extension): ... class FencedBlockPreprocessor(Preprocessor): FENCED_BLOCK_RE: ClassVar[Pattern[str]] - codehilite_conf: dict[Incomplete, Incomplete] - def __init__(self, md) -> None: ... + codehilite_conf: dict[str, Any] + def __init__(self, md: Markdown, config: dict[str, Any]) -> None: ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> FencedCodeExtension: ... diff --git a/stubs/Markdown/markdown/extensions/footnotes.pyi b/stubs/Markdown/markdown/extensions/footnotes.pyi index 92b34ecbb..604a13ca8 100644 --- a/stubs/Markdown/markdown/extensions/footnotes.pyi +++ b/stubs/Markdown/markdown/extensions/footnotes.pyi @@ -1,11 +1,12 @@ +from collections import OrderedDict from re import Pattern from typing import Any +from xml.etree.ElementTree import Element from markdown.core import Markdown from markdown.extensions import Extension from markdown.inlinepatterns import InlineProcessor from markdown.postprocessors import Postprocessor -from markdown.preprocessors import Preprocessor from markdown.treeprocessors import Treeprocessor FN_BACKLINK_TEXT: Any @@ -21,39 +22,34 @@ class FootnoteExtension(Extension): def __init__(self, **kwargs) -> None: ... parser: Any md: Markdown - footnotes: Any + footnotes: OrderedDict[str, str] def reset(self) -> None: ... - def unique_ref(self, reference, found: bool = False): ... - def findFootnotesPlaceholder(self, root): ... - def setFootnote(self, id, text) -> None: ... - def get_separator(self): ... - def makeFootnoteId(self, id): ... - def makeFootnoteRefId(self, id, found: bool = False): ... - def makeFootnotesDiv(self, root): ... - -class FootnotePreprocessor(Preprocessor): - footnotes: Any - def __init__(self, footnotes) -> None: ... - def detectTabbed(self, lines): ... + def unique_ref(self, reference: str, found: bool = False) -> str: ... + def findFootnotesPlaceholder(self, root: Element): ... + 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 FootnoteInlineProcessor(InlineProcessor): - footnotes: Any - def __init__(self, pattern, footnotes) -> None: ... + footnotes: FootnoteExtension + def __init__(self, pattern: str, footnotes: FootnoteExtension) -> None: ... class FootnotePostTreeprocessor(Treeprocessor): - footnotes: Any - def __init__(self, footnotes) -> None: ... + 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: ... offset: int class FootnoteTreeprocessor(Treeprocessor): - footnotes: Any - def __init__(self, footnotes) -> None: ... + footnotes: FootnoteExtension + def __init__(self, footnotes: FootnoteExtension) -> None: ... class FootnotePostprocessor(Postprocessor): - footnotes: Any - def __init__(self, footnotes) -> None: ... + footnotes: FootnoteExtension + def __init__(self, footnotes: FootnoteExtension) -> None: ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> FootnoteExtension: ... diff --git a/stubs/Markdown/markdown/extensions/legacy_attrs.pyi b/stubs/Markdown/markdown/extensions/legacy_attrs.pyi index 30f2e0ac6..410cf9e98 100644 --- a/stubs/Markdown/markdown/extensions/legacy_attrs.pyi +++ b/stubs/Markdown/markdown/extensions/legacy_attrs.pyi @@ -10,4 +10,4 @@ class LegacyAttrs(Treeprocessor): class LegacyAttrExtension(Extension): ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> LegacyAttrExtension: ... diff --git a/stubs/Markdown/markdown/extensions/legacy_em.pyi b/stubs/Markdown/markdown/extensions/legacy_em.pyi index dadaa8ee3..3ea44f44c 100644 --- a/stubs/Markdown/markdown/extensions/legacy_em.pyi +++ b/stubs/Markdown/markdown/extensions/legacy_em.pyi @@ -8,4 +8,4 @@ STRONG_EM_RE: str class LegacyUnderscoreProcessor(UnderscoreProcessor): ... class LegacyEmExtension(Extension): ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> LegacyEmExtension: ... diff --git a/stubs/Markdown/markdown/extensions/md_in_html.pyi b/stubs/Markdown/markdown/extensions/md_in_html.pyi index 2e7e9823b..7346ac914 100644 --- a/stubs/Markdown/markdown/extensions/md_in_html.pyi +++ b/stubs/Markdown/markdown/extensions/md_in_html.pyi @@ -4,4 +4,4 @@ from markdown.extensions import Extension class MarkdownInHtmlProcessor(BlockProcessor): ... class MarkdownInHtmlExtension(Extension): ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> MarkdownInHtmlExtension: ... diff --git a/stubs/Markdown/markdown/extensions/meta.pyi b/stubs/Markdown/markdown/extensions/meta.pyi index deca6e244..d3ef7e8d5 100644 --- a/stubs/Markdown/markdown/extensions/meta.pyi +++ b/stubs/Markdown/markdown/extensions/meta.pyi @@ -17,4 +17,4 @@ class MetaExtension(Extension): class MetaPreprocessor(Preprocessor): ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> MetaExtension: ... diff --git a/stubs/Markdown/markdown/extensions/nl2br.pyi b/stubs/Markdown/markdown/extensions/nl2br.pyi index 81dd1fb6f..914d39554 100644 --- a/stubs/Markdown/markdown/extensions/nl2br.pyi +++ b/stubs/Markdown/markdown/extensions/nl2br.pyi @@ -4,4 +4,4 @@ BR_RE: str class Nl2BrExtension(Extension): ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> Nl2BrExtension: ... diff --git a/stubs/Markdown/markdown/extensions/sane_lists.pyi b/stubs/Markdown/markdown/extensions/sane_lists.pyi index 31022c699..62404bee4 100644 --- a/stubs/Markdown/markdown/extensions/sane_lists.pyi +++ b/stubs/Markdown/markdown/extensions/sane_lists.pyi @@ -1,12 +1,13 @@ +from markdown import blockparser from markdown.blockprocessors import OListProcessor, UListProcessor from markdown.extensions import Extension class SaneOListProcessor(OListProcessor): - def __init__(self, parser) -> None: ... + def __init__(self, parser: blockparser.BlockParser) -> None: ... class SaneUListProcessor(UListProcessor): - def __init__(self, parser) -> None: ... + def __init__(self, parser: blockparser.BlockParser) -> None: ... class SaneListExtension(Extension): ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> SaneListExtension: ... diff --git a/stubs/Markdown/markdown/extensions/smarty.pyi b/stubs/Markdown/markdown/extensions/smarty.pyi index 6b237163d..d81870387 100644 --- a/stubs/Markdown/markdown/extensions/smarty.pyi +++ b/stubs/Markdown/markdown/extensions/smarty.pyi @@ -1,5 +1,9 @@ +from collections.abc import Sequence from typing import Any +from xml.etree.ElementTree import Element +from markdown import inlinepatterns, util +from markdown.core import Markdown from markdown.extensions import Extension from markdown.inlinepatterns import HtmlInlineProcessor @@ -24,16 +28,16 @@ remainingDoubleQuotesRegex: str HTML_STRICT_RE: str class SubstituteTextPattern(HtmlInlineProcessor): - replace: Any - def __init__(self, pattern, replace, md) -> None: ... + replace: Sequence[int | str | Element] + def __init__(self, pattern: str, replace: Sequence[int | str | Element], md: Markdown) -> None: ... class SmartyExtension(Extension): substitutions: Any def __init__(self, **kwargs) -> None: ... - def educateDashes(self, md) -> None: ... - def educateEllipses(self, md) -> None: ... - def educateAngledQuotes(self, md) -> None: ... - def educateQuotes(self, md) -> None: ... - inlinePatterns: Any + def educateDashes(self, md: Markdown) -> None: ... + def educateEllipses(self, md: Markdown) -> None: ... + def educateAngledQuotes(self, md: Markdown) -> None: ... + def educateQuotes(self, md: Markdown) -> None: ... + inlinePatterns: util.Registry[inlinepatterns.InlineProcessor] -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> SmartyExtension: ... diff --git a/stubs/Markdown/markdown/extensions/tables.pyi b/stubs/Markdown/markdown/extensions/tables.pyi index 26bb7c0e2..4c68c0583 100644 --- a/stubs/Markdown/markdown/extensions/tables.pyi +++ b/stubs/Markdown/markdown/extensions/tables.pyi @@ -1,6 +1,7 @@ from re import Pattern -from typing import ClassVar +from typing import Any, ClassVar +from markdown import blockparser from markdown.blockprocessors import BlockProcessor from markdown.extensions import Extension @@ -13,8 +14,8 @@ class TableProcessor(BlockProcessor): RE_END_BORDER: ClassVar[Pattern[str]] border: bool separator: str - def __init__(self, parser, config) -> None: ... + def __init__(self, parser: blockparser.BlockParser, config: dict[str, Any]) -> None: ... class TableExtension(Extension): ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> TableExtension: ... diff --git a/stubs/Markdown/markdown/extensions/toc.pyi b/stubs/Markdown/markdown/extensions/toc.pyi index 60c465f4c..223973c83 100644 --- a/stubs/Markdown/markdown/extensions/toc.pyi +++ b/stubs/Markdown/markdown/extensions/toc.pyi @@ -1,19 +1,30 @@ +from collections.abc import Iterator, MutableSet from re import Pattern from typing import Any +from typing_extensions import TypedDict +from xml.etree.ElementTree import Element from markdown.core import Markdown from markdown.extensions import Extension from markdown.treeprocessors import Treeprocessor -def slugify(value, separator): ... - IDCOUNT_RE: Pattern[str] -def unique(id, ids): ... -def get_name(el): ... -def stashedHTML2text(text, md, strip_entities: bool = True): ... -def unescape(text): ... -def nest_toc_tokens(toc_list): ... +class _FlatTocToken(TypedDict): + level: int + id: str + name: str + +class _TocToken(_FlatTocToken): + children: list[_TocToken] + +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 get_name(el: Element) -> str: ... +def stashedHTML2text(text: str, md: Markdown, strip_entities: bool = True) -> str: ... +def unescape(text: str) -> str: ... +def nest_toc_tokens(toc_list: list[_FlatTocToken]) -> list[_TocToken]: ... class TocTreeprocessor(Treeprocessor): marker: Any @@ -29,18 +40,18 @@ class TocTreeprocessor(Treeprocessor): header_rgx: Any toc_top: int toc_bottom: Any - def __init__(self, md, config) -> None: ... - def iterparent(self, node) -> None: ... - def replace_marker(self, root, elem) -> None: ... - def set_level(self, elem) -> None: ... - def add_anchor(self, c, elem_id) -> None: ... - def add_permalink(self, c, elem_id) -> None: ... - def build_toc_div(self, toc_list): ... + 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: ... + def set_level(self, elem: Element) -> None: ... + 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: ... class TocExtension(Extension): - TreeProcessorClass: Any + TreeProcessorClass: type[TocTreeprocessor] def __init__(self, **kwargs) -> None: ... md: Markdown def reset(self) -> None: ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> TocExtension: ... diff --git a/stubs/Markdown/markdown/extensions/wikilinks.pyi b/stubs/Markdown/markdown/extensions/wikilinks.pyi index 81c6455f2..d31983af8 100644 --- a/stubs/Markdown/markdown/extensions/wikilinks.pyi +++ b/stubs/Markdown/markdown/extensions/wikilinks.pyi @@ -4,14 +4,14 @@ from markdown.core import Markdown from markdown.extensions import Extension from markdown.inlinepatterns import InlineProcessor -def build_url(label, base, end): ... +def build_url(label: str, base: str, end: str) -> str: ... class WikiLinkExtension(Extension): def __init__(self, **kwargs) -> None: ... md: Markdown class WikiLinksInlineProcessor(InlineProcessor): - config: Any - def __init__(self, pattern, config) -> None: ... + config: dict[str, Any] + def __init__(self, pattern: str, config: dict[str, Any]) -> None: ... -def makeExtension(**kwargs): ... +def makeExtension(**kwargs) -> WikiLinkExtension: ... diff --git a/stubs/Markdown/markdown/inlinepatterns.pyi b/stubs/Markdown/markdown/inlinepatterns.pyi index 270f74a4b..e9be9657e 100644 --- a/stubs/Markdown/markdown/inlinepatterns.pyi +++ b/stubs/Markdown/markdown/inlinepatterns.pyi @@ -1,11 +1,12 @@ import re -from _typeshed import Incomplete +from collections.abc import Collection from typing import Any, ClassVar, NamedTuple from xml.etree.ElementTree import Element +from markdown import util from markdown.core import Markdown -def build_inlinepatterns(md, **kwargs): ... +def build_inlinepatterns(md: Markdown, **kwargs) -> util.Registry[InlineProcessor]: ... NOIMG: str BACKTICK_RE: str @@ -31,29 +32,27 @@ HTML_RE: str ENTITY_RE: str LINE_BREAK_RE: str -def dequote(string): ... +def dequote(string: str) -> str: ... -class _EmStrongItemBase(NamedTuple): +class EmStrongItem(NamedTuple): pattern: re.Pattern[str] builder: str tags: str -class EmStrongItem(_EmStrongItemBase): ... - class Pattern: - ANCESTOR_EXCLUDES: ClassVar[tuple[Incomplete, ...]] - pattern: Any + ANCESTOR_EXCLUDES: ClassVar[Collection[str]] + pattern: str compiled_re: re.Pattern[str] md: Markdown - def __init__(self, pattern, md: Markdown | None = None) -> None: ... - def getCompiledRegExp(self): ... + def __init__(self, pattern: str, md: Markdown | None = None) -> None: ... + def getCompiledRegExp(self) -> re.Pattern[str]: ... def handleMatch(self, m: re.Match[str]) -> str | Element | None: ... - def type(self): ... - def unescape(self, text): ... + def type(self) -> str: ... + def unescape(self, text: str) -> str: ... class InlineProcessor(Pattern): safe_mode: bool - def __init__(self, pattern, md: Markdown | None = None) -> None: ... + 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] class SimpleTextPattern(Pattern): ... @@ -62,11 +61,11 @@ class EscapeInlineProcessor(InlineProcessor): ... class SimpleTagPattern(Pattern): tag: Any - def __init__(self, pattern, tag) -> None: ... + def __init__(self, pattern: str, tag: str) -> None: ... class SimpleTagInlineProcessor(InlineProcessor): tag: Any - def __init__(self, pattern, tag) -> None: ... + def __init__(self, pattern: str, tag: str) -> None: ... class SubstituteTagPattern(SimpleTagPattern): ... class SubstituteTagInlineProcessor(SimpleTagInlineProcessor): ... @@ -74,7 +73,7 @@ class SubstituteTagInlineProcessor(SimpleTagInlineProcessor): ... class BacktickInlineProcessor(InlineProcessor): ESCAPED_BSLASH: Any tag: str - def __init__(self, pattern) -> None: ... + def __init__(self, pattern: str) -> None: ... class DoubleTagPattern(SimpleTagPattern): ... class DoubleTagInlineProcessor(SimpleTagInlineProcessor): ... @@ -82,26 +81,26 @@ class HtmlInlineProcessor(InlineProcessor): ... class AsteriskProcessor(InlineProcessor): PATTERNS: ClassVar[list[EmStrongItem]] - def build_single(self, m, tag, idx): ... - def build_double(self, m, tags, idx): ... - def build_double2(self, m, tags, idx): ... - def parse_sub_patterns(self, data, parent, last, idx) -> None: ... - def build_element(self, m, builder, tags, index): ... + def build_single(self, m: re.Match[str], tag: str, idx: int) -> Element: ... + def build_double(self, m: re.Match[str], tags: str, idx: int) -> Element: ... + def build_double2(self, m: re.Match[str], tags: str, idx: int) -> Element: ... + def parse_sub_patterns(self, data: str, parent: Element, last: Element | None, idx: int) -> None: ... + def build_element(self, m: re.Match[str], builder: str, tags: str, index: int) -> Element: ... class UnderscoreProcessor(AsteriskProcessor): ... class LinkInlineProcessor(InlineProcessor): RE_LINK: ClassVar[re.Pattern[str]] RE_TITLE_CLEAN: ClassVar[re.Pattern[str]] - def getLink(self, data, index): ... - def getText(self, data, index): ... + def getLink(self, data: str, index: int): ... + def getText(self, data: str, index: int): ... class ImageInlineProcessor(LinkInlineProcessor): ... class ReferenceInlineProcessor(LinkInlineProcessor): NEWLINE_CLEANUP_RE: ClassVar[re.Pattern[str]] - def evalId(self, data, index, text): ... - def makeTag(self, href, title, text): ... + def evalId(self, data: str, index: int, text: str): ... + def makeTag(self, href: str, title: str, text: str) -> Element: ... class ShortReferenceInlineProcessor(ReferenceInlineProcessor): ... class ImageReferenceInlineProcessor(ReferenceInlineProcessor): ... diff --git a/stubs/Markdown/markdown/postprocessors.pyi b/stubs/Markdown/markdown/postprocessors.pyi index 6b14388b6..ad6e2a20d 100644 --- a/stubs/Markdown/markdown/postprocessors.pyi +++ b/stubs/Markdown/markdown/postprocessors.pyi @@ -1,18 +1,14 @@ -from re import Pattern -from typing import Any +from markdown.core import Markdown from . import util -def build_postprocessors(md, **kwargs): ... +def build_postprocessors(md: Markdown, **kwargs) -> util.Registry[Postprocessor]: ... class Postprocessor(util.Processor): - def run(self, text) -> Any: ... + def run(self, text: str) -> str: ... class RawHtmlPostprocessor(Postprocessor): - def isblocklevel(self, html): ... + def isblocklevel(self, html: str) -> bool: ... + def stash_to_string(self, text: str) -> str: ... class AndSubstitutePostprocessor(Postprocessor): ... - -class UnescapePostprocessor(Postprocessor): # deprecated - RE: Pattern[str] - def unescape(self, m): ... diff --git a/stubs/Markdown/markdown/preprocessors.pyi b/stubs/Markdown/markdown/preprocessors.pyi index 52702d7ed..4a4523171 100644 --- a/stubs/Markdown/markdown/preprocessors.pyi +++ b/stubs/Markdown/markdown/preprocessors.pyi @@ -1,8 +1,10 @@ from typing import Any +from markdown.core import Markdown + from . import util -def build_preprocessors(md, **kwargs): ... +def build_preprocessors(md: Markdown, **kwargs) -> util.Registry[Preprocessor]: ... class Preprocessor(util.Processor): def run(self, lines: list[str]) -> list[str]: ... diff --git a/stubs/Markdown/markdown/serializers.pyi b/stubs/Markdown/markdown/serializers.pyi index c775a409f..b2e0a5ed5 100644 --- a/stubs/Markdown/markdown/serializers.pyi +++ b/stubs/Markdown/markdown/serializers.pyi @@ -1,2 +1,4 @@ -def to_html_string(element): ... -def to_xhtml_string(element): ... +from xml.etree.ElementTree import Element + +def to_html_string(element: Element) -> str: ... +def to_xhtml_string(element: Element) -> str: ... diff --git a/stubs/Markdown/markdown/treeprocessors.pyi b/stubs/Markdown/markdown/treeprocessors.pyi index 5e9e7936e..21edf41f9 100644 --- a/stubs/Markdown/markdown/treeprocessors.pyi +++ b/stubs/Markdown/markdown/treeprocessors.pyi @@ -1,12 +1,13 @@ -from _typeshed import Incomplete from re import Pattern from typing import Any, ClassVar +from typing_extensions import TypeGuard from xml.etree.ElementTree import Element -from . import util +from markdown import util +from markdown.core import Markdown -def build_treeprocessors(md, **kwargs): ... -def isString(s): ... +def build_treeprocessors(md: Markdown, **kwargs) -> util.Registry[Treeprocessor]: ... +def isString(s: object) -> TypeGuard[str]: ... class Treeprocessor(util.Processor): def run(self, root: Element) -> Element | None: ... @@ -17,7 +18,7 @@ class InlineProcessor(Treeprocessor): def __init__(self, md) -> None: ... stashed_nodes: Any parent_map: Any - def run(self, tree: Element, ancestors: Incomplete | None = None) -> Element: ... + def run(self, tree: Element, ancestors: list[str] | None = None) -> Element: ... class PrettifyTreeprocessor(Treeprocessor): ... diff --git a/stubs/Markdown/markdown/util.pyi b/stubs/Markdown/markdown/util.pyi index 7482313b6..6817e8ed3 100644 --- a/stubs/Markdown/markdown/util.pyi +++ b/stubs/Markdown/markdown/util.pyi @@ -1,8 +1,12 @@ +from collections.abc import Iterator from re import Pattern -from typing import Any, overload +from typing import Any, Generic, TypeVar, overload +from typing_extensions import TypedDict from markdown.core import Markdown +_T = TypeVar("_T") + BLOCK_LEVEL_ELEMENTS: Any STX: str ETX: str @@ -16,7 +20,10 @@ TAG_PLACEHOLDER: Any RTL_BIDI_RANGES: Any def deprecated(message: str, stacklevel: int = 2): ... -def parseBoolValue(value: object, fail_on_errors: bool = True, preserve_none: bool = False) -> bool | None: ... +@overload +def parseBoolValue(value: str) -> bool: ... +@overload +def parseBoolValue(value: str | None, fail_on_errors: bool = True, preserve_none: bool = False) -> bool | None: ... def code_escape(text: str) -> str: ... def nearing_recursion_limit() -> bool: ... @@ -26,26 +33,32 @@ class Processor: md: Markdown def __init__(self, md: Markdown | None = None) -> None: ... +class _TagData(TypedDict): + tag: str + attrs: dict[str, str] + left_index: int + right_index: int + class HtmlStash: html_counter: int rawHtmlBlocks: list[str] tag_counter: int - tag_data: list[dict[str, Any]] + tag_data: list[_TagData] def __init__(self) -> None: ... def store(self, html: str) -> str: ... def reset(self) -> None: ... def get_placeholder(self, key: int) -> str: ... - def store_tag(self, tag: str, attrs: list[Any], left_index: int, right_index: int) -> str: ... + def store_tag(self, tag: str, attrs: dict[str, str], left_index: int, right_index: int) -> str: ... -class Registry: +class Registry(Generic[_T]): def __init__(self) -> None: ... - def __contains__(self, item: object) -> bool: ... - def __iter__(self) -> Any: ... + def __contains__(self, item: str | _T) -> bool: ... + def __iter__(self) -> Iterator[_T]: ... @overload - def __getitem__(self, key: slice) -> Registry: ... + def __getitem__(self, key: slice) -> Registry[_T]: ... @overload - def __getitem__(self, key: str | int) -> Any: ... + def __getitem__(self, key: str | int) -> _T: ... def __len__(self) -> int: ... def get_index_for_name(self, name: str) -> int: ... - def register(self, item: Any, name: str, priority: float) -> None: ... + def register(self, item: _T, name: str, priority: float) -> None: ... def deregister(self, name: str, strict: bool = True) -> None: ...