22
33from collections .abc import Callable , Generator , Iterable , Mapping , MutableMapping
44from contextlib import contextmanager
5- from typing import Any
5+ from typing import Any , Literal , overload
66
77from . import helpers , presets # noqa F401
88from .common import normalize_url , utils # noqa F401
1212from .renderer import RendererHTML , RendererProtocol
1313from .rules_core .state_core import StateCore
1414from .token import Token
15- from .utils import OptionsDict
15+ from .utils import EnvType , OptionsDict , OptionsType , PresetType
1616
1717try :
1818 import linkify_it
1919except ModuleNotFoundError :
2020 linkify_it = None
2121
2222
23- _PRESETS = {
23+ _PRESETS : dict [ str , PresetType ] = {
2424 "default" : presets .default .make (),
2525 "js-default" : presets .js_default .make (),
2626 "zero" : presets .zero .make (),
3232class MarkdownIt :
3333 def __init__ (
3434 self ,
35- config : str | Mapping = "commonmark" ,
36- options_update : Mapping | None = None ,
35+ config : str | PresetType = "commonmark" ,
36+ options_update : Mapping [ str , Any ] | None = None ,
3737 * ,
3838 renderer_cls : Callable [[MarkdownIt ], RendererProtocol ] = RendererHTML ,
3939 ):
@@ -67,6 +67,26 @@ def __init__(
6767 def __repr__ (self ) -> str :
6868 return f"{ self .__class__ .__module__ } .{ self .__class__ .__name__ } ()"
6969
70+ @overload
71+ def __getitem__ (self , name : Literal ["inline" ]) -> ParserInline :
72+ ...
73+
74+ @overload
75+ def __getitem__ (self , name : Literal ["block" ]) -> ParserBlock :
76+ ...
77+
78+ @overload
79+ def __getitem__ (self , name : Literal ["core" ]) -> ParserCore :
80+ ...
81+
82+ @overload
83+ def __getitem__ (self , name : Literal ["renderer" ]) -> RendererProtocol :
84+ ...
85+
86+ @overload
87+ def __getitem__ (self , name : str ) -> Any :
88+ ...
89+
7090 def __getitem__ (self , name : str ) -> Any :
7191 return {
7292 "inline" : self .inline ,
@@ -75,7 +95,7 @@ def __getitem__(self, name: str) -> Any:
7595 "renderer" : self .renderer ,
7696 }[name ]
7797
78- def set (self , options : MutableMapping ) -> None :
98+ def set (self , options : OptionsType ) -> None :
7999 """Set parser options (in the same format as in constructor).
80100 Probably, you will never need it, but you can change options after constructor call.
81101
@@ -86,7 +106,7 @@ def set(self, options: MutableMapping) -> None:
86106 self .options = OptionsDict (options )
87107
88108 def configure (
89- self , presets : str | Mapping , options_update : Mapping | None = None
109+ self , presets : str | PresetType , options_update : Mapping [ str , Any ] | None = None
90110 ) -> MarkdownIt :
91111 """Batch load of all options and component settings.
92112 This is an internal method, and you probably will not need it.
@@ -108,9 +128,9 @@ def configure(
108128
109129 options = config .get ("options" , {}) or {}
110130 if options_update :
111- options = {** options , ** options_update }
131+ options = {** options , ** options_update } # type: ignore
112132
113- self .set (options )
133+ self .set (options ) # type: ignore
114134
115135 if "components" in config :
116136 for name , component in config ["components" ].items ():
@@ -206,15 +226,19 @@ def reset_rules(self) -> Generator[None, None, None]:
206226 self [chain ].ruler .enableOnly (rules )
207227 self .inline .ruler2 .enableOnly (chain_rules ["inline2" ])
208228
209- def add_render_rule (self , name : str , function : Callable , fmt : str = "html" ) -> None :
229+ def add_render_rule (
230+ self , name : str , function : Callable [..., Any ], fmt : str = "html"
231+ ) -> None :
210232 """Add a rule for rendering a particular Token type.
211233
212234 Only applied when ``renderer.__output__ == fmt``
213235 """
214236 if self .renderer .__output__ == fmt :
215237 self .renderer .rules [name ] = function .__get__ (self .renderer ) # type: ignore
216238
217- def use (self , plugin : Callable , * params , ** options ) -> MarkdownIt :
239+ def use (
240+ self , plugin : Callable [..., None ], * params : Any , ** options : Any
241+ ) -> MarkdownIt :
218242 """Load specified plugin with given params into current parser instance. (chainable)
219243
220244 It's just a sugar to call `plugin(md, params)` with curring.
@@ -229,7 +253,7 @@ def func(tokens, idx):
229253 plugin (self , * params , ** options )
230254 return self
231255
232- def parse (self , src : str , env : MutableMapping | None = None ) -> list [Token ]:
256+ def parse (self , src : str , env : EnvType | None = None ) -> list [Token ]:
233257 """Parse the source string to a token stream
234258
235259 :param src: source string
@@ -252,7 +276,7 @@ def parse(self, src: str, env: MutableMapping | None = None) -> list[Token]:
252276 self .core .process (state )
253277 return state .tokens
254278
255- def render (self , src : str , env : MutableMapping | None = None ) -> Any :
279+ def render (self , src : str , env : EnvType | None = None ) -> Any :
256280 """Render markdown string into html. It does all magic for you :).
257281
258282 :param src: source string
@@ -266,7 +290,7 @@ def render(self, src: str, env: MutableMapping | None = None) -> Any:
266290 env = {} if env is None else env
267291 return self .renderer .render (self .parse (src , env ), self .options , env )
268292
269- def parseInline (self , src : str , env : MutableMapping | None = None ) -> list [Token ]:
293+ def parseInline (self , src : str , env : EnvType | None = None ) -> list [Token ]:
270294 """The same as [[MarkdownIt.parse]] but skip all block rules.
271295
272296 :param src: source string
@@ -286,7 +310,7 @@ def parseInline(self, src: str, env: MutableMapping | None = None) -> list[Token
286310 self .core .process (state )
287311 return state .tokens
288312
289- def renderInline (self , src : str , env : MutableMapping | None = None ) -> Any :
313+ def renderInline (self , src : str , env : EnvType | None = None ) -> Any :
290314 """Similar to [[MarkdownIt.render]] but for single paragraph content.
291315
292316 :param src: source string
0 commit comments