close
Skip to content

bug(add_book): walrus operator in author dedup loop assigns bool instead of key string #13015

Description

@mekarpeles

Problem

In catalog/add_book/load_book.py, an author deduplication loop uses a walrus operator with incorrect operator precedence. The dedup never works for any record with more than one author.

Location

openlibrary/catalog/add_book/load_book.py — in import_record_to_edition().

Bug

# Current (buggy):
seen = set()
for a in authors:
    if key := a["key"] in seen:  # walrus: key = (a["key"] in seen) — bool, not string
        continue
    seen.add(key)  # adds False or True, not the author key string

The walrus operator := has lower precedence than in. Python parses this as key := (a["key"] in seen). So:

  • key is assigned the boolean result of a["key"] in seen
  • After the first iteration: seen = {False}
  • For all subsequent iterations: a["key"] in seen is False (a key string is not in {False}), so seen.add(False) is a no-op

The intended deduplication never triggers for records with 2+ authors.

Fix

seen = set()
for a in authors:
    if a["key"] in seen:
        continue
    seen.add(a["key"])

Impact

The runtime impact is benign in most cases — downstream author-resolution logic handles actual deduplication at a different level. However, the broken loop means:

  • Every record with multiple authors does redundant work
  • If two authors in the same record have identical OL keys, both are passed downstream instead of one being deduplicated

Related

This is a pure author deduplication bug within import_record_to_edition(), separate from the author-matching logic in match.py.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: ResponseIssues which require feedback from leadType: BugSomething isn't working. [managed]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions