Repository navigation
Conversation
fametrano
reviewed
Oct 18, 2026
fametrano
left a comment
There was a problem hiding this comment.
I'd keep ValueError here and leave the word itself out of the message. to_entropy already raises ValueError for a wrong word count and a bad checksum, and LookupError is not a subclass of it, so code that catches ValueError today would miss this error. A misspelled word is usually one or two letters from a word of the mnemonic and exception messages end up in logs, so the position is safer, for example word 12 is not in the word list.
Comment on lines
+168
to
171
| try: | ||
| ndx = self.wordlist.index(self.normalize_string(word)) | ||
| except ValueError: | ||
| raise LookupError('Unable to find "%s" in word list.' % word) |
There was a problem hiding this comment.
This keeps ValueError and gives the position instead of the word. Raising outside the except block also keeps the original 'abuot' is not in list out of the exception and its traceback.
Suggested change
| try: | |
| ndx = self.wordlist.index(self.normalize_string(word)) | |
| except ValueError: | |
| raise LookupError('Unable to find "%s" in word list.' % word) | |
| try: | |
| ndx = self.wordlist.index(self.normalize_string(word)) | |
| except ValueError: | |
| ndx = -1 | |
| if ndx < 0: | |
| raise ValueError("Word %d is not in the word list." % (wordindex + 1)) |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
to_entropy()looks up each word's index withlist.index():list.index()never returns a negative value — when the element isnot found, it raises
ValueErrordirectly instead of returning-1.As a result, the
if ndx < 0branch is unreachable: it can never betrue, because if the word isn't in the wordlist the exception is
already raised two lines earlier, before the check even runs.
Impact
When an invalid/misspelled word is passed to
to_entropy(), callersget a raw
ValueErrorwith Python's generic list-lookup message(e.g.
'notaword' is not in list) instead of the intended,descriptive
LookupError(Unable to find "notaword" in word list.). This is misleading for end users debugging a bad mnemonic,and breaks any caller that specifically catches
LookupError(matching how the rest of this library reports lookup failures)
without also catching
ValueError.Reproduction
Fix
Wrap the
.index()call in atry/except ValueErrorand raise theintended
LookupErrorfrom there, instead of checking a conditionthat can never be true:
Testing
a raw
ValueErrorwas raised instead ofLookupError.LookupError: Unable to find "notaword" in word list.is nowraised as intended.
pytest tests/) before andafter the change: all 7 tests pass in both cases, confirming no
regression to existing checksum/entropy behavior.
This is a pure error-handling fix; the entropy/checksum computation
logic is unchanged.