Skip to content

Don't save number of pages 0 from Amazon #10888

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

Merged
merged 1 commit into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions openlibrary/core/vendors.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,15 @@ def serialize(product: Any) -> dict:
if contrib.role == 'Translator'
],
'publishers': list({p for p in (brand, manufacturer) if p}),
'number_of_pages': (
edition_info
and edition_info.pages_count
and edition_info.pages_count.display_value
**(
{'number_of_pages': edition_info.pages_count.display_value}
if (
edition_info
and edition_info.pages_count
# Note this intentionally excludes 0
and edition_info.pages_count.display_value
)
else {}
Comment on lines +317 to +325
Copy link
Preview

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The dictionary-unpacking with **({ ... }) adds complexity. Consider building the number_of_pages key directly in the surrounding dict (e.g., using a single conditional assignment) to improve readability.

Suggested change
**(
{'number_of_pages': edition_info.pages_count.display_value}
if (
edition_info
and edition_info.pages_count
# Note this intentionally excludes 0
and edition_info.pages_count.display_value
)
else {}
'number_of_pages': (
edition_info.pages_count.display_value
if (
edition_info
and edition_info.pages_count
# Note this intentionally excludes 0
and edition_info.pages_count.display_value
)
else None

Copilot uses AI. Check for mistakes.

),
'edition_num': (
edition_info
Expand Down
8 changes: 7 additions & 1 deletion openlibrary/tests/core/test_vendors.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,13 @@ def test_serialize_sample_record() -> None:
}


def test_serialize_pages_0() -> None:
amz_item = get_sample_amazon_item()
amz_item.item_info.content_info.pages_count.display_value = 0
result = AmazonAPI.serialize(amz_item)
assert 'number_of_pages' not in result


def test_serialize_does_not_load_translators_as_authors() -> None:
"""Ensure data load does not load translators as author and relies on fake API response objects"""
classification = None
Expand Down Expand Up @@ -459,7 +466,6 @@ def test_serialize_does_not_load_translators_as_authors() -> None:
{'role': 'Translator', 'name': 'Second Translator'},
],
'publishers': [],
'number_of_pages': '',
'edition_num': '',
'publish_date': '',
'product_group': None,
Expand Down
Loading