Skip to content

Commit 5dc430c

Browse files
tonnicoheitorlessa
andauthored
fix(typing): make Response headers covariant (#3745)
Co-authored-by: Heitor Lessa <[email protected]>
1 parent 9d646c4 commit 5dc430c

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Dict,
1818
Generic,
1919
List,
20+
Mapping,
2021
Match,
2122
Optional,
2223
Pattern,
@@ -200,7 +201,7 @@ def to_dict(self, origin: Optional[str]) -> Dict[str, str]:
200201
return {}
201202

202203
# The origin matched an allowed origin, so return the CORS headers
203-
headers: Dict[str, str] = {
204+
headers = {
204205
"Access-Control-Allow-Origin": origin,
205206
"Access-Control-Allow-Headers": ",".join(sorted(self.allow_headers)),
206207
}
@@ -222,7 +223,7 @@ def __init__(
222223
status_code: int,
223224
content_type: Optional[str] = None,
224225
body: Optional[ResponseT] = None,
225-
headers: Optional[Dict[str, Union[str, List[str]]]] = None,
226+
headers: Optional[Mapping[str, Union[str, List[str]]]] = None,
226227
cookies: Optional[List[Cookie]] = None,
227228
compress: Optional[bool] = None,
228229
):
@@ -237,15 +238,15 @@ def __init__(
237238
provided http headers
238239
body: Union[str, bytes, None]
239240
Optionally set the response body. Note: bytes body will be automatically base64 encoded
240-
headers: dict[str, Union[str, List[str]]]
241+
headers: Mapping[str, Union[str, List[str]]]
241242
Optionally set specific http headers. Setting "Content-Type" here would override the `content_type` value.
242243
cookies: list[Cookie]
243244
Optionally set cookies.
244245
"""
245246
self.status_code = status_code
246247
self.body = body
247248
self.base64_encoded = False
248-
self.headers: Dict[str, Union[str, List[str]]] = headers if headers else {}
249+
self.headers: Dict[str, Union[str, List[str]]] = dict(headers) if headers else {}
249250
self.cookies = cookies or []
250251
self.compress = compress
251252
self.content_type = content_type
@@ -1940,7 +1941,7 @@ def _path_starts_with(path: str, prefix: str):
19401941

19411942
def _not_found(self, method: str) -> ResponseBuilder:
19421943
"""Called when no matching route was found and includes support for the cors preflight response"""
1943-
headers: Dict[str, Union[str, List[str]]] = {}
1944+
headers = {}
19441945
if self._cors:
19451946
logger.debug("CORS is enabled, updating headers.")
19461947
headers.update(self._cors.to_dict(self.current_event.get_header_value("Origin")))

0 commit comments

Comments
 (0)