Fix get() fallback for unset model fields - #704
Open
PEliet wants to merge 1 commit into
Open
Conversation
Signed-off-by: PEliet <2672784691@qq.com>
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.
SubscriptableBaseModelexposes mapping-style helpers, butget()usedhasattr()to decide whether a field was present. Pydantic exposes unsetoptional fields as attributes with their class default, so an unset field such
as
Message.contentreturnedNoneinstead of the fallback passed toget().This also disagreed with
__contains__and__getitem__, which already treatthe same field as absent.
Use the model's existing membership semantics in
get(). The regression testcovers both sides of the distinction: an unset optional field returns the
fallback, while a field explicitly set to
Nonestill returnsNone.Validation:
pytest tests/test_type_serialization.py::test_subscriptable_model_get_returns_default_for_unset_field -qpytest ollama/_types.py --doctest-modules -qruff check ollama/_types.py tests/test_type_serialization.pyruff format --check ollama/_types.py tests/test_type_serialization.pypytest ollama tests -q -k "not generate_images and not create_blob and not image_serialization_path and not image_serialization_string_path"(91 passed, 8 deselected)The eight deselected tests use open
NamedTemporaryFilehandles in ways thatfail on Windows. This is the existing issue #676 and is unrelated to this
change.
Prepared with assistance from OpenAI Codex.