Refine str.maketrans and str.translate (#1613)

str.translate requires a Mapping or Sequence (in essence, anything
with __getitem__), not a Dict.

str.maketrans in the one-argument form only converts character string
keys to their unicode ordinal, leaving any of the values untouched.
This mapping may use both integers or strings as keys at the same time.

str.maketrans in the multi-argument form returns a dict with any of the
values str, int or None, as recognized by str.translate.
This commit is contained in:
FichteFoll
2017-11-08 03:55:05 +01:00
committed by Jelle Zijlstra
parent c7bc0bdb00
commit f933b9384c
3 changed files with 7 additions and 7 deletions

View File

@@ -278,15 +278,15 @@ class str(Sequence[str]):
def strip(self, chars: Optional[str] = None) -> str: ...
def swapcase(self) -> str: ...
def title(self) -> str: ...
def translate(self, table: Dict[int, Any]) -> str: ...
def translate(self, table: Union[Mapping[int, Union[int, str, None]], Sequence[Union[int, str, None]]]) -> str: ...
def upper(self) -> str: ...
def zfill(self, width: int) -> str: ...
@staticmethod
@overload
def maketrans(x: Union[Dict[int, Any], Dict[str, Any]]) -> Dict[int, Any]: ...
def maketrans(x: Union[Dict[int, _T], Dict[str, _T], Dict[Union[str, int], _T]]) -> Dict[int, _T]: ...
@staticmethod
@overload
def maketrans(x: str, y: str, z: str = ...) -> Dict[int, Any]: ...
def maketrans(x: str, y: str, z: str = ...) -> Dict[int, Union[int, None]]: ...
def __getitem__(self, i: Union[int, slice]) -> str: ...
def __add__(self, s: str) -> str: ...

View File

@@ -159,10 +159,10 @@ class UserString(Sequence[str]):
if sys.version_info >= (3, 5):
@staticmethod
@overload
def maketrans(x: Union[Dict[int, Any], Dict[str, Any]]) -> Dict[int, Any]: ...
def maketrans(x: Union[Dict[int, _T], Dict[str, _T], Dict[Union[str, int], _T]]) -> Dict[int, _T]: ...
@staticmethod
@overload
def maketrans(x: str, y: str, z: str = ...) -> Dict[int, Any]: ...
def maketrans(x: str, y: str, z: str = ...) -> Dict[int, Union[int, None]]: ...
def partition(self, sep: str) -> Tuple[str, str, str]: ...
def replace(self: _UserStringT, old: Union[str, UserString], new: Union[str, UserString], maxsplit: int = ...) -> _UserStringT: ...
def rfind(self, sub: Union[str, UserString], start: int = ..., end: int = ...) -> int: ...

View File

@@ -1,6 +1,6 @@
import sys
from typing import Any, Callable, Dict, Iterable, List, Optional, Text, Tuple, Union
from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Text, Tuple, Union
from collections import Mapping
from markupsafe._compat import text_type
import string
@@ -42,7 +42,7 @@ class Markup(text_type):
def strip(self, chars: Optional[text_type] = None) -> Markup: ...
def center(self, width: int, fillchar: text_type = ...) -> Markup: ...
def zfill(self, width: int) -> Markup: ...
def translate(self, table: Union[Dict[int, Any], text_type]) -> Markup: ...
def translate(self, table: Union[Mapping[int, Union[int, text_type, None]], Sequence[Union[int, text_type, None]]]) -> Markup: ...
def expandtabs(self, tabsize: int = 8) -> Markup: ...
class EscapeFormatter(string.Formatter):