Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 57ccabc

Browse files
authoredMay 9, 2024
feat: suggest correct options in bpd.options.bigquery.location (#666)
* feat: suggest correct options in bpd.options.bigquery.location deps: add jellyfish as a dependency for spelling correction
1 parent 306953a commit 57ccabc

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed
 

‎bigframes/_config/bigquery_options.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import google.api_core.exceptions
2323
import google.auth.credentials
24+
import jellyfish
2425

2526
import bigframes.constants
2627
import bigframes.exceptions
@@ -30,7 +31,8 @@
3031
"Call bigframes.pandas.close_session() first, if you are using the bigframes.pandas API."
3132
)
3233

33-
UNKNOWN_LOCATION_MESSAGE = "The location '{location}' is set to an unknown value."
34+
35+
UNKNOWN_LOCATION_MESSAGE = "The location '{location}' is set to an unknown value. Did you mean '{possibility}'?"
3436

3537

3638
def _validate_location(value: Optional[str]):
@@ -39,8 +41,13 @@ def _validate_location(value: Optional[str]):
3941
return
4042

4143
if value not in bigframes.constants.ALL_BIGQUERY_LOCATIONS:
44+
location = str(value)
45+
possibility = min(
46+
bigframes.constants.ALL_BIGQUERY_LOCATIONS,
47+
key=lambda item: jellyfish.levenshtein_distance(location, item),
48+
)
4249
warnings.warn(
43-
UNKNOWN_LOCATION_MESSAGE.format(location=value),
50+
UNKNOWN_LOCATION_MESSAGE.format(location=location, possibility=possibility),
4451
# There are many layers before we get to (possibly) the user's code:
4552
# -> bpd.options.bigquery.location = "us-central-1"
4653
# -> location.setter

‎setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
1516
import io
1617
import itertools
1718
import os
@@ -45,6 +46,7 @@
4546
"google-cloud-resource-manager >=1.10.3",
4647
"google-cloud-storage >=2.0.0",
4748
"ibis-framework[bigquery] >=8.0.0,<9.0.0dev",
49+
"jellyfish >=0.8.9",
4850
# TODO: Relax upper bound once we have fixed `system_prerelease` tests.
4951
"pandas >=1.5.0",
5052
"pyarrow >=8.0.0",

‎testing/constraints-3.9.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ google-cloud-iam==2.12.1
1111
google-cloud-resource-manager==1.10.3
1212
google-cloud-storage==2.0.0
1313
ibis-framework==8.0.0
14+
jellyfish==0.8.9
1415
pandas==1.5.0
1516
pyarrow==8.0.0
1617
pydata-google-auth==1.8.2

‎tests/unit/_config/test_bigquery_options.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,25 @@ def test_location_set_to_valid_no_warning(valid_location):
108108
@pytest.mark.parametrize(
109109
[
110110
"invalid_location",
111+
"possibility",
111112
],
112113
[
113114
# Test with common mistakes, see article.
114115
# https://en.wikipedia.org/wiki/Edit_distance#Formal_definition_and_properties
115116
# Substitution
116-
("us-wist-3",),
117+
("us-wist3", "us-west3"),
117118
# Insertion
118-
("us-central-1",),
119+
("us-central-1", "us-central1"),
119120
# Deletion
120-
("asia-suth2",),
121+
("asia-suth2", "asia-south2"),
121122
],
122123
)
123-
def test_location_set_to_invalid_warning(invalid_location):
124+
def test_location_set_to_invalid_warning(invalid_location, possibility):
124125
options = bigquery_options.BigQueryOptions()
125126
with pytest.warns(
126127
bigframes.exceptions.UnknownLocationWarning,
127128
match=re.escape(
128-
f"The location '{invalid_location}' is set to an unknown value."
129+
f"The location '{invalid_location}' is set to an unknown value. Did you mean '{possibility}'?"
129130
),
130131
):
131132
options.location = invalid_location

0 commit comments

Comments
 (0)
Failed to load comments.