From 041286e45b2ace7a8c117f55b72f14ec854c93b7 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Fri, 15 Mar 2019 18:23:21 -0700 Subject: [PATCH 1/2] Add unicode support to py2 string.Template. I played around with this in a 2.7 interpreter, and as far as I can tell, the template and mapping keys can be str or unicode, the return type is str if all of the mapping values are str, and the return type is unicode if at least one mappng value is unicode. --- stdlib/2/string.pyi | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/stdlib/2/string.pyi b/stdlib/2/string.pyi index 751fd28a064f..0fe2f1772d37 100644 --- a/stdlib/2/string.pyi +++ b/stdlib/2/string.pyi @@ -2,7 +2,7 @@ # Based on http://docs.python.org/3.2/library/string.html -from typing import Mapping, Sequence, Any, Optional, Union, List, Tuple, Iterable, AnyStr +from typing import Any, AnyStr, Iterable, List, Mapping, Optional, overload, Sequence, Text, Tuple, Union ascii_letters = ... # type: str ascii_lowercase = ... # type: str @@ -48,13 +48,17 @@ def zfill(s: AnyStr, width: int) -> AnyStr: ... def replace(s: AnyStr, old: AnyStr, new: AnyStr, maxreplace: int = ...) -> AnyStr: ... class Template(object): - # TODO: Unicode support? - template = ... # type: str + template: Text - def __init__(self, template: str) -> None: ... - def substitute(self, mapping: Mapping[str, str] = ..., **kwds: str) -> str: ... - def safe_substitute(self, mapping: Mapping[str, str] = ..., - **kwds: str) -> str: ... + def __init__(self, template: Text) -> None: ... + @overload + def substitute(self, mapping: Mapping[Text, str] = ..., **kwds: str) -> str: ... + @overload + def substitute(self, mapping: Mapping[Text, Text] = ..., **kwds: Text) -> Text: ... + @overload + def safe_substitute(self, mapping: Mapping[Text, str] = ..., **kwds: str) -> str: ... + @overload + def safe_substitute(self, mapping: Mapping[Text, Text] = ..., **kwds: Text) -> Text: ... # TODO(MichalPokorny): This is probably badly and/or loosely typed. class Formatter(object): From bc27448ae90090eeb95b2dbdb307a19d40d7ccb3 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Sat, 16 Mar 2019 12:01:27 -0700 Subject: [PATCH 2/2] Make string.Template an old-style class. --- stdlib/2/string.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/2/string.pyi b/stdlib/2/string.pyi index 0fe2f1772d37..fc06f1a0c810 100644 --- a/stdlib/2/string.pyi +++ b/stdlib/2/string.pyi @@ -47,7 +47,7 @@ def center(s: AnyStr, width: int, fillchar: AnyStr = ...) -> AnyStr: ... def zfill(s: AnyStr, width: int) -> AnyStr: ... def replace(s: AnyStr, old: AnyStr, new: AnyStr, maxreplace: int = ...) -> AnyStr: ... -class Template(object): +class Template: template: Text def __init__(self, template: Text) -> None: ...