Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit d5b1455

Browse files
committed
fix: ensure AccessEntry equality and repr uses the correct entity_type
1 parent 2d173a5 commit d5b1455

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

google/cloud/bigquery/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def __ne__(self, other):
512512
return not self == other
513513

514514
def __repr__(self):
515-
return f"<AccessEntry: role={self.role}, {self._entity_type}={self.entity_id}>"
515+
return f"<AccessEntry: role={self.role}, {self.entity_type}={self.entity_id}>"
516516

517517
def _key(self):
518518
"""A tuple key that uniquely describes this field.
@@ -531,7 +531,7 @@ def _key(self):
531531
properties["condition"] = condition_key
532532

533533
prop_tup = tuple(sorted(properties.items()))
534-
return (self.role, self._entity_type, self.entity_id, prop_tup)
534+
return (self.role, self.entity_type, self.entity_id, prop_tup)
535535

536536
def __hash__(self):
537537
return hash(self._key())

tests/unit/test_dataset.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,15 @@ def test_equality_and_hash_without_condition(self):
613613
assert hash(entry1) == hash(entry2)
614614
assert hash(entry1) != hash(entry3) # Usually true
615615

616+
def test_equality_and_hash_from_api_repr(self):
617+
"""Compare equal entries where one was created via from_api_repr."""
618+
entry1 = AccessEntry("OWNER", "specialGroup", "projectOwners")
619+
entry2 = AccessEntry.from_api_repr(
620+
{"role": "OWNER", "specialGroup": "projectOwners"}
621+
)
622+
assert entry1 == entry2
623+
assert hash(entry1) == hash(entry2)
624+
616625
def test_equality_and_hash_with_condition(self, condition_1, condition_2):
617626
cond1a = Condition(
618627
condition_1.expression, condition_1.title, condition_1.description
@@ -746,6 +755,13 @@ def test_dataset_property_with_condition(self, condition_1):
746755
assert "dataset" in entry._properties
747756
assert "condition" in entry._properties
748757

758+
def test_repr_from_api_repr(self):
759+
"""Check that repr() includes the correct entity_type when the object is initialized from a dictionary."""
760+
api_repr = {"role": "OWNER", "userByEmail": "owner@example.com"}
761+
entry = AccessEntry.from_api_repr(api_repr)
762+
entry_str = repr(entry)
763+
assert entry_str == "<AccessEntry: role=OWNER, userByEmail=owner@example.com>"
764+
749765

750766
class TestDatasetReference(unittest.TestCase):
751767
@staticmethod

0 commit comments

Comments
 (0)