Skip to content

Static typing: resolved_headers_field is not Optional #4147

Closed
@tonnico

Description

@tonnico
Contributor

Static type checker used

pyright/pylance

AWS Lambda function runtime

3.12

Powertools for AWS Lambda (Python) version

latest

Static type checker info

Expression of type "Dict[str, Any] | None" cannot be assigned to declared type "dict[str, list[str]]"
  Type "Dict[str, Any] | None" cannot be assigned to type "dict[str, list[str]]"
    "None" is incompatible with "dict[str, list[str]]"

Code snippet

import json
from aws_lambda_powertools.event_handler.router import ALBRouter
router = ALBRouter()
@router.get("/")
def get_headers() -> str:
    headers: dict[str, list[str]] = router.current_event.resolved_headers_field
    return json.dumps(headers)


### Possible Solution

Remove `Optional` 

```diff
- def resolved_headers_field(self) -> Optional[Dict[str, Any]]:
+ def resolved_headers_field(self) -> Dict[str, Any]:

Activity

added
triagePending triage from maintainers
typingStatic typing definition related issues (mypy, pyright, etc.)
on Apr 18, 2024
rubenfonseca

rubenfonseca commented on Apr 18, 2024

@rubenfonseca
Contributor

Hi @wurstnase I'm not sure I follow this one. resolved_headers_field is currently marked as returning an Optional, meaning that in some cases it can return None. Shouldn't you handle that in your code instead of changing the API signature?

One could argue that checking the implementation, the field is always present. But due to Hyrum's Law I would prefer to add this to our v3 branch (more on that earlier next week).

@leandrodamascena I would appreciate your thoughts here too

added
triagePending triage from maintainers
and removed
triagePending triage from maintainers
on Apr 18, 2024
leandrodamascena

leandrodamascena commented on Apr 18, 2024

@leandrodamascena
Contributor

Yes! I agree with you @rubenfonseca that we should avoid doing this now and tag this issue/pr as eligible for Powertools V3.

rubenfonseca

rubenfonseca commented on Apr 18, 2024

@rubenfonseca
Contributor

Hope that's ok with you @wurstnase. I'll add this to our v3 queue. I promise v3 is not far ahead ;)

added
v3Features that will be included in Powertools v3.
and removed
triagePending triage from maintainers
on Apr 18, 2024

12 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    typingStatic typing definition related issues (mypy, pyright, etc.)v3Features that will be included in Powertools v3.

    Type

    No type

    Projects

    Status

    Shipped

    Relationships

    None yet

      Participants

      @rubenfonseca@heitorlessa@leandrodamascena@tonnico

      Issue actions

        Static typing: resolved_headers_field is not Optional · Issue #4147 · aws-powertools/powertools-lambda-python