From 9947c9d5134f9d19ec6e008682e7c2bb9fbf6e10 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 4 Jul 2017 19:20:23 -0700 Subject: [PATCH] urlparse: allow unicode arguments in more places (#1451) * urlparse: allow unicode arguments in more places Part of #1411 urlunsplit() and similar functions accept either unicode or str in all places. Their actual return type is unicode if any of the arguments is unicode and nonempty, which the type system can't exactly express. I left the return type as str because str is implicitly promoted to unicode, so using the return type in a place that accepts unicode should work. unquote, parse_qs, and parse_qsl return unicode if you pass them unicode, so AnyStr is appropriate in their stubs. * fix type for urldefrag The return type was wrong; this function returns a 2-tuple. The second member of the tuple is always a `str` if the argument type does not contain '#', and otherwise matches the type of the argument. --- stdlib/2/urlparse.pyi | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/stdlib/2/urlparse.pyi b/stdlib/2/urlparse.pyi index 9d0a7021d..169de2ff0 100644 --- a/stdlib/2/urlparse.pyi +++ b/stdlib/2/urlparse.pyi @@ -1,6 +1,8 @@ # Stubs for urlparse (Python 2) -from typing import Dict, List, NamedTuple, Tuple, Sequence, Union, overload +from typing import AnyStr, Dict, List, NamedTuple, Tuple, Sequence, Union, overload + +_String = Union[str, unicode] uses_relative = ... # type: List[str] uses_netloc = ... # type: List[str] @@ -46,23 +48,23 @@ class ParseResult( ): def geturl(self) -> str: ... -def urlparse(url: Union[str, unicode], scheme: str = ..., +def urlparse(url: _String, scheme: _String = ..., allow_fragments: bool = ...) -> ParseResult: ... -def urlsplit(url: Union[str, unicode], scheme: str = ..., +def urlsplit(url: _String, scheme: _String = ..., allow_fragments: bool = ...) -> SplitResult: ... @overload -def urlunparse(data: Tuple[str, str, str, str, str, str]) -> str: ... +def urlunparse(data: Tuple[_String, _String, _String, _String, _String, _String]) -> str: ... @overload -def urlunparse(data: Sequence[str]) -> str: ... +def urlunparse(data: Sequence[_String]) -> str: ... @overload -def urlunsplit(data: Tuple[str, str, str, str, str]) -> str: ... +def urlunsplit(data: Tuple[_String, _String, _String, _String, _String]) -> str: ... @overload -def urlunsplit(data: Sequence[str]) -> str: ... -def urljoin(base: Union[str, unicode], url: Union[str, unicode], +def urlunsplit(data: Sequence[_String]) -> str: ... +def urljoin(base: _String, url: _String, allow_fragments: bool = ...) -> str: ... -def urldefrag(url: Union[str, unicode]) -> str: ... -def unquote(s: str) -> str: ... -def parse_qs(qs: str, keep_blank_values: bool = ..., - strict_parsing: bool = ...) -> Dict[str, List[str]]: ... -def parse_qsl(qs: str, keep_blank_values: int = ..., - strict_parsing: bool = ...) -> List[Tuple[str, str]]: ... +def urldefrag(url: AnyStr) -> Tuple[AnyStr, str]: ... +def unquote(s: AnyStr) -> AnyStr: ... +def parse_qs(qs: AnyStr, keep_blank_values: bool = ..., + strict_parsing: bool = ...) -> Dict[AnyStr, List[AnyStr]]: ... +def parse_qsl(qs: AnyStr, keep_blank_values: int = ..., + strict_parsing: bool = ...) -> List[Tuple[AnyStr, AnyStr]]: ...