-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Environment
OS: macOS 15.7.2
Python: 3.10.18
google-cloud-bigquery: 3.4.2
google-cloud-bigquery-storage: 2.35.0
Description
When using BigQueryReadClient as a context manager and passing it to RowIterator.to_dataframe(), type checkers report an incompatible type error even though the code works correctly at runtime.
Steps to Reproduce
from google.cloud import bigquery, bigquery_storage
client = bigquery.Client()
result = client.query("SELECT 1").result()
with bigquery_storage.BigQueryReadClient() as storage_client:
df = result.to_dataframe(bqstorage_client=storage_client)
Type Error
Argument "bqstorage_client" to "to_dataframe" of "RowIterator" has incompatible type "google.cloud.bigquery_storage_v1.services.big_query_read.client.BigQueryReadClient"; expected "google.cloud.bigquery_storage_v1.BigQueryReadClient | None"
Root Cause
The enter method's return type in the type stubs references the internal class path:
Annotated return type: google.cloud.bigquery_storage_v1.services.big_query_read.client.BigQueryReadClient
Expected by to_dataframe(): google.cloud.bigquery_storage_v1.BigQueryReadClient
At runtime, both resolve to the same class and enter correctly returns self. The issue is purely in the type annotations.
Verification
from google.cloud import bigquery_storage
from google.cloud.bigquery_storage_v1.services.big_query_read.client import BigQueryReadClient as InternalClient
from google.cloud.bigquery_storage_v1 import BigQueryReadClient as V1Client
print(bigquery_storage.BigQueryReadClient is V1Client) # True
print(V1Client is InternalClient) # False - different classes in type system
Expected Behavior
No type error when using BigQueryReadClient as a context manager and passing it to to_dataframe().
Workaround
Adding # type: ignore[arg-type] to suppress the false positive.