Skip to content

Commit 3706b4f

Browse files
DevStephanieshobsitswast
authored
feat: warn if location is set to unknown location (#609)
* feat: warn if location is set to unknown location * tests error message --------- Co-authored-by: Shobhit Singh <[email protected]> Co-authored-by: Tim Sweña (Swast) <[email protected]>
1 parent 9665e39 commit 3706b4f

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

bigframes/_config/bigquery_options.py

+23
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,33 @@
2222
import google.api_core.exceptions
2323
import google.auth.credentials
2424

25+
import bigframes.constants
26+
import bigframes.exceptions
27+
2528
SESSION_STARTED_MESSAGE = (
2629
"Cannot change '{attribute}' once a session has started. "
2730
"Call bigframes.pandas.close_session() first, if you are using the bigframes.pandas API."
2831
)
2932

33+
UNKNOWN_LOCATION_MESSAGE = "The location '{location}' is set to an unknown value."
34+
35+
36+
def _validate_location(value: Optional[str]):
37+
38+
if value is None:
39+
return
40+
41+
if value not in bigframes.constants.ALL_BIGQUERY_LOCATIONS:
42+
warnings.warn(
43+
UNKNOWN_LOCATION_MESSAGE.format(location=value),
44+
# There are many layers before we get to (possibly) the user's code:
45+
# -> bpd.options.bigquery.location = "us-central-1"
46+
# -> location.setter
47+
# -> _validate_location
48+
stacklevel=3,
49+
category=bigframes.exceptions.UnknownLocationWarning,
50+
)
51+
3052

3153
class BigQueryOptions:
3254
"""Encapsulates configuration for working with a session."""
@@ -93,6 +115,7 @@ def location(self) -> Optional[str]:
93115
def location(self, value: Optional[str]):
94116
if self._session_started and self._location != value:
95117
raise ValueError(SESSION_STARTED_MESSAGE.format(attribute="location"))
118+
_validate_location(value)
96119
self._location = value
97120

98121
@property

bigframes/exceptions.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
class UnknownLocationWarning(Warning):
17+
"""The location is set to an unknown value."""

tests/unit/_config/test_bigquery_options.py

+51
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
# limitations under the License.
1414

1515
import re
16+
import warnings
1617

1718
import pytest
1819

20+
import bigframes
1921
import bigframes._config.bigquery_options as bigquery_options
22+
import bigframes.exceptions
2023

2124

2225
@pytest.mark.parametrize(
@@ -78,3 +81,51 @@ def test_setter_if_session_started_but_setting_the_same_value(attribute):
7881
setattr(options, attribute, original_object)
7982

8083
assert getattr(options, attribute) is original_object
84+
85+
86+
@pytest.mark.parametrize(
87+
[
88+
"valid_location",
89+
],
90+
[
91+
(None,),
92+
("us-central1",),
93+
],
94+
)
95+
def test_location_set_to_valid_no_warning(valid_location):
96+
options = bigquery_options.BigQueryOptions()
97+
# Ensure that no warnings are emitted.
98+
# https://docs.pytest.org/en/7.0.x/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests
99+
with warnings.catch_warnings():
100+
# Turn matching UnknownLocationWarning into exceptions.
101+
# https://docs.python.org/3/library/warnings.html#warning-filter
102+
warnings.simplefilter(
103+
"error", category=bigframes.exceptions.UnknownLocationWarning
104+
)
105+
options.location = valid_location
106+
107+
108+
@pytest.mark.parametrize(
109+
[
110+
"invalid_location",
111+
],
112+
[
113+
# Test with common mistakes, see article.
114+
# https://en.wikipedia.org/wiki/Edit_distance#Formal_definition_and_properties
115+
# Substitution
116+
("us-wist-3",),
117+
# Insertion
118+
("us-central-1",),
119+
# Deletion
120+
("asia-suth2",),
121+
],
122+
)
123+
def test_location_set_to_invalid_warning(invalid_location):
124+
options = bigquery_options.BigQueryOptions()
125+
with pytest.warns(
126+
bigframes.exceptions.UnknownLocationWarning,
127+
match=re.escape(
128+
f"The location '{invalid_location}' is set to an unknown value."
129+
),
130+
):
131+
options.location = invalid_location

0 commit comments

Comments
 (0)