FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: skip empty policy bindings in `len()` and `iter()` by busunkim96 · Pull Request #159 · googleapis/python-api-core · GitHub

This repository was archived by the owner on Feb 23, 2026. It is now read-only.
/ python-api-core Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 7 additions & 3 deletions google/api_core/iam.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,22 @@ def __init__(self, etag=None, version=None):

def __iter__(self):
self.__check_version__()
return (binding["role"] for binding in self._bindings)
# Exclude bindings with no members
return (binding["role"] for binding in self._bindings if binding["members"])

def __len__(self):
self.__check_version__()
return len(self._bindings)
# Exclude bindings with no members
return len(list(self.__iter__()))

def __getitem__(self, key):
self.__check_version__()
for b in self._bindings:
if b["role"] == key:
return b["members"]
# binding does not yet exist, create one
# If the binding does not yet exist, create one
# NOTE: This will create bindings with no members
# which are ignored by __iter__ and __len__
new_binding = {"role": key, "members": set()}
self._bindings.append(new_binding)
return new_binding["members"]
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_iam.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def test_ctor_defaults(self):
policy = self._make_one()
assert policy.etag is None
assert policy.version is None
assert len(policy) == 0
assert dict(policy) == {}
assert policy.owners == empty
assert policy.editors == empty
assert policy.viewers == empty
assert len(policy) == 0
assert dict(policy) == {}

def test_ctor_explicit(self):
VERSION = 1
Expand All @@ -45,11 +45,11 @@ def test_ctor_explicit(self):
policy = self._make_one(ETAG, VERSION)
assert policy.etag == ETAG
assert policy.version == VERSION
assert len(policy) == 0
assert dict(policy) == {}
assert policy.owners == empty
assert policy.editors == empty
assert policy.viewers == empty
assert len(policy) == 0
assert dict(policy) == {}

def test___getitem___miss(self):
policy = self._make_one()
Expand Down Expand Up @@ -301,10 +301,10 @@ def test_from_api_repr_only_etag(self):
policy = klass.from_api_repr(RESOURCE)
assert policy.etag == "ACAB"
assert policy.version is None
assert dict(policy) == {}
assert policy.owners == empty
assert policy.editors == empty
assert policy.viewers == empty
assert dict(policy) == {}

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

Back | FazBrowse Home | New Git URL