Convert arguments from lower level primitives to the expected types.
(
self,
fn: Function,
inputs: Mapping[APIName, Any],
)
| 510 | raise InvalidInputError(msg, extra=extra) from e |
| 511 | |
| 512 | async def _convert_inputs( |
| 513 | self, |
| 514 | fn: Function, |
| 515 | inputs: Mapping[APIName, Any], |
| 516 | ) -> Mapping[PythonName, Any]: |
| 517 | """Convert arguments from lower level primitives to the expected types.""" |
| 518 | kwargs = {} |
| 519 | |
| 520 | # Convert arguments to the expected type. |
| 521 | for python_name, param in fn.parameters.items(): |
| 522 | if param.name not in inputs: |
| 523 | if not param.is_optional: |
| 524 | msg = f"Missing required function argument '{python_name}'" |
| 525 | raise InvalidInputError(msg) |
| 526 | |
| 527 | if param.has_default: |
| 528 | continue |
| 529 | |
| 530 | # If the argument is optional and has no default, it's a nullable type. |
| 531 | # According to GraphQL spec, null is a valid value in case it's omitted. |
| 532 | value = inputs.get(param.name) |
| 533 | type_ = param.resolved_type |
| 534 | |
| 535 | try: |
| 536 | kwargs[python_name] = await self.structure(value, type_) |
| 537 | except Exception as e: |
| 538 | log_exception_only( |
| 539 | e, |
| 540 | "Failed to convert from primitive input value for argument '%s'", |
| 541 | param.name, |
| 542 | ) |
| 543 | msg = transform_error( |
| 544 | e, |
| 545 | ( |
| 546 | "Failed to convert from primitive input value for argument " |
| 547 | f"'{param.name}'" |
| 548 | ), |
| 549 | origin=fn.wrapped, |
| 550 | typ=type_, |
| 551 | ) |
| 552 | # Same as before, the API can't reasonably hold a value that |
| 553 | # contradicts its type. |
| 554 | msg += ( |
| 555 | "\n" |
| 556 | "This could be an error in the Python SDK. " |
| 557 | "If so, please file a bug report." |
| 558 | ) |
| 559 | extra = { |
| 560 | "function_name": fn.original_name, |
| 561 | "parameter_name": python_name, |
| 562 | "expected_type": type_, |
| 563 | "actual_type": type(value), |
| 564 | } |
| 565 | raise InvalidInputError(msg, extra=extra) from e |
| 566 | |
| 567 | if logger.isEnabledFor(logging.DEBUG): |
| 568 | logger.debug("structured args => %s", repr(kwargs)) |
| 569 |
no test coverage detected