Skip to content

Commit f1a5629

Browse files
authored
chore: format warning message (#1281)
* chore: format warning message * fix unit tests * fix mypy * use regional endpoints warning
1 parent 6903f71 commit f1a5629

25 files changed

+276
-281
lines changed

bigframes/_config/bigquery_options.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import bigframes.constants
2727
import bigframes.enums
28-
import bigframes.exceptions
28+
import bigframes.exceptions as bfe
2929

3030
SESSION_STARTED_MESSAGE = (
3131
"Cannot change '{attribute}' once a session has started. "
@@ -55,15 +55,12 @@ def _get_validated_location(value: Optional[str]) -> Optional[str]:
5555
bigframes.constants.ALL_BIGQUERY_LOCATIONS,
5656
key=lambda item: jellyfish.levenshtein_distance(location, item),
5757
)
58-
warnings.warn(
59-
UNKNOWN_LOCATION_MESSAGE.format(location=location, possibility=possibility),
60-
# There are many layers before we get to (possibly) the user's code:
61-
# -> bpd.options.bigquery.location = "us-central-1"
62-
# -> location.setter
63-
# -> _get_validated_location
64-
stacklevel=3,
65-
category=bigframes.exceptions.UnknownLocationWarning,
66-
)
58+
# There are many layers before we get to (possibly) the user's code:
59+
# -> bpd.options.bigquery.location = "us-central-1"
60+
# -> location.setter
61+
# -> _get_validated_location
62+
msg = UNKNOWN_LOCATION_MESSAGE.format(location=location, possibility=possibility)
63+
warnings.warn(msg, stacklevel=3, category=bfe.UnknownLocationWarning)
6764

6865
return value
6966

@@ -275,10 +272,11 @@ def use_regional_endpoints(self, value: bool):
275272
)
276273

277274
if value:
278-
warnings.warn(
275+
msg = (
279276
"Use of regional endpoints is a feature in preview and "
280277
"available only in selected regions and projects. "
281278
)
279+
warnings.warn(msg, category=bfe.PreviewWarning, stacklevel=2)
282280

283281
self._use_regional_endpoints = value
284282

@@ -334,9 +332,12 @@ def client_endpoints_override(self) -> dict:
334332

335333
@client_endpoints_override.setter
336334
def client_endpoints_override(self, value: dict):
337-
warnings.warn(
338-
"This is an advanced configuration option for directly setting endpoints. Incorrect use may lead to unexpected behavior or system instability. Proceed only if you fully understand its implications."
335+
msg = (
336+
"This is an advanced configuration option for directly setting endpoints. "
337+
"Incorrect use may lead to unexpected behavior or system instability. "
338+
"Proceed only if you fully understand its implications."
339339
)
340+
warnings.warn(msg)
340341

341342
if self._session_started and self._client_endpoints_override != value:
342343
raise ValueError(

bigframes/_config/experiment_options.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import warnings
1616

17+
import bigframes.exceptions as bfe
18+
1719

1820
class ExperimentOptions:
1921
"""
@@ -31,9 +33,11 @@ def semantic_operators(self) -> bool:
3133
@semantic_operators.setter
3234
def semantic_operators(self, value: bool):
3335
if value is True:
34-
warnings.warn(
35-
"Semantic operators are still under experiments, and are subject to change in the future."
36+
msg = (
37+
"Semantic operators are still under experiments, and are subject "
38+
"to change in the future."
3639
)
40+
warnings.warn(msg, category=bfe.PreviewWarning)
3741
self._semantic_operators = value
3842

3943
@property
@@ -43,7 +47,9 @@ def blob(self) -> bool:
4347
@blob.setter
4448
def blob(self, value: bool):
4549
if value is True:
46-
warnings.warn(
47-
"BigFrames Blob is still under experiments. It may not work and subject to change in the future."
50+
msg = (
51+
"BigFrames Blob is still under experiments. It may not work and "
52+
"subject to change in the future."
4853
)
54+
warnings.warn(msg, category=bfe.PreviewWarning)
4955
self._blob = value

bigframes/core/__init__.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import bigframes.core.utils
4040
from bigframes.core.window_spec import WindowSpec
4141
import bigframes.dtypes
42+
import bigframes.exceptions as bfe
4243
import bigframes.operations as ops
4344
import bigframes.operations.aggregations as agg_ops
4445

@@ -106,10 +107,11 @@ def from_table(
106107
if offsets_col and primary_key:
107108
raise ValueError("must set at most one of 'offests', 'primary_key'")
108109
if any(i.field_type == "JSON" for i in table.schema if i.name in schema.names):
109-
warnings.warn(
110-
"Interpreting JSON column(s) as StringDtype and pyarrow.large_string. This behavior may change in future versions.",
111-
bigframes.exceptions.PreviewWarning,
110+
msg = (
111+
"Interpreting JSON column(s) as pyarrow.large_string. "
112+
"This behavior may change in future versions."
112113
)
114+
warnings.warn(msg, bfe.PreviewWarning)
113115
# define data source only for needed columns, this makes row-hashing cheaper
114116
table_def = nodes.GbqTable.from_table(table, columns=schema.names)
115117

@@ -228,10 +230,8 @@ def slice(
228230
self, start: Optional[int], stop: Optional[int], step: Optional[int]
229231
) -> ArrayValue:
230232
if self.node.order_ambiguous and not (self.session._strictly_ordered):
231-
warnings.warn(
232-
"Window ordering may be ambiguous, this can cause unstable results.",
233-
bigframes.exceptions.AmbiguousWindowWarning,
234-
)
233+
msg = "Window ordering may be ambiguous, this can cause unstable results."
234+
warnings.warn(msg, bfe.AmbiguousWindowWarning)
235235
return ArrayValue(
236236
nodes.SliceNode(
237237
self.node,
@@ -252,10 +252,10 @@ def promote_offsets(self) -> Tuple[ArrayValue, str]:
252252
"Generating offsets not supported in partial ordering mode"
253253
)
254254
else:
255-
warnings.warn(
256-
"Window ordering may be ambiguous, this can cause unstable results.",
257-
bigframes.exceptions.AmbiguousWindowWarning,
255+
msg = (
256+
"Window ordering may be ambiguous, this can cause unstable results."
258257
)
258+
warnings.warn(msg, category=bfe.AmbiguousWindowWarning)
259259

260260
return (
261261
ArrayValue(
@@ -391,10 +391,8 @@ def project_window_op(
391391
"Generating offsets not supported in partial ordering mode"
392392
)
393393
else:
394-
warnings.warn(
395-
"Window ordering may be ambiguous, this can cause unstable results.",
396-
bigframes.exceptions.AmbiguousWindowWarning,
397-
)
394+
msg = "Window ordering may be ambiguous, this can cause unstable results."
395+
warnings.warn(msg, category=bfe.AmbiguousWindowWarning)
398396

399397
output_name = self._gen_namespaced_uid()
400398
return (

bigframes/core/blocks.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
import bigframes.core.utils as utils
6464
import bigframes.core.window_spec as windows
6565
import bigframes.dtypes
66-
import bigframes.exceptions
66+
import bigframes.exceptions as bfe
6767
import bigframes.features
6868
import bigframes.operations as ops
6969
import bigframes.operations.aggregations as agg_ops
@@ -137,10 +137,8 @@ def __init__(
137137
)
138138

139139
if len(index_columns) == 0:
140-
warnings.warn(
141-
"Creating object with Null Index. Null Index is a preview feature.",
142-
category=bigframes.exceptions.NullIndexPreviewWarning,
143-
)
140+
msg = "Creating object with Null Index. Null Index is a preview feature."
141+
warnings.warn(msg, category=bfe.NullIndexPreviewWarning)
144142
self._index_columns = tuple(index_columns)
145143
# Index labels don't need complicated hierarchical access so can store as tuple
146144
self._index_labels = (
@@ -616,13 +614,13 @@ def _materialize_local(
616614
" # Setting it to None will download all the data\n"
617615
f"{constants.FEEDBACK_LINK}"
618616
)
619-
620-
warnings.warn(
617+
msg = (
621618
f"The data size ({table_mb:.2f} MB) exceeds the maximum download limit of"
622-
f"({max_download_size} MB). It will be downsampled to {max_download_size} MB for download."
623-
"\nPlease refer to the documentation for configuring the downloading limit.",
624-
UserWarning,
619+
f"({max_download_size} MB). It will be downsampled to {max_download_size} "
620+
"MB for download.\nPlease refer to the documentation for configuring "
621+
"the downloading limit."
625622
)
623+
warnings.warn(msg, category=UserWarning)
626624
total_rows = execute_result.total_rows
627625
# Remove downsampling config from subsequent invocations, as otherwise could result in many
628626
# iterations if downsampling undershoots

bigframes/core/compile/ibis_types.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import pyarrow as pa
3333

3434
import bigframes.dtypes
35+
import bigframes.exceptions as bfe
3536

3637
# Type hints for Ibis data types supported by BigQuery DataFrame
3738
IbisDtype = Union[
@@ -305,10 +306,11 @@ def ibis_dtype_to_bigframes_dtype(
305306

306307
# Temporary: Will eventually support an explicit json type instead of casting to string.
307308
if isinstance(ibis_dtype, ibis_dtypes.JSON):
308-
warnings.warn(
309-
"Interpreting JSON as string. This behavior may change in future versions.",
310-
bigframes.exceptions.PreviewWarning,
309+
msg = (
310+
"Interpreting JSON column(s) as pyarrow.large_string. This behavior may change "
311+
"in future versions."
311312
)
313+
warnings.warn(msg, category=bfe.PreviewWarning)
312314
return bigframes.dtypes.JSON_DTYPE
313315

314316
if ibis_dtype in IBIS_TO_BIGFRAMES:

bigframes/core/compile/polars/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222

2323
__all__ = ["PolarsCompiler"]
2424
except Exception:
25-
warnings.warn("Polars compiler not available as polars is not installed.")
25+
msg = "Polars compiler not available as polars is not installed."
26+
warnings.warn(msg)

bigframes/core/global_session.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import google.auth.exceptions
2323

2424
import bigframes._config
25+
import bigframes.exceptions as bfe
2526
import bigframes.session
2627

2728
_global_session: Optional[bigframes.session.Session] = None
@@ -38,11 +39,11 @@ def _try_close_session(session: bigframes.session.Session):
3839
session_id = session.session_id
3940
location = session._location
4041
project_id = session._project
41-
warnings.warn(
42+
msg = (
4243
f"Session cleanup failed for session with id: {session_id}, "
43-
f"location: {location}, project: {project_id}",
44-
category=bigframes.exceptions.CleanupFailedWarning,
44+
f"location: {location}, project: {project_id}"
4545
)
46+
warnings.warn(msg, category=bfe.CleanupFailedWarning)
4647
traceback.print_tb(e.__traceback__)
4748

4849

bigframes/core/indexers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import bigframes.core.scalar
3030
import bigframes.dataframe
3131
import bigframes.dtypes
32-
import bigframes.exceptions
32+
import bigframes.exceptions as bfe
3333
import bigframes.operations as ops
3434
import bigframes.series
3535

@@ -407,11 +407,12 @@ def _struct_accessor_check_and_warn(
407407
return
408408

409409
if not bigframes.dtypes.is_string_like(series.index.dtype):
410-
warnings.warn(
411-
"Are you trying to access struct fields? If so, please use Series.struct.field(...) method instead.",
412-
category=bigframes.exceptions.BadIndexerKeyWarning,
413-
stacklevel=7, # Stack depth from series.__getitem__ to here
410+
msg = (
411+
"Are you trying to access struct fields? If so, please use Series.struct.field(...) "
412+
"method instead."
414413
)
414+
# Stack depth from series.__getitem__ to here
415+
warnings.warn(msg, stacklevel=7, category=bfe.BadIndexerKeyWarning)
415416

416417

417418
@typing.overload

bigframes/core/utils.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import pandas as pd
2222
import typing_extensions
2323

24-
import bigframes.exceptions as exc
24+
import bigframes.exceptions as bfe
2525

2626
UNNAMED_COLUMN_ID = "bigframes_unnamed_column"
2727
UNNAMED_INDEX_ID = "bigframes_unnamed_index"
@@ -170,11 +170,6 @@ def merge_column_labels(
170170
return pd.Index(result_labels)
171171

172172

173-
def warn_preview(msg=""):
174-
"""Warn a preview API."""
175-
warnings.warn(msg, exc.PreviewWarning)
176-
177-
178173
def preview(*, name: str):
179174
"""Decorate to warn of a preview API."""
180175

@@ -183,7 +178,7 @@ def decorator(func):
183178

184179
@functools.wraps(func)
185180
def wrapper(*args, **kwargs):
186-
warn_preview(msg=msg)
181+
warnings.warn(msg, category=bfe.PreviewWarning)
187182
return func(*args, **kwargs)
188183

189184
return wrapper

bigframes/dataframe.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
import bigframes.core.window
6969
import bigframes.core.window_spec as windows
7070
import bigframes.dtypes
71-
import bigframes.exceptions
71+
import bigframes.exceptions as bfe
7272
import bigframes.formatting_helpers as formatter
7373
import bigframes.operations as ops
7474
import bigframes.operations.aggregations
@@ -1481,10 +1481,8 @@ def to_arrow(
14811481
Returns:
14821482
pyarrow.Table: A pyarrow Table with all rows and columns of this DataFrame.
14831483
"""
1484-
warnings.warn(
1485-
"to_arrow is in preview. Types and unnamed / duplicate name columns may change in future.",
1486-
category=bigframes.exceptions.PreviewWarning,
1487-
)
1484+
msg = "to_arrow is in preview. Types and unnamed / duplicate name columns may change in future."
1485+
warnings.warn(msg, category=bfe.PreviewWarning)
14881486

14891487
pa_table, query_job = self._block.to_arrow(ordered=ordered)
14901488
self._set_internal_query_job(query_job)
@@ -3920,10 +3918,8 @@ def map(self, func, na_action: Optional[str] = None) -> DataFrame:
39203918

39213919
def apply(self, func, *, axis=0, args: typing.Tuple = (), **kwargs):
39223920
if utils.get_axis_number(axis) == 1:
3923-
warnings.warn(
3924-
"axis=1 scenario is in preview.",
3925-
category=bigframes.exceptions.PreviewWarning,
3926-
)
3921+
msg = "axis=1 scenario is in preview."
3922+
warnings.warn(msg, category=bfe.PreviewWarning)
39273923

39283924
# Check if the function is a remote function
39293925
if not hasattr(func, "bigframes_remote_function"):

0 commit comments

Comments
 (0)