Skip to content

Commit c55a243

Browse files
authored
👌 IMPROVE: Type annotate common/utils (#160)
1 parent 53d9b33 commit c55a243

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

markdown_it/common/utils.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"""
33
import html
44
import re
5+
from typing import Any
56

67
from .entities import entities
78

89
# from .normalize_url import unescape_string
910

1011

11-
def charCodeAt(src: str, pos: int):
12+
def charCodeAt(src: str, pos: int) -> Any:
1213
"""
1314
Returns the Unicode value of the character at the specified location.
1415
@@ -26,12 +27,11 @@ def charCodeAt(src: str, pos: int):
2627
# function _class(obj) { return Object.prototype.toString.call(obj); }
2728

2829

29-
def isString(obj):
30+
def isString(obj: object) -> bool:
3031
return isinstance(obj, str)
3132

3233

33-
def has(object, key):
34-
return hasattr(object, key)
34+
has = hasattr
3535

3636

3737
# Merge objects
@@ -56,18 +56,18 @@ def assign(obj):
5656
# return obj
5757

5858

59-
def arrayReplaceAt(src: list, pos: int, newElements: list):
59+
def arrayReplaceAt(src: list, pos: int, newElements: list) -> list:
6060
"""
6161
Remove element from array and put another array at those position.
6262
Useful for some operations with tokens
6363
"""
64-
return src[0:pos] + newElements + src[pos + 1 :]
64+
return src[:pos] + newElements + src[pos + 1 :]
6565

6666

6767
######################################################################
6868

6969

70-
def isValidEntityCode(c):
70+
def isValidEntityCode(c: int) -> bool:
7171

7272
# broken sequence
7373
if c >= 0xD800 and c <= 0xDFFF:
@@ -111,7 +111,7 @@ def fromCodePoint(c: int) -> str:
111111
DIGITAL_ENTITY_TEST_RE = re.compile(r"^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))", re.IGNORECASE)
112112

113113

114-
def replaceEntityPattern(match, name):
114+
def replaceEntityPattern(match: str, name: str) -> str:
115115
"""Convert HTML entity patterns
116116
117117
::
@@ -138,22 +138,22 @@ def replaceEntityPattern(match, name):
138138
# return string.replace(ENTITY_RE, replaceEntityPattern)
139139

140140

141-
def unescapeMd(string: str):
141+
def unescapeMd(string: str) -> str:
142142
raise NotImplementedError
143143
# if "\\" in string:
144144
# return string
145145
# return string.replace(UNESCAPE_MD_RE, "$1")
146146

147147

148-
def unescapeAll(string: str):
148+
def unescapeAll(string: str) -> str:
149149
return html.unescape(string)
150150

151151

152152
ESCAPABLE = r"""\\!"#$%&'()*+,./:;<=>?@\[\]^`{}|_~-"""
153153
ESCAPE_CHAR = re.compile(r"\\([" + ESCAPABLE + r"])")
154154

155155

156-
def stripEscape(string):
156+
def stripEscape(string: str) -> str:
157157
"""Strip escape \\ characters"""
158158
return ESCAPE_CHAR.sub(r"\1", string)
159159

@@ -179,7 +179,7 @@ def stripEscape(string):
179179
# return string
180180

181181

182-
def escapeHtml(raw: str):
182+
def escapeHtml(raw: str) -> str:
183183
# return html.escape(html.unescape(raw)).replace("&#x27;", "'")
184184
return html.escape(raw).replace("&#x27;", "'")
185185

@@ -197,7 +197,7 @@ def escapeRE(string: str) -> str:
197197
# //////////////////////////////////////////////////////////////////////////////
198198

199199

200-
def isSpace(code) -> bool:
200+
def isSpace(code: object) -> bool:
201201
return code in {0x09, 0x20}
202202

203203

@@ -271,7 +271,7 @@ def isPunctChar(ch: str) -> bool:
271271
}
272272

273273

274-
def isMdAsciiPunct(ch: int):
274+
def isMdAsciiPunct(ch: int) -> bool:
275275
"""Markdown ASCII punctuation characters.
276276
277277
::

0 commit comments

Comments
 (0)