-
Notifications
You must be signed in to change notification settings - Fork 421
Bug: APIGatewayHttpResolver fails with ValueError: [TypeError('cannot convert dictionary update sequence element #0 to a sequence'), TypeError('vars() argument must have __dict__ attribute')] #4300
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
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Hi @toddcooke, thanks for opening this issue and reporting what might be a bug. I'm looking at the traceback and I can imagine some situations where this could happen, but I may not be able to reproduce the exact problem. Can you share some minimal reproducible code or stack, please? This would help a lot in identifying exactly the problem. |
@leandrodamascena I've added you as a collaborator to a private repo which reproduces the issue. The steps to reproduce are in the readme |
Hey @toddcooke! Thanks for sharing the repository with the minimal reproducible code. I enjoyed debugging the code using SQLAlchemy. This reminds me of the time I had to maintain a system that was a mix of Django, Django ORM, SQLAlchemy, and other technologies. It was an interesting experience! The issue you're facing is that you're trying to return a list of
class DataclassCustomEncoder(JSONEncoder):
def default(self, obj):
return asdict(obj) if is_dataclass(obj) else super().default(obj)
def custom_serializer(obj) -> str:
return json.dumps(obj, separators=(",", ":"), cls=DataclassCustomEncoder)
app = APIGatewayRestResolver(serializer=custom_serializer) This code might fail because you may have other classes and this serialize won't be able to serialize. You need to change this code to match your classes. Event Handler utility supports serializing objects of various types, including scalar types (e.g., strings, numbers), Pydantic models, dataclasses, Enum, PurePath objects, dictionaries, sequences (lists, tuples), and few others. However, it currently does not support serializing SQLAlchemy models out of the box. We currently have no plans to implement native SQLAlchemy model serialization. The reason for this is that SQLAlchemy models can have complex relationships and lazy-loading behavior, which can make serialization non-trivial and potentially lead to performance issues or unexpected behavior. Thanks and please let me know if you any other question or I can close this issue. |
Gotcha! If I may make a suggestion, it would be helpful to say explicitly on the powertools docs site that sqlalchemy is not supported. Thanks for figuring out this issue! |
|
Hi @toddcooke! I'm reopening this issue because it looks like we have room to improve the customer experience. If you and I have ever spent time trying to figure out behavior that isn't very clear on the Powertools side, it means something needs to be improved. I see two things we need to improve: 1 - We need to place this code in a try/except block and raise an error with a clear message saying that we do not support serialization of this type of object. Does this make sense to you? Would you like to add anything else? Thanks |
@leandrodamascena Those both sound like excellent additions, I think that would cover it. |
Coming out next Thursday (13th) ;) |
|
This is now released under 2.39.0 version! |
Expected Behaviour
I expect the APIGatewayHttpResolver to route my request to the proper annotated function, eg:
Current Behaviour
For some requests, I get the error
ValueError: [TypeError('cannot convert dictionary update sequence element #0 to a sequence'), TypeError('vars() argument must have __dict__ attribute')]
I'm using the serverless framework to package and deploy my app. I'm also using the serverless-python-requirements plugin to package my app.
See attached snippet for full traceback.
Code snippet
The text was updated successfully, but these errors were encountered: