from urllib.parse import urlencode
from ._exceptions import StreamConsumed
+from ._types import StrOrBytes
from ._utils import format_form_param
RequestData = typing.Union[
str,
typing.Union[
# file (or str)
- typing.Union[typing.IO[typing.AnyStr], typing.AnyStr],
+ typing.Union[typing.IO[str], typing.IO[bytes], StrOrBytes],
# (filename, file (or str))
typing.Tuple[
- typing.Optional[str], typing.Union[typing.IO[typing.AnyStr], typing.AnyStr],
+ typing.Optional[str],
+ typing.Union[typing.IO[str], typing.IO[bytes], StrOrBytes],
],
# (filename, file (or str), content_type)
typing.Tuple[
typing.Optional[str],
- typing.Union[typing.IO[typing.AnyStr], typing.AnyStr],
+ typing.Union[typing.IO[str], typing.IO[bytes], StrOrBytes],
typing.Optional[str],
],
],
"""
def __init__(
- self, name: str, value: typing.Union[typing.IO[typing.AnyStr], tuple]
+ self,
+ name: str,
+ value: typing.Union[typing.IO[str], typing.IO[bytes], tuple],
) -> None:
self.name = name
if not isinstance(value, tuple):
StreamConsumed,
)
from ._status_codes import StatusCode
+from ._types import StrOrBytes
from ._utils import (
ElapsedTimer,
flatten_queryparams,
HeaderTypes = typing.Union[
"Headers",
- typing.Dict[typing.AnyStr, typing.AnyStr],
- typing.List[typing.Tuple[typing.AnyStr, typing.AnyStr]],
+ typing.Dict[StrOrBytes, StrOrBytes],
+ typing.Sequence[typing.Tuple[StrOrBytes, StrOrBytes]],
]
CookieTypes = typing.Union["Cookies", CookieJar, typing.Dict[str, str]]
--- /dev/null
+"""
+Type definitions for type checking purposes.
+"""
+
+from typing import Union
+
+StrOrBytes = Union[str, bytes]
from urllib.request import getproxies
from ._exceptions import NetworkError
+from ._types import StrOrBytes
if typing.TYPE_CHECKING: # pragma: no cover
from ._models import PrimitiveData
)
-def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:
+def normalize_header_key(value: StrOrBytes, encoding: str = None) -> bytes:
"""
Coerce str/bytes into a strictly byte-wise HTTP header key.
"""
return value.encode(encoding or "ascii").lower()
-def normalize_header_value(value: typing.AnyStr, encoding: str = None) -> bytes:
+def normalize_header_value(value: StrOrBytes, encoding: str = None) -> bytes:
"""
Coerce str/bytes into a strictly byte-wise HTTP header value.
"""
def obfuscate_sensitive_headers(
- items: typing.Iterable[typing.Tuple[typing.AnyStr, typing.AnyStr]]
-) -> typing.Iterator[typing.Tuple[typing.AnyStr, typing.AnyStr]]:
+ items: typing.Iterable[typing.Tuple[StrOrBytes, StrOrBytes]]
+) -> typing.Iterator[typing.Tuple[StrOrBytes, StrOrBytes]]:
for k, v in items:
if to_str(k.lower()) in SENSITIVE_HEADERS:
v = to_bytes_or_str("[secure]", match_type_of=v)
return value if isinstance(value, str) else value.decode(encoding)
-def to_bytes_or_str(value: str, match_type_of: typing.AnyStr) -> typing.AnyStr:
+def to_bytes_or_str(value: str, match_type_of: StrOrBytes) -> StrOrBytes:
return value if isinstance(match_type_of, str) else value.encode()