Skip to content

Commit

Permalink
feat: add type hints to Client (#2044)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
rinarakaki and chalmerlowe authored Dec 10, 2024
1 parent 729322c commit 40529de
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -65,6 +67,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
Expand Down Expand Up @@ -126,15 +129,14 @@
_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]
] # for resumable media methods

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
Expand Down Expand Up @@ -231,30 +233,34 @@ 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,
client_options=client_options,
_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
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
Expand Down

0 comments on commit 40529de

Please sign in to comment.