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
{{ message }}
Repository navigation
Bug: data masking prepare_data leaves dataclasses and Pydantic models unconverted #8672
DataMasking.erase (and the other operations that go through prepare_data) should convert every dataclass and Pydantic model in the input to plain data, however many there are and whether or not the same instance appears twice.
Current Behaviour
prepare_data records the id() of every object it has visited in _visited to detect circular references, and never removes them. That includes the temporary dicts built by dataclasses.asdict() and model_dump(). Once one of those is freed, CPython reuses its id for a later object, which is then taken for a circular reference and returned unconverted. An instance referenced twice is skipped the same way. erase then fails with TypeError: Object of type X is not JSON serializable.
Code snippet
fromdataclassesimportdataclassfrompydanticimportBaseModelfromaws_lambda_powertools.utilities.data_maskingimportDataMaskingfromaws_lambda_powertools.utilities.data_masking.baseimportprepare_dataclassCard(BaseModel):
number: str@dataclassclassItem:
card: Cardprint(prepare_data({"items": [Item(Card(number=str(i))) foriinrange(5)]}))
# some items still contain Card(...) objects instead of dicts@dataclassclassPerson:
age: intp=Person(age=30)
DataMasking().erase({"items": [p, p]}, fields=["items[*].age"])
# TypeError: Object of type Person is not JSON serializable
Possible Solution
Keep in _visited only the objects on the current conversion path (add before recursing, remove in a finally). Circular references are still caught, and the existing circular-reference test still passes. I have this fix with regression tests in tests/functional/data_masking/_pydantic/test_data_masking_with_pydantic.py (fail on develop, pass with the change) and can open a PR once this is triaged.
Hi, thank you for the clear report and for maintaining Powertools.
I would like to work on a fix for this. Per CONTRIBUTING.md, could a maintainer triage the issue first? I will wait for that before opening a PR.
For transparency: I am using an AI assistant (Claude) to help me investigate and draft the change. I will run the tests myself, read the output, and say so in the PR, as the contribution guide asks.
My plan, if it matches what you would want: keep prepare_data behaviour the same, track only the objects that are currently being walked (so circular references are still detected) instead of every id ever seen, and add a regression test that fails on develop. Please tell me if you would prefer a different approach.
Activity
therealemmanueloppong commentedon Nov 1, 2026
Hi, thank you for the clear report and for maintaining Powertools.
I would like to work on a fix for this. Per CONTRIBUTING.md, could a maintainer triage the issue first? I will wait for that before opening a PR.
For transparency: I am using an AI assistant (Claude) to help me investigate and draft the change. I will run the tests myself, read the output, and say so in the PR, as the contribution guide asks.
My plan, if it matches what you would want: keep
prepare_databehaviour the same, track only the objects that are currently being walked (so circular references are still detected) instead of every id ever seen, and add a regression test that fails ondevelop. Please tell me if you would prefer a different approach.