Skip to content
Myr

Use AnyStr where appropriate (#999)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Use AnyStr where appropriate * Update httpx/_types.py Co-authored-by: Florimond Manca <florimond.manca@gmail.com> * Update _types.py

authored by Tom Christie · committed by GitHub · parent 7c8158a · Browse files · 2 files+15 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

Diff stats

2 files+15 added−8 removed

  • other:+15 added−8 removed

2 files+15 added−8 removed

modified:httpx/_types.py+5 added-2 removed
22 unmodified lines
23 from ._auth import Auth # noqa: F40124 from ._config import Proxy, Timeout # noqa: F40125 from ._models import URL, Cookies, Headers, QueryParams, Request # noqa: F4012627StrOrBytes = Union[str, bytes]2728PrimitiveData = Optional[Union[str, int, float, bool]]2930URLTypes = Union["URL", str]
5 unmodified lines
36 str,37]3839HeaderTypes = Union[41 "Headers", Dict[StrOrBytes, StrOrBytes], Sequence[Tuple[StrOrBytes, StrOrBytes]],40 "Headers",41 Dict[str, str],42 Dict[bytes, bytes],43 Sequence[Tuple[str, str]],44 Sequence[Tuple[bytes, bytes]],45]4647CookieTypes = Union["Cookies", CookieJar, Dict[str, str]]48
27 unmodified lines
modified:httpx/_utils.py+10 added-6 removed
14 unmodified lines
15from types import TracebackType16from urllib.request import getproxies1718from ._exceptions import NetworkError19from ._types import PrimitiveData, StrOrBytes19from ._types import PrimitiveData2021if typing.TYPE_CHECKING: # pragma: no cover22 from ._models import URL23
6 unmodified lines
30 r"|".join([re.escape(c) for c in _HTML5_FORM_ENCODING_REPLACEMENTS.keys()])31)323334def normalize_header_key(value: StrOrBytes, encoding: str = None) -> bytes:34def normalize_header_key(35 value: typing.Union[str, bytes], encoding: str = None36) -> bytes:37 """38 Coerce str/bytes into a strictly byte-wise HTTP header key.39 """40 if isinstance(value, bytes):41 return value.lower()42 return value.encode(encoding or "ascii").lower()434443def normalize_header_value(value: StrOrBytes, encoding: str = None) -> bytes:45def normalize_header_value(46 value: typing.Union[str, bytes], encoding: str = None47) -> bytes:48 """49 Coerce str/bytes into a strictly byte-wise HTTP header value.50 """51 if isinstance(value, bytes):
157 unmodified lines
209SENSITIVE_HEADERS = {"authorization", "proxy-authorization"}210211212def obfuscate_sensitive_headers(209 items: typing.Iterable[typing.Tuple[StrOrBytes, StrOrBytes]]210) -> typing.Iterator[typing.Tuple[StrOrBytes, StrOrBytes]]:213 items: typing.Iterable[typing.Tuple[typing.AnyStr, typing.AnyStr]]214) -> typing.Iterator[typing.Tuple[typing.AnyStr, typing.AnyStr]]:215 for k, v in items:216 if to_str(k.lower()) in SENSITIVE_HEADERS:217 v = to_bytes_or_str("[secure]", match_type_of=v)218 yield k, v
87 unmodified lines
306def to_str(value: typing.Union[str, bytes], encoding: str = "utf-8") -> str:307 return value if isinstance(value, str) else value.decode(encoding)308309306def to_bytes_or_str(value: str, match_type_of: StrOrBytes) -> StrOrBytes:310def to_bytes_or_str(value: str, match_type_of: typing.AnyStr) -> typing.AnyStr:311 return value if isinstance(match_type_of, str) else value.encode()312313314def unquote(value: str) -> str:
97 unmodified lines