Skip to content

Fix TypeError building key error message for a tuple key (#253)#347

Merged
skorokithakis merged 1 commit into
keleshev:masterfrom
vineethsaivs:fix/tuple-key-error-format
Jun 13, 2026
Merged

Fix TypeError building key error message for a tuple key (#253)#347
skorokithakis merged 1 commit into
keleshev:masterfrom
vineethsaivs:fix/tuple-key-error-format

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

Fixes #253.

When a key's value fails validation, the key error message is built with:

k = "Key '%s' error:" % nkey

If nkey is a tuple, % interprets it as the format arguments rather than a single value. For the empty tuple this raises TypeError: not enough arguments for format string, and for other tuple keys it would consume the tuple's elements as multiple format args.

>>> Schema({(): [(str,)]}).is_valid({(): ["foo", ("bar",)]})
TypeError: not enough arguments for format string

Wrapping the key in a single-element tuple (% (nkey,)) formats it as one value:

>>> Schema({(): [(str,)]}).is_valid({(): ["foo", ("bar",)]})
False
>>> Schema({("a", "b"): int}).validate({("a", "b"): "x"})
SchemaError: Key '('a', 'b')' error: ...

Added test_tuple_key_error_message_does_not_crash, which fails on master and passes with this change. All 118 existing tests still pass.


Disclosure: prepared with AI assistance; reviewed by me and I can explain every line.

When a key's value failed validation, the error message was built with
`"Key '%s' error:" % nkey`. If the key is a tuple (including the empty tuple),
`%` treats it as the format arguments rather than a single value, so an empty
tuple key raised `TypeError: not enough arguments for format string` and other
tuple keys would consume their elements as multiple format args.

Wrap the key in a single-element tuple (`% (nkey,)`) so it is always formatted
as one value.

Fixes keleshev#253

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@skorokithakis skorokithakis merged commit 27e268d into keleshev:master Jun 13, 2026
2 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: TypeError: not enough arguments for format string

2 participants