1- from typing import Any , Dict , Optional , Protocol , runtime_checkable
1+ from typing import Any , Iterator , Optional , Protocol , Tuple , runtime_checkable
2+
3+
4+ class Headers (Protocol ):
5+ """
6+ A case-insensitive mapping of HTTP response headers.
7+
8+ Header name lookups are case-insensitive per RFC 7230, so
9+ ``headers.get('content-type')`` and ``headers.get('Content-Type')``
10+ return the same value. The concrete type returned depends on the HTTP
11+ backend in use and should not be relied upon directly.
12+ """
13+
14+ def get (self , key : str , default : Any = None ) -> Any :
15+ """Return the value for *key* (case-insensitive), or *default*."""
16+ ...
17+
18+ def __getitem__ (self , key : str ) -> Any : ...
19+
20+ def __contains__ (self , key : object ) -> bool : ...
21+
22+ def __iter__ (self ) -> Iterator [str ]: ...
23+
24+ def items (self ) -> Any : ...
225
326
427@runtime_checkable
@@ -11,7 +34,7 @@ class ExceptionWithHeaders(Protocol):
1134 """
1235
1336 @property
14- def headers (self ) -> Optional [Dict [ str , Any ] ]:
37+ def headers (self ) -> Optional [Headers ]:
1538 """The HTTP response headers associated with this exception."""
1639 raise NotImplementedError
1740
@@ -24,7 +47,7 @@ class HTTPStatusError(Exception):
2447 When available, the response headers are accessible via the :attr:`headers` property.
2548 """
2649
27- def __init__ (self , status : int , headers : Optional [Dict [ str , Any ] ] = None ):
50+ def __init__ (self , status : int , headers : Optional [Headers ] = None ):
2851 super ().__init__ ("HTTP error %d" % status )
2952 self ._status = status
3053 self ._headers = headers
@@ -34,8 +57,8 @@ def status(self) -> int:
3457 return self ._status
3558
3659 @property
37- def headers (self ) -> Optional [Dict [ str , Any ] ]:
38- """The HTTP response headers, if available."""
60+ def headers (self ) -> Optional [Headers ]:
61+ """The HTTP response headers, if available. Header names are case-insensitive. """
3962 return self ._headers
4063
4164
@@ -47,7 +70,7 @@ class HTTPContentTypeError(Exception):
4770 When available, the response headers are accessible via the :attr:`headers` property.
4871 """
4972
50- def __init__ (self , content_type : str , headers : Optional [Dict [ str , Any ] ] = None ):
73+ def __init__ (self , content_type : str , headers : Optional [Headers ] = None ):
5174 super ().__init__ ("invalid content type \" %s\" " % content_type )
5275 self ._content_type = content_type
5376 self ._headers = headers
@@ -57,6 +80,6 @@ def content_type(self) -> str:
5780 return self ._content_type
5881
5982 @property
60- def headers (self ) -> Optional [Dict [ str , Any ] ]:
61- """The HTTP response headers, if available."""
83+ def headers (self ) -> Optional [Headers ]:
84+ """The HTTP response headers, if available. Header names are case-insensitive. """
6285 return self ._headers
0 commit comments