Skip to content

Commit e99db3d

Browse files
committed
Markdown: add fake base class to Pattern to fix overrides
Currently `InlineProcessor` and every subclass of it violates the `override` rule as per Liskov Substitution Principle. Signature of "handleMatch" incompatible with supertype "Pattern" [override] Superclass: def handleMatch(self, m: Match[str]) -> Element | str Subclass: def handleMatch(self, m: Match[str], data: str) -> tuple[Element | str | None, int | None, int | None] For all *usage* purposes these aren't actual subclasses to one another, they're handled totally separately inside `__applyPattern`. However yes, this change totally disagrees with actual `isinstance` checks at run time (though even that is still for the best).
1 parent 643d911 commit e99db3d

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

stubs/Markdown/markdown/core.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class _ReadableStream(Protocol):
2121

2222
class Markdown:
2323
preprocessors: Registry[preprocessors.Preprocessor]
24-
inlinePatterns: Registry[inlinepatterns.Pattern]
24+
inlinePatterns: Registry[inlinepatterns.InlineProcessor | inlinepatterns.Pattern]
2525
treeprocessors: Registry[treeprocessors.Treeprocessor]
2626
postprocessors: Registry[postprocessors.Postprocessor]
2727
parser: blockparser.BlockParser

stubs/Markdown/markdown/extensions/smarty.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ class SmartyExtension(Extension):
3838
def educateEllipses(self, md: Markdown) -> None: ...
3939
def educateAngledQuotes(self, md: Markdown) -> None: ...
4040
def educateQuotes(self, md: Markdown) -> None: ...
41-
inlinePatterns: util.Registry[inlinepatterns.Pattern]
41+
inlinePatterns: util.Registry[inlinepatterns.InlineProcessor | inlinepatterns.Pattern]
4242

4343
def makeExtension(**kwargs) -> SmartyExtension: ...

stubs/Markdown/markdown/inlinepatterns.pyi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from xml.etree.ElementTree import Element
66
from markdown import util
77
from markdown.core import Markdown
88

9-
def build_inlinepatterns(md: Markdown, **kwargs) -> util.Registry[Pattern]: ...
9+
def build_inlinepatterns(md: Markdown, **kwargs) -> util.Registry[InlineProcessor | Pattern]: ...
1010

1111
NOIMG: str
1212
BACKTICK_RE: str
@@ -39,21 +39,23 @@ class EmStrongItem(NamedTuple):
3939
builder: str
4040
tags: str
4141

42-
class Pattern:
42+
class _BasePattern:
4343
ANCESTOR_EXCLUDES: ClassVar[Collection[str]]
4444
pattern: str
4545
compiled_re: re.Pattern[str]
4646
md: Markdown
4747
def __init__(self, pattern: str, md: Markdown | None = None) -> None: ...
4848
def getCompiledRegExp(self) -> re.Pattern[str]: ...
49-
def handleMatch(self, m: re.Match[str]) -> str | Element | None: ...
5049
def type(self) -> str: ...
5150
def unescape(self, text: str) -> str: ...
5251

53-
class InlineProcessor(Pattern):
52+
class Pattern(_BasePattern):
53+
def handleMatch(self, m: re.Match[str]) -> str | Element | None: ...
54+
55+
class InlineProcessor(_BasePattern):
5456
safe_mode: bool
5557
def __init__(self, pattern: str, md: Markdown | None = None) -> None: ...
56-
def handleMatch(self, m: re.Match[str], data) -> tuple[Element, int, int] | tuple[None, None, None]: ... # type: ignore[override]
58+
def handleMatch(self, m: re.Match[str], data: str) -> tuple[Element, int, int] | tuple[None, None, None]: ...
5759

5860
class SimpleTextPattern(Pattern): ...
5961
class SimpleTextInlineProcessor(InlineProcessor): ...

0 commit comments

Comments
 (0)