Closed Mallington closed 11 months ago
Thank you submitting this feature request. If you have code OR a patch that you would like to submit to implement this new functionality, feel free to issue a PR.
In the meantime, I will consider this feature request and explore where it should fit in our priority list based on current staffing and workloads.
Thank you for your quick response. I created a monkey patch to replace the __hash__
implementation, however it is really inefficient and slow, because it is called multiple times when doing set comparisons. Hoping this will be useful for someone if they have the same issue though.
def __custom_hash(obj):
from builtins import dict
import json
if isinstance(obj, dict):
return hash(json.dumps(obj, sort_keys=True))
elif isinstance(obj, tuple):
return hash(tuple(__custom_hash(item) for item in obj))
else:
return hash(obj)
bigquery.AccessEntry.__hash__ = lambda self: __custom_hash(self._key())
I am gonna close this issue. With a potentially applicable workaround listed here for others who might encounter a similar issue and when considering the other items on our priority list, I do not see us fixing this anytime soon.
Description
I encountered an issue while attempting to use Google Cloud BigQuery's
AccessEntry
objects as hashable elements in sets or as keys in dictionaries. The problem arises due to the presence of nested dictionaries in the_key()
method of thebigquery.AccessEntry
class, which makes the objects unhashable.Expected Behavior
I expect to be able to use
bigquery.AccessEntry
objects as hashable elements in sets and as keys in dictionaries. This would enable more efficient and organized handling of access permissions for datasets within Google BigQuery and facilitate the usage of these objects in various Python data structures.Current Behavior
Currently, when attempting to use
bigquery.AccessEntry
objects in sets or dictionaries, the following error is encountered:Steps to Reproduce
Here's a code snippet that reproduces the issue:
Proposed Solution
To resolve this limitation, Google Cloud BigQuery's AccessEntry class needs to implement a proper hash method that considers the hashability of its all the fields, including dictionaires . Consider reimplementing the hash method to implement frozensets when hashing dictionaries:
Additional Information:
Impact:
This issue affects users who wish to leverage Python's built-in set operations and any data structures that rely on the hashing function.
Thank you for your help in advance :)