Skip to content

Static typing: headers in Response should be Mapping instead of Dict #3743

Closed
@tonnico

Description

@tonnico

Static type checker used

mypy (project's standard)

AWS Lambda function runtime

3.12

Powertools for AWS Lambda (Python) version

latest

Static type checker info

error: Argument "headers" to "Response" has incompatible type "dict[str, str]"; expected
"dict[str, str | list[str]] | None"  [arg-type]
            headers=headers,
                    ^~~~~~~
note: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
note: Consider using "Mapping" instead, which is covariant in the value type

Code snippet

headers = {"str": "str"}
Response(200, headers=headers)

Possible Solution

Instead of Dict[str, Union[str, List[str]] use Mapping[str, Union[str, List[str]].
E.g. https://stackoverflow.com/a/73603324/6621062

Metadata

Metadata

Assignees

Labels

typingStatic typing definition related issues (mypy, pyright, etc.)

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions