Repository navigation
fix(bigtable): normalize scopes in _init_accelerator_config and _norm… #19031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: bigtable-accel
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,10 +174,18 @@ def _normalize_scopes(scopes: Any) -> frozenset[str]: | |
| if not scopes: | ||
| return frozenset() | ||
| if isinstance(scopes, str): | ||
| scopes = scopes.replace(",", " ").split() | ||
| elif not isinstance(scopes, (list, tuple, set, frozenset)): | ||
| return frozenset(scopes.replace(",", " ").split()) | ||
| if not isinstance(scopes, (list, tuple, set, frozenset)): | ||
| return frozenset() | ||
| return frozenset((s.strip() for s in scopes if isinstance(s, str) and s.strip())) | ||
| return frozenset( | ||
| ( | ||
| part.strip() | ||
| for s in scopes | ||
| if isinstance(s, str) | ||
| for part in s.split(",") | ||
| if part.strip() | ||
| ) | ||
| ) | ||
|
Comment on lines
+180
to
+188
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can simplify the generator expression by replacing commas with spaces and using return frozenset(
part
for s in scopes
if isinstance(s, str)
for part in s.replace(",", " ").split()
) |
||
|
|
||
|
|
||
| def _credential_type(creds: Any) -> str | None: | ||
|
|
@@ -379,7 +387,12 @@ def _init_accelerator_config( | |
| f"python-v{google.cloud.bigtable.__version__}", | ||
| ] | ||
| scopes = getattr(client_options, "scopes", None) if client_options else None | ||
| effective_scopes = list(scopes) if scopes else list(TransportType.AUTH_SCOPES) | ||
| normalized_scopes = _normalize_scopes(scopes) | ||
| effective_scopes = ( | ||
| sorted(normalized_scopes) | ||
| if normalized_scopes | ||
| else list(TransportType.AUTH_SCOPES) | ||
| ) | ||
| self._accelerator_scopes: list[str] = effective_scopes | ||
| if effective_scopes: | ||
| self._accelerator_flags += ["--scopes", ",".join(effective_scopes)] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify the generator expression by replacing commas with spaces and using
split()without arguments. This aligns the list-element parsing behavior with the string parsing behavior (which also splits on whitespace and commas), and leverages Python's optimizedsplit()behavior to automatically strip whitespace and filter out empty strings, making the code cleaner and more robust.