Skip to content

👌 IMPROVE: Type annotate common/utils #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions markdown_it/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"""
import html
import re
from typing import Any

from .entities import entities

# from .normalize_url import unescape_string


def charCodeAt(src: str, pos: int):
def charCodeAt(src: str, pos: int) -> Any:
Copy link
Contributor Author

@hukkin hukkin Apr 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Any return type is to explicitly keep disabling type checking for the return value. This because the None case is pretty painful and dynamic: it happens when pos >= len(src) but in code, before calling the function, we always have dynamic checks to ensure this doesn't happen. Thus the proper Optional[int] annotation requires many ugly cast(int, ...)s.

Best would be to raise IndexError instead of returning None, but I guess that isn't true to JS upstream, so IMO this is the second best option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeh I'm not picking at that thread right now 😬

"""
Returns the Unicode value of the character at the specified location.

Expand All @@ -26,12 +27,11 @@ def charCodeAt(src: str, pos: int):
# function _class(obj) { return Object.prototype.toString.call(obj); }


def isString(obj):
def isString(obj: object) -> bool:
return isinstance(obj, str)


def has(object, key):
return hasattr(object, key)
has = hasattr


# Merge objects
Expand All @@ -56,18 +56,18 @@ def assign(obj):
# return obj


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


######################################################################


def isValidEntityCode(c):
def isValidEntityCode(c: int) -> bool:

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


def replaceEntityPattern(match, name):
def replaceEntityPattern(match: str, name: str) -> str:
"""Convert HTML entity patterns

::
Expand All @@ -138,22 +138,22 @@ def replaceEntityPattern(match, name):
# return string.replace(ENTITY_RE, replaceEntityPattern)


def unescapeMd(string: str):
def unescapeMd(string: str) -> str:
raise NotImplementedError
# if "\\" in string:
# return string
# return string.replace(UNESCAPE_MD_RE, "$1")


def unescapeAll(string: str):
def unescapeAll(string: str) -> str:
return html.unescape(string)


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


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

Expand All @@ -179,7 +179,7 @@ def stripEscape(string):
# return string


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

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


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


Expand Down Expand Up @@ -271,7 +271,7 @@ def isPunctChar(ch: str) -> bool:
}


def isMdAsciiPunct(ch: int):
def isMdAsciiPunct(ch: int) -> bool:
"""Markdown ASCII punctuation characters.

::
Expand Down