From dd0bb17c00bc701cf43d2552e96abe30a1682690 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sat, 21 Jul 2018 18:29:59 -0400 Subject: [PATCH] escape: Type-annotate everything --- setup.cfg | 3 ++ tornado/escape.py | 80 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/setup.cfg b/setup.cfg index c5fc02484..5ea4c8755 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,3 +6,6 @@ disallow_untyped_defs = True [mypy-tornado.httputil] disallow_untyped_defs = True + +[mypy-tornado.escape] +disallow_untyped_defs = True diff --git a/tornado/escape.py b/tornado/escape.py index 17754b361..8bf4d1d53 100644 --- a/tornado/escape.py +++ b/tornado/escape.py @@ -26,7 +26,8 @@ import urllib.parse from tornado.util import unicode_type, basestring_type -import typing # noqa +import typing +from typing import Union, Any, Optional, Dict, List, Callable _XHTML_ESCAPE_RE = re.compile('[&<>"\']') @@ -34,7 +35,7 @@ _XHTML_ESCAPE_DICT = {'&': '&', '<': '<', '>': '>', '"': '"', '\'': '''} -def xhtml_escape(value): +def xhtml_escape(value: Union[str, bytes]) -> str: """Escapes a string so it is valid within HTML or XML. Escapes the characters ``<``, ``>``, ``"``, ``'``, and ``&``. @@ -49,7 +50,7 @@ def xhtml_escape(value): to_basestring(value)) -def xhtml_unescape(value): +def xhtml_unescape(value: Union[str, bytes]) -> str: """Un-escapes an XML-escaped string.""" return re.sub(r"&(#?)(\w+?);", _convert_entity, _unicode(value)) @@ -57,7 +58,7 @@ def xhtml_unescape(value): # The fact that json_encode wraps json.dumps is an implementation detail. # Please see https://github.com/tornadoweb/tornado/pull/706 # before sending a pull request that adds **kwargs to this function. -def json_encode(value): +def json_encode(value: Any) -> str: """JSON-encodes the given Python object.""" # JSON permits but does not require forward slashes to be escaped. # This is useful when json data is emitted in a