Skip to content

fix(typing): make Response headers covariant #3745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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