Determine this is the right repository
Summary of the feature request
I'd like to be able to use other dictionary types, custom ones and frozendict (Python 3.15+), when adding or updating documents in Firestore without having to create new dict objects by wrapping my instance (dict(my_mapping)).
Looking at the code, the input value doesn't need to be mutable at all (as dict is), and it only needs to support .items(), so it really can be any valid collections.abc.Mapping:
|
def encode_dict(values_dict) -> dict: |
|
"""Encode a dictionary into protobuf ``Value``-s. |
|
|
|
Args: |
|
values_dict (dict): The dictionary to encode as protobuf fields. |
|
|
|
Returns: |
|
Dict[str, ~google.cloud.firestore_v1.types.Value]: A |
|
dictionary of string keys and ``Value`` protobufs as dictionary |
|
values. |
|
""" |
|
return {key: encode_value(value) for key, value in values_dict.items()} |
That means the code here
|
if isinstance(value, dict): |
|
value_dict = encode_dict(value) |
|
value_pb = document.MapValue(fields=value_dict) |
|
return document.Value(map_value=value_pb) |
could be simply checking
if isinstance(value, collections.abc.Mapping):
...
Desired code experience
from collections import UserDict
class UpperCaseDict(UserDict):
def __setitem__(self, key, item):
key = key.upper()
super().__setitem__(key, item)
record = {"my_dict": UpperCaseDict({"one": 1, "two": 2})}
db = firestore.client(...)
db.collection("cities").add(record) # no errors
Expected results
No errors for valid mapping types:
|
raise TypeError( |
|
"Cannot convert to a Firestore Value", value, "Invalid type", type(value) |
|
) |
API client name and version
google-cloud-firestore v2.30.0
Use case
Uploading my custom mapping instances directly without conversion.
Additional context
A few valid mapping types currently cause an exception:
from collections import UserDict, defaultdict
from collections.abc import Mapping
from types import MappingProxyType
class UpperCaseDict(UserDict):
def __setitem__(self, key, item):
key = key.upper()
super().__setitem__(key, item)
d = {"one": 1, "two": 2}
dd = defaultdict(int, d)
proxy = MappingProxyType(d)
fd = frozendict(one=1, two=2) # Python 3.15+
uppercase = UpperCaseDict({"one": 1, "two": 2})
print(isinstance(d, Mapping)) # True
print(isinstance(d, dict)) # True
print(isinstance(dd, Mapping)) # True
print(isinstance(dd, dict)) # True
print(isinstance(proxy, Mapping)) # True
print(isinstance(proxy, dict)) # False
print(isinstance(fd, Mapping)) # True
print(isinstance(fd, dict)) # False
print(isinstance(uppercase, Mapping)) # True
print(isinstance(uppercase, dict)) # False
Determine this is the right repository
Summary of the feature request
I'd like to be able to use other dictionary types, custom ones and
frozendict(Python 3.15+), when adding or updating documents in Firestore without having to create newdictobjects by wrapping my instance (dict(my_mapping)).Looking at the code, the input value doesn't need to be mutable at all (as
dictis), and it only needs to support.items(), so it really can be any validcollections.abc.Mapping:google-cloud-python/packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py
Lines 234 to 245 in 3bbcbdf
That means the code here
google-cloud-python/packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py
Lines 224 to 227 in 3bbcbdf
could be simply checking
Desired code experience
Expected results
No errors for valid mapping types:
google-cloud-python/packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py
Lines 229 to 231 in 3bbcbdf
API client name and version
google-cloud-firestore v2.30.0
Use case
Uploading my custom mapping instances directly without conversion.
Additional context
A few valid mapping types currently cause an exception: