Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit e6f5b76

Browse files
committed
refactor: simplify warning suppression by relying on global filter
1 parent f48b69b commit e6f5b76

File tree

2 files changed

+40
-47
lines changed

2 files changed

+40
-47
lines changed

bigframes/_magics.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import warnings
16-
1715
from IPython.core import magic_arguments # type: ignore
1816
from IPython.core.getipython import get_ipython
1917
from IPython.display import display
@@ -44,11 +42,9 @@ def _cell_magic(line, cell):
4442
print("Query is missing.")
4543
return
4644
pyformat_args = ipython.user_ns
47-
with warnings.catch_warnings():
48-
warnings.simplefilter("ignore", category=FutureWarning)
49-
dataframe = bigframes.pandas._read_gbq_colab(
50-
cell, pyformat_args=pyformat_args, dry_run=args.dry_run
51-
)
45+
dataframe = bigframes.pandas._read_gbq_colab(
46+
cell, pyformat_args=pyformat_args, dry_run=args.dry_run
47+
)
5248
if args.destination_var:
5349
ipython.push({args.destination_var: dataframe})
5450

bigframes/pandas/io/api.py

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -328,50 +328,47 @@ def _read_gbq_colab(
328328
if pyformat_args is None:
329329
pyformat_args = {}
330330

331-
with warnings.catch_warnings():
332-
warnings.simplefilter("ignore", category=FutureWarning)
333-
334-
# Only try to set the global location if it's not a dry run. We don't want
335-
# to bind to a location too early. This is especially important if the query
336-
# only refers to local data and not any BigQuery tables.
337-
if dry_run:
338-
result = _try_read_gbq_colab_sessionless_dry_run(
339-
query_or_table, pyformat_args=pyformat_args
340-
)
341-
342-
if result is not None:
343-
return result
331+
# Only try to set the global location if it's not a dry run. We don't want
332+
# to bind to a location too early. This is especially important if the query
333+
# only refers to local data and not any BigQuery tables.
334+
if dry_run:
335+
result = _try_read_gbq_colab_sessionless_dry_run(
336+
query_or_table, pyformat_args=pyformat_args
337+
)
344338

345-
# If we made it this far, we must have a session that has already
346-
# started. That means we can safely call the "real" _read_gbq_colab,
347-
# which generates slightly nicer SQL.
348-
else:
349-
# Delay formatting the query with the special "session-less" logic. This
350-
# avoids doing unnecessary work if the session already has a location or has
351-
# already started.
352-
create_query = functools.partial(
353-
bigframes.core.pyformat.pyformat,
354-
query_or_table,
355-
pyformat_args=pyformat_args,
356-
dry_run=True,
357-
)
358-
_set_default_session_location_if_possible_deferred_query(create_query)
359-
if not config.options.bigquery._session_started:
360-
# Don't warning about Polars in SQL cell.
361-
# Related to b/437090788.
362-
try:
363-
bigframes._importing.import_polars()
364-
warnings.simplefilter("ignore", bigframes.exceptions.PreviewWarning)
365-
config.options.bigquery.enable_polars_execution = True
366-
except ImportError:
367-
pass # don't fail if polars isn't available
368-
369-
return global_session.with_default_session(
370-
bigframes.session.Session._read_gbq_colab,
339+
if result is not None:
340+
return result
341+
342+
# If we made it this far, we must have a session that has already
343+
# started. That means we can safely call the "real" _read_gbq_colab,
344+
# which generates slightly nicer SQL.
345+
else:
346+
# Delay formatting the query with the special "session-less" logic. This
347+
# avoids doing unnecessary work if the session already has a location or has
348+
# already started.
349+
create_query = functools.partial(
350+
bigframes.core.pyformat.pyformat,
371351
query_or_table,
372352
pyformat_args=pyformat_args,
373-
dry_run=dry_run,
353+
dry_run=True,
374354
)
355+
_set_default_session_location_if_possible_deferred_query(create_query)
356+
if not config.options.bigquery._session_started:
357+
# Don't warning about Polars in SQL cell.
358+
# Related to b/437090788.
359+
try:
360+
bigframes._importing.import_polars()
361+
warnings.simplefilter("ignore", bigframes.exceptions.PreviewWarning)
362+
config.options.bigquery.enable_polars_execution = True
363+
except ImportError:
364+
pass # don't fail if polars isn't available
365+
366+
return global_session.with_default_session(
367+
bigframes.session.Session._read_gbq_colab,
368+
query_or_table,
369+
pyformat_args=pyformat_args,
370+
dry_run=dry_run,
371+
)
375372

376373

377374
def read_gbq_model(model_name: str):

0 commit comments

Comments
 (0)