googleapis / googleapis/google-cloud-python
google-cloud-firestore: Accept values of generic `Mapping` type (e.g. Python 3.15 `frozendict`) in addition to `dict` as field values when creating / replacing / merging a document in the Firestore database
- Ngôn ngữ chính
- Python
- Star
- 5.4k
- Fork
- 1.8k
- Merge trung bình
- 3 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 122
Mô tả
### Determine this is the right repository
- [x] I determined this is the correct repository in which to report this feature request.
### 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`:
https://github.com/googleapis/google-cloud-python/blob/3bbcbdf1a62dd10ab96f72e5c8198cffe9174ac7/packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py#L234-L245
That means the code here
https://github.com/googleapis/google-cloud-python/blob/3bbcbdf1a62dd10ab96f72e5c8198cffe9174ac7/packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py#L224-L227
could be simply checking
```python
if isinstance(value, collections.abc.Mapping):
...
```
### Desired code experience
```python
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:
https://github.com/googleapis/google-cloud-python/blob/3bbcbdf1a62dd10ab96f72e5c8198cffe9174ac7/packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py#L229-L231
### 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:
```python
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
```
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.