Skip to content

Wrong type signature on parser envelopes #285

Closed

Description

@zroger

The model argument of BaseEnvelope and all its subclasses should be of type Type[Model], not just Model.
The same issue affects parse.

Expected Behavior

Parsing an event dict using one of the envelope classes expects a BaseModel class as a parameter and returns an instance of that class. The type signature should match.

Current Behavior

mypy complains with the error

Argument 2 to "parse" of "SqsEnvelope" has incompatible type "Type[Record]"; expected "Record"

Possible Solution

The type signature of the model argument should be changed to Type[Model].

Steps to Reproduce (for bugs)

# main.py
from typing import Dict, List, Optional
from aws_lambda_powertools.utilities.parser import envelopes, parse
from pydantic import BaseModel


class Record(BaseModel):
    id: str


envelope = envelopes.SqsEnvelope()
records: List[Optional[Record]] = envelope.parse({}, Record)

records2: List[Optional[Record]] = parse({}, model=Record, envelope=envelope)
mypy main.py
main.py:11: error: Argument 2 to "parse" of "SqsEnvelope" has incompatible type "Type[Record]"; expected "Record"
main.py:13: error: Value of type variable "Model" of "parse" cannot be "Type[Record]"
main.py:13: error: Incompatible types in assignment (expression has type "Type[Record]", variable has type "List[Optional[Record]]")
Found 3 errors in 1 file (checked 1 source file)

Environment

  • Python 3.8.5
  • aws-lambda-powertools==1.10.2
  • mypy==0.800
  • pydantic==1.7.3

Activity

  1. added
    bugSomething isn't working
    triagePending triage from maintainers
    on Feb 6, 2021
  2. heitorlessa commented on Feb 9, 2021

    @heitorlessa
    Contributor

    @nmoutschen Could you please check this before you release a patch version for Tracer? This seems like a super quick fix to help MyPy customers.

    Related issue with much bigger scope to support MyPy: #221

  3. heitorlessa commented on Feb 12, 2021

    @heitorlessa
    Contributor

    This is now available in 1.10.3 - Closing it. Thank you for reporting another one @zroger, and for the fix @nmoutschen

  4. added a commit that references this issue on Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions