|
14 | 14 |
|
15 | 15 | from dataclasses import dataclass |
16 | 16 | import inspect |
| 17 | +import typing |
17 | 18 | from typing import Callable, Iterable, Union |
18 | 19 |
|
19 | 20 | import google.cloud.bigquery as bigquery |
@@ -70,6 +71,12 @@ def _input_bq_signature(self): |
70 | 71 |
|
71 | 72 | def _output_bq_type(self): |
72 | 73 | sig = inspect.signature(self._func) |
| 74 | + return_annotation = sig.return_annotation |
| 75 | + origin = typing.get_origin(return_annotation) |
| 76 | + if origin is Union: |
| 77 | + args = typing.get_args(return_annotation) |
| 78 | + if len(args) == 2 and args[1] is type(None): |
| 79 | + return _PYTHON_TO_BQ_TYPES[args[0]] |
73 | 80 | return _PYTHON_TO_BQ_TYPES[sig.return_annotation] |
74 | 81 |
|
75 | 82 | def _create_udf(self): |
@@ -177,7 +184,7 @@ def image_blur_func( |
177 | 184 | ksize_y: int, |
178 | 185 | ext: str, |
179 | 186 | verbose: bool, |
180 | | -) -> str: |
| 187 | +) -> typing.Optional[str]: |
181 | 188 | try: |
182 | 189 | import json |
183 | 190 |
|
@@ -241,9 +248,7 @@ def image_blur_func( |
241 | 248 | if verbose: |
242 | 249 | return json.dumps(error_result) |
243 | 250 | else: |
244 | | - # The calling function expects a json string that can be parsed as a blob ref |
245 | | - # Return a valid blob ref json string with empty values. |
246 | | - return '{"access_urls": {"read_url": "", "write_url": ""}, "authorizer": "", "generation": "", "uri": ""}' |
| 251 | + return None |
247 | 252 |
|
248 | 253 |
|
249 | 254 | image_blur_def = FunctionDef(image_blur_func, ["opencv-python", "numpy", "requests"]) |
@@ -316,7 +321,7 @@ def image_resize_func( |
316 | 321 | fy: float, |
317 | 322 | ext: str, |
318 | 323 | verbose: bool, |
319 | | -) -> str: |
| 324 | +) -> typing.Optional[str]: |
320 | 325 | try: |
321 | 326 | import json |
322 | 327 |
|
@@ -379,9 +384,7 @@ def image_resize_func( |
379 | 384 | if verbose: |
380 | 385 | return json.dumps(error_result) |
381 | 386 | else: |
382 | | - # The calling function expects a json string that can be parsed as a blob ref |
383 | | - # Return a valid blob ref json string with empty values. |
384 | | - return '{"access_urls": {"read_url": "", "write_url": ""}, "authorizer": "", "generation": "", "uri": ""}' |
| 387 | + return None |
385 | 388 |
|
386 | 389 |
|
387 | 390 | image_resize_def = FunctionDef( |
@@ -461,7 +464,7 @@ def image_normalize_func( |
461 | 464 | norm_type: str, |
462 | 465 | ext: str, |
463 | 466 | verbose: bool, |
464 | | -) -> str: |
| 467 | +) -> typing.Optional[str]: |
465 | 468 | try: |
466 | 469 | import json |
467 | 470 |
|
@@ -533,9 +536,7 @@ def image_normalize_func( |
533 | 536 | if verbose: |
534 | 537 | return json.dumps(error_result) |
535 | 538 | else: |
536 | | - # The calling function expects a json string that can be parsed as a blob ref |
537 | | - # Return a valid blob ref json string with empty values. |
538 | | - return '{"access_urls": {"read_url": "", "write_url": ""}, "authorizer": "", "generation": "", "uri": ""}' |
| 539 | + return None |
539 | 540 |
|
540 | 541 |
|
541 | 542 | image_normalize_def = FunctionDef( |
|
0 commit comments