fix(add_book): use _data.pop to strip infobase metadata from matched author
Closes #13016
**Fix**
The metadata cleanup loop in author_import_record_to_author() was broken in two ways:
existing.kaccessed a literal attribute named"k"(alwaysNothing/falsy) instead of the loop variablek, so the condition was neverTrue.- Even with the condition corrected,
del existing[k]raisesAttributeErrorbecause InfogamiThingobjects have no__delitem__.
Fix: use existing._data.pop(k, None) to remove the metadata keys (last_modified, id, revision, created)
directly from the underlying data dict, which is the only supported mutation path on a Thing.
Technical
Thing.__setitem__delegates toself._getdata()[key], but there is no__delitem__— mutation must go through_datadirectly.- The
mock_site.save()helper automatically appendsrevision,last_modified, andcreatedto every saved document, so tests that return a matched author exercised this code path in CI. - No behaviour changes to the matching logic itself — only the post-match metadata strip is fixed.
Testing
-
Run the affected test file: pytest openlibrary/catalog/add_book/tests/test_load_book.py -v All 35 tests should pass, including the new
test_infobase_metadata_keys_stripped_from_existing_authorwhich directly verifies thatlast_modified,revision, andcreatedare absent from the returned author when a match is found. -
Before this fix, 7 tests in that file failed with
AttributeError: __delitem__wheneverfind_entity()returned a match. All now pass.
Screenshot
N/A — no UI changes.
Stakeholders
Thank you @TechGenius-Karan for this PR!
🤖 Copilot has been assigned for an initial review.
The linked issue hasn't been triaged yet — triage happens on Mondays and Fridays. There are currently 99 open non-draft PRs in the review queue.
PR triage checklist (maintainers)
- [x] PR description — not empty; explains what the change does and how to verify it
- [x] References an issue — PR body contains
Closes #13016- [ ] Linked issue is triaged — has a
Priority: *label (not justNeeds: Triage) - [ ] Linked issue is assigned — has at least one assignee
- [ ] Linked issue is triaged — has a
- [x] Commit history clean — no WIP/fixup/conflict noise; commit messages are meaningful
- [ ] CI passing — pre-commit.ci still running at time of comment
- [x] Test cases present — 22 lines of new tests added in
test_load_book.py✓ - [x] Proof of testing — PR body describes
pytest openlibrary/catalog/add_book/tests/test_load_book.pyand expected outcomes ✓
[!NOTE] This comment was automatically generated by PAM, Open Library's Project AI Manager. PAM provides status visibility, performs basic project management functions, and gives actionable feedback so contributors aren't left waiting.