Preserve key type for Optional defaults#350
Open
gaoflow wants to merge 1 commit into
Open
Conversation
Optional(key, default=...) stored self.key as str(self._schema), so when an optional key was absent the default filled the output under the stringified key (e.g. '5' or 'None'), while a present key kept its original type (5, None). The output key type silently depended on whether the data contained the key. Store the original key object instead (unwrapping a Literal, whose str() form the old code relied on), mirroring Hook.key. String keys are unchanged; int/None/float/bool keys now keep their type whether the key is present or filled from the default.
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.
Summary
Optional(key, default=...)stringifies the key, so when the optional key is absent the default fills the output dict understr(key), while when the key is present the original-typed key is used. The output key type silently depends on whether the data contained the key.The same affects
None(→'None'),float, andboolkeys. String keys are unaffected (str("a") == "a"), which is why the existing tests — all using string keys — never caught it.Cause
Optional.__init__setself.key = str(self._schema). The default-fill path then writesnew[default.key] = ..., so the stringified key reached the output. A present key, by contrast, flows through normally and keeps its type.Fix
Store the original key object (unwrapping a
Literal, whosestr()form the old code relied on fordescription/titlekeys), mirroringHook.key = self._schema:Now present and absent paths produce the same key type.
Literaland plain string keys are unchanged.Verification
test_dict_optional_default_preserves_key_type, asserting thatint/None/floatkeys keep their type when filled from the default and that present/absent agree. It fails before the fix and passes after.ruff checkadds no new errors (the pre-existingB023reports injson_schemaare untouched);ruff format --checkreports already-formatted.This pull request was prepared with the assistance of AI, under my direction and review.