Skip to content

Commit 9eaa786

Browse files
authored
fix: skip empty policy bindings in len() and iter() (googleapis#159)
Exclude empty policy bindings (bindings with no members) in `Policy.__iter__()` and `Policy.__len__()` Follow up to googleapis#155
1 parent 1d76b57 commit 9eaa786

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

google/api_core/iam.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,22 @@ def __init__(self, etag=None, version=None):
125125

126126
def __iter__(self):
127127
self.__check_version__()
128-
return (binding["role"] for binding in self._bindings)
128+
# Exclude bindings with no members
129+
return (binding["role"] for binding in self._bindings if binding["members"])
129130

130131
def __len__(self):
131132
self.__check_version__()
132-
return len(self._bindings)
133+
# Exclude bindings with no members
134+
return len(list(self.__iter__()))
133135

134136
def __getitem__(self, key):
135137
self.__check_version__()
136138
for b in self._bindings:
137139
if b["role"] == key:
138140
return b["members"]
139-
# binding does not yet exist, create one
141+
# If the binding does not yet exist, create one
142+
# NOTE: This will create bindings with no members
143+
# which are ignored by __iter__ and __len__
140144
new_binding = {"role": key, "members": set()}
141145
self._bindings.append(new_binding)
142146
return new_binding["members"]

tests/unit/test_iam.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def test_ctor_defaults(self):
3232
policy = self._make_one()
3333
assert policy.etag is None
3434
assert policy.version is None
35-
assert len(policy) == 0
36-
assert dict(policy) == {}
3735
assert policy.owners == empty
3836
assert policy.editors == empty
3937
assert policy.viewers == empty
38+
assert len(policy) == 0
39+
assert dict(policy) == {}
4040

4141
def test_ctor_explicit(self):
4242
VERSION = 1
@@ -45,11 +45,11 @@ def test_ctor_explicit(self):
4545
policy = self._make_one(ETAG, VERSION)
4646
assert policy.etag == ETAG
4747
assert policy.version == VERSION
48-
assert len(policy) == 0
49-
assert dict(policy) == {}
5048
assert policy.owners == empty
5149
assert policy.editors == empty
5250
assert policy.viewers == empty
51+
assert len(policy) == 0
52+
assert dict(policy) == {}
5353

5454
def test___getitem___miss(self):
5555
policy = self._make_one()
@@ -301,10 +301,10 @@ def test_from_api_repr_only_etag(self):
301301
policy = klass.from_api_repr(RESOURCE)
302302
assert policy.etag == "ACAB"
303303
assert policy.version is None
304-
assert dict(policy) == {}
305304
assert policy.owners == empty
306305
assert policy.editors == empty
307306
assert policy.viewers == empty
307+
assert dict(policy) == {}
308308

309309
def test_from_api_repr_complete(self):
310310
from google.api_core.iam import OWNER_ROLE, EDITOR_ROLE, VIEWER_ROLE

0 commit comments

Comments
 (0)