From 14fd6a93eae9c8b8657095096c00fafc45d7e18f Mon Sep 17 00:00:00 2001 From: rinarakaki Date: Mon, 21 Oct 2024 10:31:39 +0000 Subject: [PATCH 1/5] add type hints --- google/cloud/bigquery/client.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/google/cloud/bigquery/client.py b/google/cloud/bigquery/client.py index 1c222f2dd..14f392b43 100644 --- a/google/cloud/bigquery/client.py +++ b/google/cloud/bigquery/client.py @@ -65,6 +65,7 @@ DEFAULT_BQSTORAGE_CLIENT_INFO = None # type: ignore +from google.auth.credentials import Credentials from google.cloud.bigquery._http import Connection from google.cloud.bigquery import _job_helpers from google.cloud.bigquery import _pandas_helpers @@ -122,9 +123,7 @@ from google.cloud.bigquery.table import RowIterator pyarrow = _versions_helpers.PYARROW_VERSIONS.try_import() -pandas = ( - _versions_helpers.PANDAS_VERSIONS.try_import() -) # mypy check fails because pandas import is outside module, there are type: ignore comments related to this +pandas = _versions_helpers.PANDAS_VERSIONS.try_import() # mypy check fails because pandas import is outside module, there are type: ignore comments related to this ResumableTimeoutType = Union[ None, float, Tuple[float, float] @@ -231,15 +230,23 @@ class Client(ClientWithProject): def __init__( self, - project=None, - credentials=None, - _http=None, - location=None, - default_query_job_config=None, - default_load_job_config=None, - client_info=None, - client_options=None, + project: Optional[str] = None, + credentials: Optional[Credentials] = None, + _http: Optional[requests.Session] = None, + location: Optional[str] = None, + default_query_job_config: Optional[QueryJobConfig] = None, + default_load_job_config: Optional[LoadJobConfig] = None, + client_info: Optional[google.api_core.client_info.ClientInfo] = None, + client_options: Optional[ + Union[google.api_core.client_options.ClientOptions, Dict[str, Any]] + ] = None, ) -> None: + if client_options is None: + client_options = {} + if isinstance(client_options, dict): + client_options = google.api_core.client_options.from_dict(client_options) + assert isinstance(client_options, google.api_core.client_options.ClientOptions) + super(Client, self).__init__( project=project, credentials=credentials, @@ -251,10 +258,6 @@ def __init__( bq_host = _get_bigquery_host() kw_args["api_endpoint"] = bq_host if bq_host != _DEFAULT_HOST else None client_universe = None - if client_options is None: - client_options = {} - if isinstance(client_options, dict): - client_options = google.api_core.client_options.from_dict(client_options) if client_options.api_endpoint: api_endpoint = client_options.api_endpoint kw_args["api_endpoint"] = api_endpoint From a3c2b7f537493f42fd15502d0fbcbe5b289dc9e4 Mon Sep 17 00:00:00 2001 From: Chalmer Lowe Date: Tue, 10 Dec 2024 07:01:13 -0500 Subject: [PATCH 2/5] Update client.py Moves import from being used solely during specific checks to being more universally available. --- google/cloud/bigquery/client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/google/cloud/bigquery/client.py b/google/cloud/bigquery/client.py index 13af2fb2a..c48be800c 100644 --- a/google/cloud/bigquery/client.py +++ b/google/cloud/bigquery/client.py @@ -44,6 +44,8 @@ import uuid import warnings +import requests + from google import resumable_media # type: ignore from google.resumable_media.requests import MultipartUpload # type: ignore from google.resumable_media.requests import ResumableUpload @@ -132,8 +134,7 @@ if typing.TYPE_CHECKING: # pragma: NO COVER # os.PathLike is only subscriptable in Python 3.9+, thus shielding with a condition. PathType = Union[str, bytes, os.PathLike[str], os.PathLike[bytes]] - import requests # required by api-core - + _DEFAULT_CHUNKSIZE = 100 * 1024 * 1024 # 100 MB _MAX_MULTIPART_SIZE = 5 * 1024 * 1024 _DEFAULT_NUM_RETRIES = 6 From a71e1936395bffc858ff331d00ab6c77b22ba780 Mon Sep 17 00:00:00 2001 From: Chalmer Lowe Date: Tue, 10 Dec 2024 09:14:04 -0500 Subject: [PATCH 3/5] Update google/cloud/bigquery/client.py --- google/cloud/bigquery/client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/google/cloud/bigquery/client.py b/google/cloud/bigquery/client.py index c48be800c..6ae858125 100644 --- a/google/cloud/bigquery/client.py +++ b/google/cloud/bigquery/client.py @@ -134,7 +134,6 @@ if typing.TYPE_CHECKING: # pragma: NO COVER # os.PathLike is only subscriptable in Python 3.9+, thus shielding with a condition. PathType = Union[str, bytes, os.PathLike[str], os.PathLike[bytes]] - _DEFAULT_CHUNKSIZE = 100 * 1024 * 1024 # 100 MB _MAX_MULTIPART_SIZE = 5 * 1024 * 1024 _DEFAULT_NUM_RETRIES = 6 From 701ef135752ec672aedf2139199ba6c313735a54 Mon Sep 17 00:00:00 2001 From: Chalmer Lowe Date: Tue, 10 Dec 2024 10:26:40 -0500 Subject: [PATCH 4/5] Update client.py testing some minor changes to deal with mypy quirks --- google/cloud/bigquery/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google/cloud/bigquery/client.py b/google/cloud/bigquery/client.py index 6ae858125..f1241bdad 100644 --- a/google/cloud/bigquery/client.py +++ b/google/cloud/bigquery/client.py @@ -245,7 +245,7 @@ def __init__( client_options = {} if isinstance(client_options, dict): client_options = google.api_core.client_options.from_dict(client_options) - assert isinstance(client_options, google.api_core.client_options.ClientOptions) + # assert isinstance(client_options, google.api_core.client_options.ClientOptions) super(Client, self).__init__( project=project, @@ -254,7 +254,7 @@ def __init__( _http=_http, ) - kw_args = {"client_info": client_info} + kw_args: Dict[str, Any] = {"client_info": client_info} bq_host = _get_bigquery_host() kw_args["api_endpoint"] = bq_host if bq_host != _DEFAULT_HOST else None client_universe = None From 0f8af52aed8280767c9eec2adc05088278eb16f0 Mon Sep 17 00:00:00 2001 From: Chalmer Lowe Date: Tue, 10 Dec 2024 11:54:44 -0500 Subject: [PATCH 5/5] Update google/cloud/bigquery/client.py --- google/cloud/bigquery/client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/google/cloud/bigquery/client.py b/google/cloud/bigquery/client.py index f1241bdad..03ded93b1 100644 --- a/google/cloud/bigquery/client.py +++ b/google/cloud/bigquery/client.py @@ -125,7 +125,10 @@ from google.cloud.bigquery.table import RowIterator pyarrow = _versions_helpers.PYARROW_VERSIONS.try_import() -pandas = _versions_helpers.PANDAS_VERSIONS.try_import() # mypy check fails because pandas import is outside module, there are type: ignore comments related to this +pandas = ( + _versions_helpers.PANDAS_VERSIONS.try_import() +) # mypy check fails because pandas import is outside module, there are type: ignore comments related to this + ResumableTimeoutType = Union[ None, float, Tuple[float, float]