Skip to content

Commit c3247d5

Browse files
feat: Improve UnsupportedTypeError message
The `UnsupportedTypeError` now provides a more user-friendly error message by displaying a sorted, comma-separated list of supported type names instead of the raw object representations. This improves clarity and makes it easier for developers to identify the correct types to use. Added a new test file, `tests/unit/functions/test_function_typing.py`, with unit tests to ensure the error message is formatted correctly for different types of collections (dictionaries and sets) and that the exception is raised with the expected message.
1 parent 4c98c95 commit c3247d5

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

bigframes/functions/function_typing.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ class UnsupportedTypeError(ValueError):
6060
def __init__(self, type_, supported_types):
6161
self.type = type_
6262
self.supported_types = supported_types
63+
64+
types_to_format = supported_types
65+
if isinstance(supported_types, dict):
66+
types_to_format = supported_types.keys()
67+
68+
supported_types_str = ", ".join(sorted([t.__name__ for t in types_to_format]))
69+
6370
super().__init__(
64-
f"'{type_}' must be one of the supported types ({supported_types}) "
71+
f"'{type_.__name__}' must be one of the supported types ({supported_types_str}) "
6572
"or a list of one of those types."
6673
)
6774

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2024 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+
import datetime
16+
import decimal
17+
18+
import pytest
19+
20+
from bigframes.functions import function_typing
21+
22+
23+
def test_unsupported_type_error_init_with_dict():
24+
err = function_typing.UnsupportedTypeError(
25+
decimal.Decimal, {int: "INT64", float: "FLOAT64"}
26+
)
27+
assert "Decimal" in str(err)
28+
assert "float, int" in str(err)
29+
30+
31+
def test_unsupported_type_error_init_with_set():
32+
err = function_typing.UnsupportedTypeError(decimal.Decimal, {int, float})
33+
assert "Decimal" in str(err)
34+
assert "float, int" in str(err)
35+
36+
37+
def test_sdk_type_from_python_type_raises_unsupported_type_error():
38+
with pytest.raises(function_typing.UnsupportedTypeError) as excinfo:
39+
function_typing.sdk_type_from_python_type(datetime.datetime)
40+
assert "datetime" in str(excinfo.value)
41+
assert "bool, bytes, float, int, str" in str(excinfo.value)

0 commit comments

Comments
 (0)