From a4a24bac2ed7743062eb4dd918b2e54d0843ddef Mon Sep 17 00:00:00 2001 From: David Cain Date: Sat, 17 Jun 2023 09:32:01 -0600 Subject: [PATCH] Use `zip(strict=True)` to avoid exhaustion bug The `zip()` function just silently stops iterating both iterables as soon as one is exhausted, which can lead to subtle bugs. Avoid that. --- pyproject.toml | 1 - ws/tests/email/test_sole.py | 6 ++++-- ws/utils/member_sheets.py | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a59d1d6e..69963549 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -100,7 +100,6 @@ ignore = [ # Errors which I'll plan to fix soon: "B904", # Use `raise from` to preserve context (also see TRY200) "PLR0913", # Refactor functions with too many args - "B905", # Use `strict()` on `zip()` "DJ001", # Avoid nullable charfields "DJ006", # Use fields instead of exclude with `ModelForm` "DJ008", # Each model should declare `__str__` diff --git a/ws/tests/email/test_sole.py b/ws/tests/email/test_sole.py index fc42e5a1..d4dd7282 100644 --- a/ws/tests/email/test_sole.py +++ b/ws/tests/email/test_sole.py @@ -219,8 +219,10 @@ def test_has_drivers(self): # Make sure that both participant and leader drivers are given in the table! drivers_table = soup.find('h3', string='Drivers').find_next_sibling('table') header = [el.text for el in drivers_table.find('thead').find_all('th')] - rows = drivers_table.find('tbody').find_all('tr') - drivers = [zip(header, (td.text for td in row.find_all('td'))) for row in rows] + drivers = [ + zip(header, (td.text for td in row.find_all('td')), strict=True) + for row in drivers_table.find('tbody').find_all('tr') + ] expected = [ [ ('Driver', 'Tim Beaver'), diff --git a/ws/utils/member_sheets.py b/ws/utils/member_sheets.py index 1430a303..c5cb2126 100644 --- a/ws/utils/member_sheets.py +++ b/ws/utils/member_sheets.py @@ -248,7 +248,7 @@ def get_row(self, participant: models.Participant) -> tuple[str, ...]: def _assign(cells, values): - for cell, value in zip(cells, values): + for cell, value in zip(cells, values, strict=True): cell.value = value @@ -308,7 +308,7 @@ def update_discount_sheet(discount: models.Discount, trust_cache: bool) -> None: _assign(next(rows), writer.header) # Update each row with current membership information - for participant, row in zip(participants, rows): + for participant, row in zip(participants, rows, strict=True): _assign(row, writer.get_row(participant)) # Batch update to minimize API calls