]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Drop usage of `AnyStr` (#882)
authorFlorimond Manca <florimond.manca@gmail.com>
Sun, 29 Mar 2020 09:39:31 +0000 (11:39 +0200)
committerGitHub <noreply@github.com>
Sun, 29 Mar 2020 09:39:31 +0000 (11:39 +0200)
httpx/_content_streams.py
httpx/_models.py
httpx/_types.py [new file with mode: 0644]
httpx/_utils.py

index dd6c51f6af7d3fa172d0512268a4d95e6f4e1bc4..e6a3881f4ea74e4ff370f46546922cab0653af0f 100644 (file)
@@ -8,6 +8,7 @@ from pathlib import Path
 from urllib.parse import urlencode
 
 from ._exceptions import StreamConsumed
+from ._types import StrOrBytes
 from ._utils import format_form_param
 
 RequestData = typing.Union[
@@ -18,15 +19,16 @@ RequestFiles = typing.Dict[
     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],
         ],
     ],
@@ -222,7 +224,9 @@ class MultipartStream(ContentStream):
         """
 
         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):
index 2bfa367ab911d4120364a2b733bf5c4885710951..9465e454c4f5a863973d6351a2a9d61b7431e9cf 100644 (file)
@@ -39,6 +39,7 @@ from ._exceptions import (
     StreamConsumed,
 )
 from ._status_codes import StatusCode
+from ._types import StrOrBytes
 from ._utils import (
     ElapsedTimer,
     flatten_queryparams,
@@ -67,8 +68,8 @@ QueryParamTypes = typing.Union[
 
 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]]
diff --git a/httpx/_types.py b/httpx/_types.py
new file mode 100644 (file)
index 0000000..cb13a60
--- /dev/null
@@ -0,0 +1,7 @@
+"""
+Type definitions for type checking purposes.
+"""
+
+from typing import Union
+
+StrOrBytes = Union[str, bytes]
index b884f8193e1db6d868d36a0df273280e3b9cb51d..83312fd60b4e380a309333168063b1be5b2f8306 100644 (file)
@@ -14,6 +14,7 @@ from types import TracebackType
 from urllib.request import getproxies
 
 from ._exceptions import NetworkError
+from ._types import StrOrBytes
 
 if typing.TYPE_CHECKING:  # pragma: no cover
     from ._models import PrimitiveData
@@ -29,7 +30,7 @@ _HTML5_FORM_ENCODING_RE = re.compile(
 )
 
 
-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.
     """
@@ -38,7 +39,7 @@ def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:
     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.
     """
@@ -204,8 +205,8 @@ SENSITIVE_HEADERS = {"authorization", "proxy-authorization"}
 
 
 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)
@@ -301,7 +302,7 @@ def to_str(value: typing.Union[str, bytes], encoding: str = "utf-8") -> str:
     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()