Skip to content
Myr

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

4 files+24 added−11 removed

modified:httpx/_content_streams.py+8 added-4 removed
6 unmodified lines
7from pathlib import Path8from urllib.parse import urlencode910from ._exceptions import StreamConsumed11from ._types import StrOrBytes12from ._utils import format_form_param1314RequestData = typing.Union[15 dict, str, bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]
2 unmodified lines
18RequestFiles = 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 lines
223 A single file field item, within a multipart form field.224 """225226 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"))).name
116 unmodified lines
modified:httpx/_models.py+3 added-2 removed
37 unmodified lines
38 ResponseNotRead,39 StreamConsumed,40)41from ._status_codes import StatusCode42from ._types import StrOrBytes43from ._utils import (44 ElapsedTimer,45 flatten_queryparams,46 guess_json_utf,
20 unmodified lines
67]6869HeaderTypes = 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]7475CookieTypes = typing.Union["Cookies", CookieJar, typing.Dict[str, str]]76
1132 unmodified lines
added:httpx/_types.py+7 added
1"""2Type definitions for type checking purposes.3"""45from typing import Union67StrOrBytes = Union[str, bytes]
modified:httpx/_utils.py+6 added-5 removed
12 unmodified lines
13from types import TracebackType14from urllib.request import getproxies1516from ._exceptions import NetworkError17from ._types import StrOrBytes1819if typing.TYPE_CHECKING: # pragma: no cover20 from ._models import PrimitiveData21 from ._models import URL
7 unmodified lines
29 r"|".join([re.escape(c) for c in _HTML5_FORM_ENCODING_REPLACEMENTS.keys()])30)313232def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:33def 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()404141def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:42def 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 lines
204SENSITIVE_HEADERS = {"authorization", "proxy-authorization"}205206207def 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, v
87 unmodified lines
301def to_str(value: typing.Union[str, bytes], encoding: str = "utf-8") -> str:302 return value if isinstance(value, str) else value.decode(encoding)303304304def to_bytes_or_str(value: str, match_type_of: typing.AnyStr) -> typing.AnyStr:305def to_bytes_or_str(value: str, match_type_of: StrOrBytes) -> StrOrBytes:306 return value if isinstance(match_type_of, str) else value.encode()307308309def unquote(value: str) -> str:
61 unmodified lines