Repository navigation
Reject empty type and field names in namedutils - #462
Merged
mahmoud merged 1 commit intoSep 7, 2026
Merged
Conversation
`all()` returns True for the empty string, so an empty name passed both ValueError checks in the validation loop and reached `name[0].isdigit()`, raising IndexError instead of the intended ValueError. Both namedtuple and namedlist were affected, for an empty field name and an empty typename. The rename branch a few lines above already guards with `or not name`; add the same guard to the validation loop and extend the message, which was vacuously true for the empty string.
ozoboy93
approved these changes
Sep 4, 2026
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.
The validation loop in
namedtupleandnamedlistchecks names with:all()returnsTruefor an empty iterable, so an empty name passes this check. It then passes_iskeyword("")as well, and reachesname[0].isdigit()— which raisesIndexErrorinstead of the intendedValueError:Both factories are affected, and both an empty field name and an empty typename trigger it.
Why this looks like an oversight rather than a decision
The
renamebranch seven lines above the validation loop already guards for this exact case:So the guard exists in the same function; it is just missing from the loop that raises.
CPythonscollections.namedtuple, which this module is a variant of, rejects the same input properly:The change
Adds
or not nameto the validation loop in both functions, matching the form the rename branch already uses.I also extended the message. The existing wording — "can only contain alphanumeric characters and underscores" — is vacuously true of the empty string: it states a rule the empty string does not actually break, so it would not explain the failure. The new wording is "must be non-empty and can only contain alphanumeric characters and underscores". No test asserted on the old text. Happy to drop that part and keep the one-word condition change if you would rather leave the message alone.
The
rename=Truebranches are untouched.Tests
Added to
tests/test_namedutils.py, parametrized over both factories: empty field name, empty typename, an invalid character (stillValueError), and a valid pair (still accepted).Verified the tests pin the behaviour rather than just passing — against an unmodified
namedutils.pythe four empty-name cases fail with theIndexErrorabove:With the fix, and using the command from
tox.ini:Conflicts
#382 also touches
boltons/namedutils.py, but its hunks are at lines 45, 120-122 and 279-281, while this change is at 163 and 322 — no overlap.