Skip to content

Commit 46c1173

Browse files
fix: Handle generic types in UnsupportedTypeError
This commit fixes a test failure caused by an `AttributeError` when handling generic types from the `typing` module in the `UnsupportedTypeError` class. The `__init__` method of `UnsupportedTypeError` has been updated to check if a type is a generic from the `typing` module and, if so, convert it to a string directly to get the full type representation (e.g., `list[str]`). This ensures that the error message is generated correctly without raising an `AttributeError`. A new unit test has also been added to `tests/unit/functions/test_function_typing.py` to verify the fix.
1 parent b23cb64 commit 46c1173

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

bigframes/functions/function_typing.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ def __init__(self, type_, supported_types):
6565
if isinstance(supported_types, dict):
6666
types_to_format = supported_types.keys()
6767

68-
supported_types_str = ", ".join(sorted([t.__name__ for t in types_to_format]))
68+
supported_types_str = ", ".join(
69+
sorted([getattr(t, "__name__", str(t)) for t in types_to_format])
70+
)
71+
if get_origin(type_) is not None:
72+
type_str = str(type_)
73+
else:
74+
type_str = getattr(type_, "__name__", str(type_))
6975

7076
super().__init__(
71-
f"'{type_.__name__}' must be one of the supported types ({supported_types_str}) "
77+
f"'{type_str}' must be one of the supported types ({supported_types_str}) "
7278
"or a list of one of those types."
7379
)
7480

tests/unit/functions/test_function_typing.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,14 @@ def test_sdk_type_from_python_type_raises_unsupported_type_error():
5555
function_typing.sdk_type_from_python_type(unsupported_type)
5656
assert "datetime" in str(excinfo.value)
5757
assert "bool, bytes, float, int, str" in str(excinfo.value)
58+
59+
60+
def test_sdk_type_from_python_type_with_generic_type_raises_unsupported_type_error():
61+
# Arrange
62+
unsupported_type = list[str]
63+
64+
# Act & Assert
65+
with pytest.raises(function_typing.UnsupportedTypeError) as excinfo:
66+
function_typing.sdk_type_from_python_type(unsupported_type)
67+
assert "list[str]" in str(excinfo.value)
68+
assert "bool, bytes, float, int, str" in str(excinfo.value)

0 commit comments

Comments
 (0)