GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — feat(firestore): add PyMongo duck-typing serialization support by ohmayr · Pull Request #18406 · googleapis/google-cloud-python Conversation
|
| if hasattr(value, "__class__"): | ||
| cls_name = value.__class__.__name__ | ||
| if cls_name == "ObjectId" and hasattr(value, "binary"): | ||
| return encode_value({"__oid__": str(value).lower()}) | ||
| if cls_name == "Decimal128" and hasattr(value, "to_decimal"): | ||
| return encode_value({"__decimal128__": str(value)}) | ||
| if cls_name == "Regex" and hasattr(value, "pattern"): | ||
| opts = getattr(value, "flags", "") or getattr(value, "options", "") | ||
| return encode_value( | ||
| {"__regex__": {"pattern": value.pattern, "options": str(opts)}} | ||
| ) | ||
| if cls_name == "Timestamp" and hasattr(value, "time") and hasattr(value, "inc"): | ||
| return encode_value( | ||
| { | ||
| "__request_timestamp__": { | ||
| "seconds": value.time, | ||
| "increment": value.inc, | ||
| } | ||
| } | ||
| ) | ||
| if cls_name == "MinKey": | ||
| return encode_value({"__min__": None}) | ||
| if cls_name == "MaxKey": | ||
| return encode_value({"__max__": None}) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The encode_value function is a critical hot path called recursively for every field of every document during serialization. While the proposed change aims to optimize standard types using an O(1) set lookup, any changes to this performance-critical code path must be validated and benchmarked to ensure they do not degrade performance or eliminate fast-path optimizations (such as the overhead of double getattr calls). Please run benchmarks to verify the performance impact of this change.
cls_name = getattr(getattr(value, "__class__", None), "__name__", None) if cls_name in {"ObjectId", "Decimal128", "Regex", "Timestamp", "MinKey", "MaxKey"}: if cls_name == "ObjectId" and hasattr(value, "binary"): return encode_value({"__oid__": str(value).lower()}) elif cls_name == "Decimal128" and hasattr(value, "to_decimal"): return encode_value({"__decimal128__": str(value)}) elif cls_name == "Regex" and hasattr(value, "pattern"): opts = getattr(value, "flags", "") or getattr(value, "options", "") return encode_value( {"__regex__": {"pattern": value.pattern, "options": str(opts)}} ) elif cls_name == "Timestamp" and hasattr(value, "time") and hasattr(value, "inc"): return encode_value( { "__request_timestamp__": { "seconds": value.time, "increment": value.inc, } } ) elif cls_name == "MinKey": return encode_value({"__min__": None}) elif cls_name == "MaxKey": return encode_value({"__max__": None})
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Addressed: Moved the duck-typing inspection entirely out of the standard serialization hot path down to the fallback position right before TypeError is raised. Standard native Python types (int, float, str, dict, list, etc.) and native Firestore types bypass this check completely with zero overhead.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
e40a2d2 to
06445ee
Compare
06445ee to
80c0c3f
Compare
80c0c3f to
740326c
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
it looks like we should add to this type here
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Done! Updated the encode_value docstring Args to include ~google.cloud.firestore_v1.bson._BSONType as well as supported BSON / PyMongo-compatible types.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
740326c to
7038386
Compare
7038386 to
337ec30
Compare
337ec30 to
bb5e132
Compare
bb5e132 to
f5c66e0
Compare
There was an error while loading. Please reload this page.
| value encoded as a Firestore protobuf. | ||
|
|
||
| Raises: | ||
| TypeError: If the ``value`` is not one of the accepted types. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think some of the BSON constructors raise ValueError or others. We should probably either update this docstring to be exhaustive, or catch and wrap the errors raised in _try_duck_type_bson
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
f5c66e0 to
4a5b47f
Compare
4a5b47f to
03363e3
Compare
03363e3 to
018974b
Compare
018974b to
977f9c0
Compare
6f6861c to
1316f5b
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| if isinstance(value, bytes): | ||
| subtype = getattr(value, "subtype", None) | ||
| if subtype is not None: |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be if suptype is not None and subtype > 1? (or just if subtype:, but that might not be as clear)
IIRC, BSONBinary rejects subtype of 0
Sorry, something went wrong.
There was an error while loading. Please reload this page.
4f2ed99 to
2ca7478
Compare
2ca7478 to
d3b1541
Compare
d3b1541 to
a1779fe
Compare
…ate to BSON types Move duck-typing checks in encode_value() to a fallback position before raising TypeError, preserving hot-path performance for standard types. Delegate conversion of duck-typed objects to Firestore BSON classes rather than manually creating wire dictionaries, and document supported BSON types in the encode_value docstring.
… typing Support PyMongo Binary subclasses without losing subtype tags by inspecting subtype attribute in bytes check. Support integer regex bitmasks in duck-typed Regex by mapping standard re flags to BSON options characters. Document ValueError in encode_value.
a1779fe to
8b6c670
Compare
There was an error while loading. Please reload this page.
🤖 I have created a release *beep* *boop* --- <details><summary>django-google-spanner: 5.2.0</summary> ## [5.2.0](django-google-spanner-v5.1.0...django-google-spanner-v5.2.0) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>gapic-generator: 1.41.0</summary> ## [1.41.0](gapic-generator-v1.40.0...gapic-generator-v1.41.0) (2026-09-29) ### Features * **gapic-generator:** add schema support for resumable uploads ([#18480](#18480)) ([78ff458](78ff458)) ### Bug Fixes * **gapic-generator:** init mock response in version header test ([#18488](#18488)) ([7871fa9](7871fa9)) </details> <details><summary>google-api-core: 2.40.0</summary> ## [2.40.0](google-api-core-v2.39.0...google-api-core-v2.40.0) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>google-auth-httplib2: 0.4.3</summary> ## [0.4.3](google-auth-httplib2-v0.4.2...google-auth-httplib2-v0.4.3) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>google-auth-oauthlib: 1.5.0</summary> ## [1.5.0](google-auth-oauthlib-v1.4.1...google-auth-oauthlib-v1.5.0) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) ### Bug Fixes * **google-auth-oauthlib:** prevent ipv6 address reuse ([#18463](#18463)) ([30b4f44](30b4f44)) </details> <details><summary>google-cloud-bigtable: 2.48.0</summary> ## [2.48.0](google-cloud-bigtable-v2.47.0...google-cloud-bigtable-v2.48.0) (2026-09-29) ### Features * **bigtable:** Reroute Mutations Batcher to use data client ([#18200](#18200)) ([0b488a3](0b488a3)) * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>google-cloud-core: 2.8.0</summary> ## [2.8.0](google-cloud-core-v2.7.0...google-cloud-core-v2.8.0) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>google-cloud-dns: 0.37.2</summary> ## [0.37.2](google-cloud-dns-v0.37.1...google-cloud-dns-v0.37.2) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>google-cloud-documentai-toolbox: 0.17.4</summary> ## [0.17.4](google-cloud-documentai-toolbox-v0.17.3...google-cloud-documentai-toolbox-v0.17.4) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>google-cloud-firestore: 2.33.0</summary> ## [2.33.0](google-cloud-firestore-v2.32.0...google-cloud-firestore-v2.33.0) (2026-09-29) ### Features * **firestore:** add BSON cross-type query ordering support ([#18405](#18405)) ([13be295](13be295)) * **firestore:** add BSON read deserialization support ([#18402](#18402)) ([ce2544f](ce2544f)) * **firestore:** add PyMongo duck-typing serialization support ([#18406](#18406)) ([a1e0e00](a1e0e00)) </details> <details><summary>google-cloud-ndb: 2.7.0</summary> ## [2.7.0](google-cloud-ndb-v2.6.1...google-cloud-ndb-v2.7.0) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>google-cloud-runtimeconfig: 0.37.2</summary> ## [0.37.2](google-cloud-runtimeconfig-v0.37.1...google-cloud-runtimeconfig-v0.37.2) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>google-cloud-storage: 3.15.1</summary> ## [3.15.1](google-cloud-storage-v3.15.0...google-cloud-storage-v3.15.1) (2026-09-29) ### Bug Fixes * **storage:** add correct app hub uri prefix to aco traces ([#18483](#18483)) ([7cead02](7cead02)) </details> <details><summary>google-cloud-testutils: 1.10.0</summary> ## [1.10.0](google-cloud-testutils-v1.9.3...google-cloud-testutils-v1.10.0) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>google-resumable-media: 2.11.0</summary> ## [2.11.0](google-resumable-media-v2.10.2...google-resumable-media-v2.11.0) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> <details><summary>googleapis-common-protos: 1.75.5</summary> ## [1.75.5](googleapis-common-protos-v1.75.4...googleapis-common-protos-v1.75.5) (2026-09-29) ### Bug Fixes * resolve error where google/longrunning/operations.proto is missing ([#18477](#18477)) ([e1d306a](e1d306a)), refs [#18478](#18478) </details> <details><summary>proto-plus: 1.29.0</summary> ## [1.29.0](proto-plus-v1.28.4...proto-plus-v1.29.0) (2026-09-29) ### Features * declare Python3.15 support ([05b0c34](05b0c34)) </details> --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.