-
Notifications
You must be signed in to change notification settings - Fork 167
feat(storage): add abstract BaseClient for Python storage client #1656
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
Conversation
Summary of ChangesHello @googlyrahman, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a foundational architectural change to the Python storage client by creating an abstract Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
The pull request introduces a new abstract base class, BaseClient, in google/cloud/storage/abstracts/base_client.py to centralize client initialization logic, including project handling, API endpoint determination, credential management, and universe domain validation. The existing Client class in google/cloud/storage/client.py is refactored to inherit from BaseClient, significantly simplifying its __init__ method by delegating these responsibilities to the superclass. Review comments suggest improving the BaseClient's __init__ method by changing a mutable default argument for extra_headers to None, simplifying a boolean assignment for _is_emulator_set, and reordering imports according to PEP 8. Additionally, a comment in the Client class regarding connection_kw_args is recommended for removal.
| client_info=None, | ||
| client_options=None, | ||
| use_auth_w_custom_endpoint=True, | ||
| extra_headers={}, |
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.
Using a mutable default argument like {} can lead to unexpected behavior. If this dictionary is modified, the change will persist across all subsequent calls to this function that don't specify extra_headers. It's safer to use None as the default and initialize an empty dictionary inside the function, for example by adding if extra_headers is None: extra_headers = {} at the beginning of the __init__ method.
| extra_headers={}, | |
| extra_headers=None, |
| from google.cloud.storage._helpers import _get_api_endpoint_override | ||
| from google.cloud.storage._helpers import _get_environ_project | ||
| from google.cloud.storage._helpers import _get_storage_emulator_override | ||
| from google.cloud.storage._helpers import _DEFAULT_SCHEME | ||
| from google.cloud.storage._helpers import _STORAGE_HOST_TEMPLATE | ||
| from google.auth.credentials import AnonymousCredentials | ||
| from google.cloud.client import ClientWithProject | ||
| from google.cloud._helpers import _LocalStack | ||
| from google.auth.transport import mtls | ||
| from abc import ABC | ||
|
|
||
| import os | ||
| import google.api_core |
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.
The imports are not ordered according to PEP 8 guidelines. They should be grouped into standard library, third-party, and local application imports, with each group sorted alphabetically.
| from google.cloud.storage._helpers import _get_api_endpoint_override | |
| from google.cloud.storage._helpers import _get_environ_project | |
| from google.cloud.storage._helpers import _get_storage_emulator_override | |
| from google.cloud.storage._helpers import _DEFAULT_SCHEME | |
| from google.cloud.storage._helpers import _STORAGE_HOST_TEMPLATE | |
| from google.auth.credentials import AnonymousCredentials | |
| from google.cloud.client import ClientWithProject | |
| from google.cloud._helpers import _LocalStack | |
| from google.auth.transport import mtls | |
| from abc import ABC | |
| import os | |
| import google.api_core | |
| from abc import ABC | |
| import os | |
| import google.api_core | |
| from google.auth.credentials import AnonymousCredentials | |
| from google.auth.transport import mtls | |
| from google.cloud._helpers import _LocalStack | |
| from google.cloud.client import ClientWithProject | |
| from google.cloud.storage._helpers import _DEFAULT_SCHEME | |
| from google.cloud.storage._helpers import _get_api_endpoint_override | |
| from google.cloud.storage._helpers import _get_environ_project | |
| from google.cloud.storage._helpers import _get_storage_emulator_override | |
| from google.cloud.storage._helpers import _STORAGE_HOST_TEMPLATE |
| api_endpoint = None | ||
|
|
||
| connection_kw_args["api_endpoint"] = api_endpoint | ||
| self._is_emulator_set = True if storage_emulator_override else False |
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.
|
|
||
| # Pass extra_headers to Connection | ||
| connection = Connection(self, **connection_kw_args) | ||
| connection = Connection(self, **self.connection_kw_args) # connection_kw_args would always be set in base class |
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.
This comment seems to be a note for the developer and could be removed to keep the code cleaner. The fact that self.connection_kw_args is set in the base class should be clear from the class structure.
| connection = Connection(self, **self.connection_kw_args) # connection_kw_args would always be set in base class | |
| connection = Connection(self, **self.connection_kw_args) |
This PR introduces an abstract class to support the upcoming async client for the Python SDK. This refactor defines the public interface and enables code sharing between sync and async client, without introducing new logic.