Skip to content

Fix IndexError in protein_name_desc() due to off-by-one#25

Merged
richarda23 merged 1 commit into
mainfrom
fix/22-protein-name-desc-off-by-one
Mar 8, 2026
Merged

Fix IndexError in protein_name_desc() due to off-by-one#25
richarda23 merged 1 commit into
mainfrom
fix/22-protein-name-desc-off-by-one

Conversation

@richarda23

Copy link
Copy Markdown
Owner

Summary

Fixes #22.

random.randint(a, b) is inclusive on both ends. Using len(protein_names) as the upper bound means it can return an index equal to the list length, which is out of range and raises an IndexError.

Fixed by changing:

index = random.randint(0, len(dna_data.protein_names))

to:

index = random.randint(0, len(dna_data.protein_names) - 1)

Test plan

  • test_protein_name_desc_no_index_error calls the method 1000 times — fails before fix, passes after
  • Full test suite passes: poetry run python -m pytest faker_biology/tests

🤖 Generated with Claude Code

random.randint(a, b) is inclusive on both ends, so using
len(protein_names) as the upper bound could return an index equal to
the list length, causing an IndexError. Fixed by using len() - 1.

Adds a regression test that calls protein_name_desc() 1000 times to
reliably expose the error before the fix.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@richarda23
richarda23 merged commit f64c8da into main Mar 8, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: off-by-one IndexError risk in protein_name_desc

2 participants