From 2a30462c4aa1b4a62e59be38f7d1d9b18afa84ea Mon Sep 17 00:00:00 2001 From: Israel Tsadok Date: Wed, 26 Sep 2018 06:26:23 +0300 Subject: [PATCH] The filter function may return str, tuple or list (#2472) In Python 2, the return type of filter depends on the iterable parameter. --- stdlib/2/__builtin__.pyi | 17 +++++++++++++---- stdlib/2/builtins.pyi | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index f0f999353..4dc1b434b 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -739,11 +739,20 @@ def divmod(a: int, b: int) -> Tuple[int, int]: ... def divmod(a: float, b: float) -> Tuple[float, float]: ... def exit(code: Any = ...) -> NoReturn: ... @overload -def filter(function: None, - iterable: Iterable[Optional[_T]]) -> List[_T]: ... +def filter(__function: Callable[[AnyStr], Any], # type: ignore + __iterable: AnyStr) -> AnyStr: ... @overload -def filter(function: Callable[[_T], Any], - iterable: Iterable[_T]) -> List[_T]: ... +def filter(__function: None, # type: ignore + __iterable: Tuple[Optional[_T], ...]) -> Tuple[_T, ...]: ... +@overload +def filter(__function: Callable[[_T], Any], # type: ignore + __iterable: Tuple[_T, ...]) -> Tuple[_T, ...]: ... +@overload +def filter(__function: None, + __iterable: Iterable[Optional[_T]]) -> List[_T]: ... +@overload +def filter(__function: Callable[[_T], Any], + __iterable: Iterable[_T]) -> List[_T]: ... def format(o: object, format_spec: str = ...) -> str: ... # TODO unicode def getattr(o: Any, name: unicode, default: Optional[Any] = ...) -> Any: ... def hasattr(o: Any, name: unicode) -> bool: ... diff --git a/stdlib/2/builtins.pyi b/stdlib/2/builtins.pyi index f0f999353..4dc1b434b 100644 --- a/stdlib/2/builtins.pyi +++ b/stdlib/2/builtins.pyi @@ -739,11 +739,20 @@ def divmod(a: int, b: int) -> Tuple[int, int]: ... def divmod(a: float, b: float) -> Tuple[float, float]: ... def exit(code: Any = ...) -> NoReturn: ... @overload -def filter(function: None, - iterable: Iterable[Optional[_T]]) -> List[_T]: ... +def filter(__function: Callable[[AnyStr], Any], # type: ignore + __iterable: AnyStr) -> AnyStr: ... @overload -def filter(function: Callable[[_T], Any], - iterable: Iterable[_T]) -> List[_T]: ... +def filter(__function: None, # type: ignore + __iterable: Tuple[Optional[_T], ...]) -> Tuple[_T, ...]: ... +@overload +def filter(__function: Callable[[_T], Any], # type: ignore + __iterable: Tuple[_T, ...]) -> Tuple[_T, ...]: ... +@overload +def filter(__function: None, + __iterable: Iterable[Optional[_T]]) -> List[_T]: ... +@overload +def filter(__function: Callable[[_T], Any], + __iterable: Iterable[_T]) -> List[_T]: ... def format(o: object, format_spec: str = ...) -> str: ... # TODO unicode def getattr(o: Any, name: unicode, default: Optional[Any] = ...) -> Any: ... def hasattr(o: Any, name: unicode) -> bool: ...