Repository navigation
fix(pandas-gbq): reuse bigquery_client in GbqConnector.get_client and support client_options - #19295
chalmerlowe wants to merge 1 commit into
Conversation
… support client_options
There was a problem hiding this comment.
Code Review
This pull request introduces support for client_options in read_gbq and to_gbq by forwarding the parameter to the underlying GbqConnector and BigQuery client. It also adds comprehensive unit tests to verify the forwarding of client options and the reuse of supplied BigQuery clients. The feedback suggests simplifying the get_client method in gbq_connector.py by replacing defensive getattr checks with direct attribute access, as these attributes are guaranteed to be initialized in __init__.
| if getattr(self, "client", None) is not None: | ||
| return self.client | ||
| return _get_client( | ||
| self.user_agent, | ||
| self.rfc9110_delimiter, | ||
| self.project_id, | ||
| self.credentials, | ||
| client_options=getattr(self, "client_options", None), |
There was a problem hiding this comment.
Using getattr(self, ...) for client and client_options is unnecessary and inconsistent with the direct attribute access used for self.user_agent, self.rfc9110_delimiter, self.project_id, and self.credentials in the same method. Since both self.client and self.client_options are guaranteed to be initialized in __init__, they should be accessed directly.
| if getattr(self, "client", None) is not None: | |
| return self.client | |
| return _get_client( | |
| self.user_agent, | |
| self.rfc9110_delimiter, | |
| self.project_id, | |
| self.credentials, | |
| client_options=getattr(self, "client_options", None), | |
| if self.client is not None: | |
| return self.client | |
| return _get_client( | |
| self.user_agent, | |
| self.rfc9110_delimiter, | |
| self.project_id, | |
| self.credentials, | |
| client_options=self.client_options, | |
| ) |
References
- Do not use defensive
getattr(self, 'attribute', None)checks for attributes that are guaranteed to be initialized in the class's__init__method, as it adds unnecessary complexity.
Summary
Fixes
GbqConnector.get_client()dropping caller-suppliedbigquery_clientconfiguration (client_options,universe_domain,_http) and exposesclient_optionsacrosspandas_gbq.read_gbq(),pandas_gbq.to_gbq(),GbqConnector, and_get_client():pandas_gbq/gbq_connector.py:GbqConnector.get_client()now reusesself.clientwhen already constructed (including caller-suppliedbigquery_client) and forwardsclient_optionsin the fallback path and in_get_client().pandas_gbq/gbq.py: Accepts optionalclient_optionsinread_gbq()andto_gbq()and forwards it toGbqConnector.tests/unit/test_universe_domain.py: Adds unit tests coveringbigquery_clientreuse inGbqConnector.get_client()andclient_optionsforwarding (ClientOptions,dict,MappingProxyType) acrossGbqConnector,read_gbq, andto_gbq.Internal tracking:
b/589828817