Drop usage of `AnyStr` (#882)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Florimond Manca · committed by GitHub · parent fc980e7 · Browse files · 4 files·+24 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+24 added−11 removed
- other:+24 added−11 removed
Provenance
- Commit
8f400c2- Tree
a9107a9- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
fc980e7
4 files changed+24 added−11 removed
modified:httpx/_content_streams.py+8 added-4 removed
6 unmodified lines7 from pathlib import Path8 from urllib.parse import urlencode9 10 from ._exceptions import StreamConsumed11+from ._types import StrOrBytes12 from ._utils import format_form_param13 14 RequestData = typing.Union[15 dict, str, bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]2 unmodified lines18 RequestFiles = typing.Dict[19 str,20 typing.Union[21 # file (or str)21- typing.Union[typing.IO[typing.AnyStr], typing.AnyStr],22+ typing.Union[typing.IO[str], typing.IO[bytes], StrOrBytes],23 # (filename, file (or str))24 typing.Tuple[24- typing.Optional[str], typing.Union[typing.IO[typing.AnyStr], typing.AnyStr],25+ typing.Optional[str],26+ typing.Union[typing.IO[str], typing.IO[bytes], StrOrBytes],27 ],28 # (filename, file (or str), content_type)29 typing.Tuple[30 typing.Optional[str],29- typing.Union[typing.IO[typing.AnyStr], typing.AnyStr],31+ typing.Union[typing.IO[str], typing.IO[bytes], StrOrBytes],32 typing.Optional[str],33 ],34 ],35 ]187 unmodified lines223 A single file field item, within a multipart form field.224 """225 226 def __init__(225- self, name: str, value: typing.Union[typing.IO[typing.AnyStr], tuple]227+ self,228+ name: str,229+ value: typing.Union[typing.IO[str], typing.IO[bytes], tuple],230 ) -> None:231 self.name = name232 if not isinstance(value, tuple):233 self.filename = Path(str(getattr(value, "name", "upload"))).name116 unmodified linesmodified:httpx/_models.py+3 added-2 removed
37 unmodified lines38 ResponseNotRead,39 StreamConsumed,40 )41 from ._status_codes import StatusCode42+from ._types import StrOrBytes43 from ._utils import (44 ElapsedTimer,45 flatten_queryparams,46 guess_json_utf,20 unmodified lines67 ]68 69 HeaderTypes = typing.Union[70 "Headers",70- typing.Dict[typing.AnyStr, typing.AnyStr],71- typing.List[typing.Tuple[typing.AnyStr, typing.AnyStr]],71+ typing.Dict[StrOrBytes, StrOrBytes],72+ typing.Sequence[typing.Tuple[StrOrBytes, StrOrBytes]],73 ]74 75 CookieTypes = typing.Union["Cookies", CookieJar, typing.Dict[str, str]]76 1132 unmodified linesadded:httpx/_types.py+7 added
1+"""2+Type definitions for type checking purposes.3+"""4+5+from typing import Union6+7+StrOrBytes = Union[str, bytes]modified:httpx/_utils.py+6 added-5 removed
12 unmodified lines13 from types import TracebackType14 from urllib.request import getproxies15 16 from ._exceptions import NetworkError17+from ._types import StrOrBytes18 19 if typing.TYPE_CHECKING: # pragma: no cover20 from ._models import PrimitiveData21 from ._models import URL7 unmodified lines29 r"|".join([re.escape(c) for c in _HTML5_FORM_ENCODING_REPLACEMENTS.keys()])30 )31 32 32-def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:33+def normalize_header_key(value: StrOrBytes, encoding: str = None) -> bytes:34 """35 Coerce str/bytes into a strictly byte-wise HTTP header key.36 """37 if isinstance(value, bytes):38 return value.lower()39 return value.encode(encoding or "ascii").lower()40 41 41-def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:42+def normalize_header_value(value: StrOrBytes, encoding: str = None) -> bytes:43 """44 Coerce str/bytes into a strictly byte-wise HTTP header value.45 """46 if isinstance(value, bytes):157 unmodified lines204 SENSITIVE_HEADERS = {"authorization", "proxy-authorization"}205 206 207 def obfuscate_sensitive_headers(207- items: typing.Iterable[typing.Tuple[typing.AnyStr, typing.AnyStr]]208-) -> typing.Iterator[typing.Tuple[typing.AnyStr, typing.AnyStr]]:208+ items: typing.Iterable[typing.Tuple[StrOrBytes, StrOrBytes]]209+) -> typing.Iterator[typing.Tuple[StrOrBytes, StrOrBytes]]:210 for k, v in items:211 if to_str(k.lower()) in SENSITIVE_HEADERS:212 v = to_bytes_or_str("[secure]", match_type_of=v)213 yield k, v87 unmodified lines301 def to_str(value: typing.Union[str, bytes], encoding: str = "utf-8") -> str:302 return value if isinstance(value, str) else value.decode(encoding)303 304 304-def to_bytes_or_str(value: str, match_type_of: typing.AnyStr) -> typing.AnyStr:305+def to_bytes_or_str(value: str, match_type_of: StrOrBytes) -> StrOrBytes:306 return value if isinstance(match_type_of, str) else value.encode()307 308 309 def unquote(value: str) -> str:61 unmodified lines