Skip to content

Commit

Permalink
fix(typing): make Response headers covariant (#3745)
Browse files Browse the repository at this point in the history
Co-authored-by: Heitor Lessa <[email protected]>
  • Loading branch information
Wurstnase and heitorlessa authored Feb 9, 2024
1 parent 9d646c4 commit 5dc430c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Dict,
Generic,
List,
Mapping,
Match,
Optional,
Pattern,
Expand Down Expand Up @@ -200,7 +201,7 @@ def to_dict(self, origin: Optional[str]) -> Dict[str, str]:
return {}

# The origin matched an allowed origin, so return the CORS headers
headers: Dict[str, str] = {
headers = {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Headers": ",".join(sorted(self.allow_headers)),
}
Expand All @@ -222,7 +223,7 @@ def __init__(
status_code: int,
content_type: Optional[str] = None,
body: Optional[ResponseT] = None,
headers: Optional[Dict[str, Union[str, List[str]]]] = None,
headers: Optional[Mapping[str, Union[str, List[str]]]] = None,
cookies: Optional[List[Cookie]] = None,
compress: Optional[bool] = None,
):
Expand All @@ -237,15 +238,15 @@ def __init__(
provided http headers
body: Union[str, bytes, None]
Optionally set the response body. Note: bytes body will be automatically base64 encoded
headers: dict[str, Union[str, List[str]]]
headers: Mapping[str, Union[str, List[str]]]
Optionally set specific http headers. Setting "Content-Type" here would override the `content_type` value.
cookies: list[Cookie]
Optionally set cookies.
"""
self.status_code = status_code
self.body = body
self.base64_encoded = False
self.headers: Dict[str, Union[str, List[str]]] = headers if headers else {}
self.headers: Dict[str, Union[str, List[str]]] = dict(headers) if headers else {}
self.cookies = cookies or []
self.compress = compress
self.content_type = content_type
Expand Down Expand Up @@ -1940,7 +1941,7 @@ def _path_starts_with(path: str, prefix: str):

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

0 comments on commit 5dc430c

Please sign in to comment.