Add Object.to_dict() and CrossReference.__repr__ for JSON-esque output - #2134
Open
shaikn6 wants to merge 1 commit into
Open
Add Object.to_dict() and CrossReference.__repr__ for JSON-esque output#2134shaikn6 wants to merge 1 commit into
shaikn6 wants to merge 1 commit into
Conversation
Closes weaviate#881. - _Object.to_dict() converts a query result (Object/ObjectSingleReturn/ GroupByObject) into a JSON-serializable dict, handling datetime, UUID, GeoCoordinate, and PhoneNumber field types, and recursively expanding cross-referenced objects. - _CrossReference.__repr__ shows its objects instead of the default memory-address repr, so printing an object with cross-references is readable out of the box. Includes unit tests covering plain objects, datetime/UUID conversion, recursive cross-reference expansion, and the new repr.
There was a problem hiding this comment.
Orca Security Scan Summary
| Status | Check | Issues by priority | |
|---|---|---|---|
| Infrastructure as Code | View in Orca | ||
| SAST | View in Orca | ||
| Secrets | View in Orca | ||
| Vulnerabilities | View in Orca |
|
To avoid any confusion in the future about your contribution to Weaviate, we work with a Contributor License Agreement. If you agree, you can simply add a comment to this PR that you agree with the CLA so that we can merge. |
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.
Closes #881.
What
_Object.to_dict()(inherited byObject,ObjectSingleReturn,GroupByObject) converts a query result into a fully JSON-serializable dict — handlingdatetime→ isoformat,UUID→ str,GeoCoordinate/PhoneNumber→ JSON, and recursively expanding cross-referenced objects into their ownto_dict()output._CrossReference.__repr__now shows its objects (CrossReference(objects=[...])) instead of the default<... object at 0x...>memory address.Why
Per the issue and the existing comment on it, printing/serializing query results — especially ones with cross-references — currently either falls back to Python's default repr or requires hand-rolling a converter.
to_dict()gives an explicit, opt-in way to get a JSON-serializable representation; the__repr__fix makes ad-hocprint()/logging readable without calling anything.Scope notes
to_dict()as an explicit method rather than overridingObject.__repr__/__str__, so existing scripts/logs that already print objects aren't affected — onlyCrossReference's repr changes.CollectionConfigalready had its ownto_dict()(added separately, for REST-payload serialization) — this PR doesn't touch that, it fills the gap on the query-result side (Object/CrossReference).Testing
test/collection/test_object_to_dict.py— all passingtest/collection/suite: 326 passed, 0 regressionstest/test_timeout.pyreproduced identically on a clean checkout (not caused by this change)ruff check,ruff format --check, andflake8(matching this repo's pre-commit config) all clean