You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DictWrapper and all of its subclasses should fully implement the Mapping interface so that it can be used interchangeably with code that expects the event to be a dictionary. This helps Powertools Middleware play nicely with existing middleware.
Current Behaviour
DictWrapper does not implement the abstract __iter__ and __len__ methods from the Mapping interface. Currently, client code cannot iterate over any subclass of DictWrapper, nor can it use the dictionary built-ins keys, values, or items.
Code snippet
fromaws_lambda_powertools.utilities.data_classes.commonimportDictWrapperevent=DictWrapper({'foo': 'bar'})
[kforkinevent] # raises KeyError: 0# all of the following raise AttributeError: 'DictWrapper' object has no attribute '...'event.keys()
event.items()
event.values()
Possible Solution
DictWrapper should subclass either Mapping or Dict, for example:
classDictWrapper(Mapping):
"""Provides a single read only access to a wrapper dict"""def__init__(self, data: Dict[str, Any]):
self._data=datadef__getitem__(self, key: str) ->Any:
returnself._data[key]
def__eq__(self, other: Any) ->bool:
ifnotisinstance(other, DictWrapper):
returnFalsereturnself._data==other._datadefget(self, key: str) ->Optional[Any]:
returnself._data.get(key)
@propertydefraw_event(self) ->Dict[str, Any]:
"""The original raw event dict"""returnself._data# missing methodsdef__iter__(self) ->Iterator:
returniter(self._data)
def__len__(self) ->int:
returnlen(self._data)
Hi @Tankanow thank you for the very clear explanation! Would you mind opening a PR with the changes, together with a couple of tests? We will be thrilled to review it and include it in our next release.
Expected Behaviour
DictWrapper
and all of its subclasses should fully implement the Mapping interface so that it can be used interchangeably with code that expects theevent
to be a dictionary. This helps Powertools Middleware play nicely with existing middleware.Current Behaviour
DictWrapper
does not implement the abstract__iter__
and__len__
methods from theMapping
interface. Currently, client code cannot iterate over any subclass ofDictWrapper
, nor can it use the dictionary built-inskeys
,values
, oritems
.Code snippet
Possible Solution
DictWrapper
should subclass eitherMapping
orDict
, for example:Steps to Reproduce
see comment above
AWS Lambda Powertools for Python version
latest
AWS Lambda function runtime
3.9
Packaging format used
PyPi
Debugging logs
The text was updated successfully, but these errors were encountered: