Allow Union[unicode, str] rather than just str in several places (#3887)

* Allow unicode objects throughout urlparse.py

Functions such as `urlparse()`, if given a unicode object,
will happily return a ParseResult of unicode components.

Relatedly, functions like `unquote()` will accept any of
bytes/str/unicode and return an object of the same type
or a composite containing that type.

* Allow unicode in several places in Thread

`name` and `kwargs` to Thread.__init__ function perfectly
well with unicode, not just str.

Note: the .name attribute will always be str even
if the constructor is passed something else, since
__init__ calls:

    self.__name = str(name or _newname())

* Use typing.AnyStr properly

...rather than defining a new TypeVar unncessarily.

* Use typing.Text properly

Text is behaviorally equivalent to Union[str, unicode]
for Python 2 argument types.

* Remove outdated import & definition

* [check file consistent] copy changes to _dummy_threading.pyi
This commit is contained in:
Brad Solomon
2020-05-27 22:52:14 -04:00
committed by GitHub
parent 851efa550e
commit ddb47deb27
3 changed files with 30 additions and 32 deletions

View File

@@ -17,48 +17,48 @@ def clear_cache() -> None: ...
class ResultMixin(object):
@property
def username(self) -> Optional[str]: ...
def username(self) -> Optional[_String]: ...
@property
def password(self) -> Optional[str]: ...
def password(self) -> Optional[_String]: ...
@property
def hostname(self) -> Optional[str]: ...
def hostname(self) -> Optional[_String]: ...
@property
def port(self) -> Optional[int]: ...
class _SplitResult(NamedTuple):
scheme: str
netloc: str
path: str
query: str
fragment: str
scheme: _String
netloc: _String
path: _String
query: _String
fragment: _String
class SplitResult(_SplitResult, ResultMixin):
def geturl(self) -> str: ...
def geturl(self) -> _String: ...
class _ParseResult(NamedTuple):
scheme: str
netloc: str
path: str
params: str
query: str
fragment: str
scheme: _String
netloc: _String
path: _String
params: _String
query: _String
fragment: _String
class ParseResult(_ParseResult, ResultMixin):
def geturl(self) -> str: ...
def geturl(self) -> _String: ...
def urlparse(url: _String, scheme: _String = ...,
allow_fragments: bool = ...) -> ParseResult: ...
def urlsplit(url: _String, scheme: _String = ...,
allow_fragments: bool = ...) -> SplitResult: ...
@overload
def urlunparse(data: Tuple[_String, _String, _String, _String, _String, _String]) -> str: ...
def urlunparse(data: Tuple[_String, _String, _String, _String, _String, _String]) -> _String: ...
@overload
def urlunparse(data: Sequence[_String]) -> str: ...
def urlunparse(data: Sequence[_String]) -> _String: ...
@overload
def urlunsplit(data: Tuple[_String, _String, _String, _String, _String]) -> str: ...
def urlunsplit(data: Tuple[_String, _String, _String, _String, _String]) -> _String: ...
@overload
def urlunsplit(data: Sequence[_String]) -> str: ...
def urlunsplit(data: Sequence[_String]) -> _String: ...
def urljoin(base: _String, url: _String,
allow_fragments: bool = ...) -> str: ...
def urldefrag(url: AnyStr) -> Tuple[AnyStr, str]: ...
allow_fragments: bool = ...) -> _String: ...
def urldefrag(url: AnyStr) -> Tuple[AnyStr, AnyStr]: ...
def unquote(s: AnyStr) -> AnyStr: ...
def parse_qs(qs: AnyStr, keep_blank_values: bool = ...,
strict_parsing: bool = ...) -> Dict[AnyStr, List[AnyStr]]: ...

View File

@@ -1,6 +1,6 @@
from typing import (
Any, Callable, Iterable, List, Mapping, Optional, Tuple, Type, Union,
Any, Callable, Iterable, List, Mapping, Optional, Text, Tuple, Type, Union,
TypeVar,
)
from types import FrameType, TracebackType
@@ -12,7 +12,6 @@ _TF = Callable[[FrameType, str, Any], Optional[Callable[..., Any]]]
_PF = Callable[[FrameType, str, Any], None]
_T = TypeVar('_T')
__all__: List[str]
def active_count() -> int: ...
@@ -63,14 +62,14 @@ class Thread:
else:
def __init__(self, group: None = ...,
target: Optional[Callable[..., Any]] = ...,
name: Optional[str] = ...,
name: Optional[Text] = ...,
args: Iterable[Any] = ...,
kwargs: Mapping[str, Any] = ...) -> None: ...
kwargs: Mapping[Text, Any] = ...) -> None: ...
def start(self) -> None: ...
def run(self) -> None: ...
def join(self, timeout: Optional[float] = ...) -> None: ...
def getName(self) -> str: ...
def setName(self, name: str) -> None: ...
def setName(self, name: Text) -> None: ...
if sys.version_info >= (3, 8):
@property
def native_id(self) -> Optional[int]: ... # only available on some platforms

View File

@@ -1,6 +1,6 @@
from typing import (
Any, Callable, Iterable, List, Mapping, Optional, Tuple, Type, Union,
Any, Callable, Iterable, List, Mapping, Optional, Text, Tuple, Type, Union,
TypeVar,
)
from types import FrameType, TracebackType
@@ -12,7 +12,6 @@ _TF = Callable[[FrameType, str, Any], Optional[Callable[..., Any]]]
_PF = Callable[[FrameType, str, Any], None]
_T = TypeVar('_T')
__all__: List[str]
def active_count() -> int: ...
@@ -63,14 +62,14 @@ class Thread:
else:
def __init__(self, group: None = ...,
target: Optional[Callable[..., Any]] = ...,
name: Optional[str] = ...,
name: Optional[Text] = ...,
args: Iterable[Any] = ...,
kwargs: Mapping[str, Any] = ...) -> None: ...
kwargs: Mapping[Text, Any] = ...) -> None: ...
def start(self) -> None: ...
def run(self) -> None: ...
def join(self, timeout: Optional[float] = ...) -> None: ...
def getName(self) -> str: ...
def setName(self, name: str) -> None: ...
def setName(self, name: Text) -> None: ...
if sys.version_info >= (3, 8):
@property
def native_id(self) -> Optional[int]: ... # only available on some platforms