Skip to content

Static typing: resolve() typing should accept Mapping, not just dict #7864

@Iamrodos

Description

@Iamrodos

Static type checker used

pyright/pylance

AWS Lambda function runtime

3.14

Powertools for AWS Lambda (Python) version

latest

Static type checker info

uv run pyright example.py

Argument of type "LambdaFunctionUrlEvent" cannot be assigned to parameter "event" of type "dict[str, Any]"

Code snippet

from aws_lambda_powertools.event_handler import LambdaFunctionUrlResolver
from aws_lambda_powertools.utilities.data_classes import LambdaFunctionUrlEvent
from aws_lambda_powertools.utilities.typing import LambdaContext

app = LambdaFunctionUrlResolver()

@app.get("/")
def home():
    return {"message": "Hello"}

# This is the recommended typed handler signature from Powertools docs
def handler(event: LambdaFunctionUrlEvent, context: LambdaContext) -> dict:
    # ERROR: Argument of type "LambdaFunctionUrlEvent" cannot be assigned
    #        to parameter "event" of type "dict[str, Any]"
    return app.resolve(event, context)

Possible Solution

straightforward and backwards-compatible:

in aws_lambda_powertools/event_handler/api_gateway.py

From:
def resolve(self, event: dict[str, Any], context: LambdaContext) -> dict[str, Any]:

To:

from collections.abc import Mapping
def resolve(self, event: Mapping[str, Any], context: LambdaContext) -> dict[str, Any]:

This works because dict is a subtype of Mapping, and resolve() only reads from the event, never mutates it

Happy to do a PR for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    triagePending triage from maintainerstypingStatic typing definition related issues (mypy, pyright, etc.)

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions