Skip to content

Commit

Permalink
Use zip(strict=True) to avoid exhaustion bug
Browse files Browse the repository at this point in the history
The `zip()` function just silently stops iterating both iterables as
soon as one is exhausted, which can lead to subtle bugs. Avoid that.
  • Loading branch information
DavidCain committed Jun 17, 2023
1 parent 577f1fb commit a4a24ba
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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__`
Expand Down
6 changes: 4 additions & 2 deletions ws/tests/email/test_sole.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 2 additions & 2 deletions ws/utils/member_sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a4a24ba

Please sign in to comment.