Skip to content

Commit 40529de

Browse files
feat: add type hints to Client (#2044)
* add type hints * Update client.py Moves import from being used solely during specific checks to being more universally available. * Update google/cloud/bigquery/client.py * Update client.py testing some minor changes to deal with mypy quirks * Update google/cloud/bigquery/client.py --------- Co-authored-by: Chalmer Lowe <[email protected]>
1 parent 729322c commit 40529de

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

google/cloud/bigquery/client.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import uuid
4545
import warnings
4646

47+
import requests
48+
4749
from google import resumable_media # type: ignore
4850
from google.resumable_media.requests import MultipartUpload # type: ignore
4951
from google.resumable_media.requests import ResumableUpload
@@ -65,6 +67,7 @@
6567
DEFAULT_BQSTORAGE_CLIENT_INFO = None # type: ignore
6668

6769

70+
from google.auth.credentials import Credentials
6871
from google.cloud.bigquery._http import Connection
6972
from google.cloud.bigquery import _job_helpers
7073
from google.cloud.bigquery import _pandas_helpers
@@ -126,15 +129,14 @@
126129
_versions_helpers.PANDAS_VERSIONS.try_import()
127130
) # mypy check fails because pandas import is outside module, there are type: ignore comments related to this
128131

132+
129133
ResumableTimeoutType = Union[
130134
None, float, Tuple[float, float]
131135
] # for resumable media methods
132136

133137
if typing.TYPE_CHECKING: # pragma: NO COVER
134138
# os.PathLike is only subscriptable in Python 3.9+, thus shielding with a condition.
135139
PathType = Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]
136-
import requests # required by api-core
137-
138140
_DEFAULT_CHUNKSIZE = 100 * 1024 * 1024 # 100 MB
139141
_MAX_MULTIPART_SIZE = 5 * 1024 * 1024
140142
_DEFAULT_NUM_RETRIES = 6
@@ -231,30 +233,34 @@ class Client(ClientWithProject):
231233

232234
def __init__(
233235
self,
234-
project=None,
235-
credentials=None,
236-
_http=None,
237-
location=None,
238-
default_query_job_config=None,
239-
default_load_job_config=None,
240-
client_info=None,
241-
client_options=None,
236+
project: Optional[str] = None,
237+
credentials: Optional[Credentials] = None,
238+
_http: Optional[requests.Session] = None,
239+
location: Optional[str] = None,
240+
default_query_job_config: Optional[QueryJobConfig] = None,
241+
default_load_job_config: Optional[LoadJobConfig] = None,
242+
client_info: Optional[google.api_core.client_info.ClientInfo] = None,
243+
client_options: Optional[
244+
Union[google.api_core.client_options.ClientOptions, Dict[str, Any]]
245+
] = None,
242246
) -> None:
247+
if client_options is None:
248+
client_options = {}
249+
if isinstance(client_options, dict):
250+
client_options = google.api_core.client_options.from_dict(client_options)
251+
# assert isinstance(client_options, google.api_core.client_options.ClientOptions)
252+
243253
super(Client, self).__init__(
244254
project=project,
245255
credentials=credentials,
246256
client_options=client_options,
247257
_http=_http,
248258
)
249259

250-
kw_args = {"client_info": client_info}
260+
kw_args: Dict[str, Any] = {"client_info": client_info}
251261
bq_host = _get_bigquery_host()
252262
kw_args["api_endpoint"] = bq_host if bq_host != _DEFAULT_HOST else None
253263
client_universe = None
254-
if client_options is None:
255-
client_options = {}
256-
if isinstance(client_options, dict):
257-
client_options = google.api_core.client_options.from_dict(client_options)
258264
if client_options.api_endpoint:
259265
api_endpoint = client_options.api_endpoint
260266
kw_args["api_endpoint"] = api_endpoint

0 commit comments

Comments
 (0)