Skip to content

fix: tuple/list dict keys cross-match when elements overlap (fixes #312)#351

Open
gaoflow wants to merge 1 commit into
keleshev:masterfrom
gaoflow:fix-tuple-dict-keys-cross-match
Open

fix: tuple/list dict keys cross-match when elements overlap (fixes #312)#351
gaoflow wants to merge 1 commit into
keleshev:masterfrom
gaoflow:fix-tuple-dict-keys-cross-match

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown

Tuple/list dict keys cross-match when elements overlap because
Schema(skey).validate(key) treats tuple keys as ITERABLE, building
Or(*elements) semantics. Two distinct tuple keys with overlapping
elements (e.g. ('a','b') and ('b','b')) interfere — the first
greedily matches the second data key.

Fixes #312.

from schema import Schema
s = Schema({('a', 'b'): 'ok', ('b', 'b'): 'also ok'})
s.validate({('a', 'b'): 'ok', ('b', 'b'): 'also ok'})
# Before: SchemaMissingKeyError: Missing key: Or('b', 'b')
# After:  returns validated data

Root cause: In Schema.validate DICT flavor, tuple/list keys are
routed through the ITERABLE validation path which builds Or(*elements),
turning key matching into set-membership instead of exact literal
comparison.

Fix (+11/-1): When a schema key is tuple/list, compare exactly
(skey == key) instead of routing through Schema(skey).validate(key).
Non-tuple/list keys (strings, Optional, Or, And, Regex, etc.) continue
through the normal validation path unchanged.

120/120 tests pass.

This pull request was prepared with the assistance of AI, under my
direction and review.

…dation

Schema keys that are tuples or lists were validated through the
ITERABLE path which uses Or semantics on elements. When two tuple
keys had overlapping elements (e.g. ('a','b') and ('b','b')),
the first key would incorrectly match the second data key because
Or('a','b') matches all elements of ('b','b').

Fix by using exact comparison for tuple/list schema keys during
dict key matching, matching the expectation that dict keys should
be compared literally rather than as sets of allowed values.

Fixes keleshev#312
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.

Two tuples as dictionary keys report a false alarm of SchemaMissingKeyError

1 participant