[sdk/python] Fix error on empty invoke returns (#14470)
When an empty struct is returned from an invoke, the Python SDK was
returning None, which causes the following error to be raised from
generated provider Python SDKs when calling the invoke:
```
AssertionError: get can only be used with classes decorated with @input_type or @output_type
```
This is because the Python SDK is returning None if the return value
isn't truthy, roughly:
```
ret_obj = getattr(resp, "return")
if ret_obj:
return ret_obj
return None
```
However, an empty struct isn't truthy, so we're returning None in that
case, and generated provider Python SDKs can't handle that.
Other SDKs like the Node.js and Go SDKs don't have this problem.
This commit fixes the issue by removing the `if ret_obj` check, as it's
not necessary. In practice, `ret_obj` is always going to be an instance
of `struct_pb2.Struct` because all monitors (CLI and mock) return an
instance, though the instance may be empty, which is ok.
Fixes #13985
2023-11-01 17:47:23 +00:00
|
|
|
# Copyright 2016-2023, Pulumi Corporation.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2023-11-14 22:26:45 +00:00
|
|
|
import pytest
|
|
|
|
|
[sdk/python] Fix error on empty invoke returns (#14470)
When an empty struct is returned from an invoke, the Python SDK was
returning None, which causes the following error to be raised from
generated provider Python SDKs when calling the invoke:
```
AssertionError: get can only be used with classes decorated with @input_type or @output_type
```
This is because the Python SDK is returning None if the return value
isn't truthy, roughly:
```
ret_obj = getattr(resp, "return")
if ret_obj:
return ret_obj
return None
```
However, an empty struct isn't truthy, so we're returning None in that
case, and generated provider Python SDKs can't handle that.
Other SDKs like the Node.js and Go SDKs don't have this problem.
This commit fixes the issue by removing the `if ret_obj` check, as it's
not necessary. In practice, `ret_obj` is always going to be an instance
of `struct_pb2.Struct` because all monitors (CLI and mock) return an
instance, though the instance may be empty, which is ok.
Fixes #13985
2023-11-01 17:47:23 +00:00
|
|
|
import pulumi
|
|
|
|
|
|
|
|
|
|
|
|
class MyMocks(pulumi.runtime.Mocks):
|
|
|
|
def new_resource(self, args: pulumi.runtime.MockResourceArgs):
|
2024-04-23 08:29:58 +00:00
|
|
|
return [args.name + "_id", args.inputs]
|
[sdk/python] Fix error on empty invoke returns (#14470)
When an empty struct is returned from an invoke, the Python SDK was
returning None, which causes the following error to be raised from
generated provider Python SDKs when calling the invoke:
```
AssertionError: get can only be used with classes decorated with @input_type or @output_type
```
This is because the Python SDK is returning None if the return value
isn't truthy, roughly:
```
ret_obj = getattr(resp, "return")
if ret_obj:
return ret_obj
return None
```
However, an empty struct isn't truthy, so we're returning None in that
case, and generated provider Python SDKs can't handle that.
Other SDKs like the Node.js and Go SDKs don't have this problem.
This commit fixes the issue by removing the `if ret_obj` check, as it's
not necessary. In practice, `ret_obj` is always going to be an instance
of `struct_pb2.Struct` because all monitors (CLI and mock) return an
instance, though the instance may be empty, which is ok.
Fixes #13985
2023-11-01 17:47:23 +00:00
|
|
|
|
|
|
|
def call(self, args: pulumi.runtime.MockCallArgs):
|
2023-11-14 22:26:45 +00:00
|
|
|
return {} if args.args.get("empty") else {"result": "mock"}
|
|
|
|
|
|
|
|
|
2024-04-23 08:29:58 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"tok,version,empty,expected",
|
|
|
|
[
|
|
|
|
("test:index:MyFunction", "", True, {}),
|
|
|
|
("test:index:MyFunction", "invalid", True, {}),
|
|
|
|
("test:index:MyFunction", "1.0.0", True, {}),
|
|
|
|
("test:index:MyFunction", "", False, {"result": "mock"}),
|
|
|
|
("test:index:MyFunction", "invalid", False, {"result": "mock"}),
|
|
|
|
("test:index:MyFunction", "1.0.0", False, {"result": "mock"}),
|
|
|
|
("kubernetes:something:new", "", True, {}),
|
|
|
|
("kubernetes:something:new", "invalid", True, {}),
|
|
|
|
("kubernetes:something:new", "1.0.0", True, {}),
|
|
|
|
("kubernetes:something:new", "4.5.3", True, {}),
|
|
|
|
("kubernetes:something:new", "4.5.4", True, {}),
|
|
|
|
("kubernetes:something:new", "4.5.5", True, {}),
|
|
|
|
("kubernetes:something:new", "4.6.0", True, {}),
|
|
|
|
("kubernetes:something:new", "5.0.0", True, {}),
|
|
|
|
("kubernetes:something:new", "", False, {"result": "mock"}),
|
|
|
|
("kubernetes:something:new", "invalid", False, {"result": "mock"}),
|
|
|
|
("kubernetes:something:new", "1.0.0", False, {"result": "mock"}),
|
|
|
|
# Expect the legacy behavior of getting None for empty results for these Kubernetes
|
|
|
|
# function tokens when the version is unspecified, invalid, or <= 4.5.4.
|
|
|
|
("kubernetes:yaml:decode", "", True, None),
|
|
|
|
("kubernetes:yaml:decode", "invalid", True, None),
|
|
|
|
("kubernetes:yaml:decode", "1.0.0", True, None),
|
|
|
|
("kubernetes:yaml:decode", "4.5.3", True, None),
|
|
|
|
("kubernetes:yaml:decode", "4.5.4", True, None),
|
|
|
|
("kubernetes:yaml:decode", "4.5.5", True, {}),
|
|
|
|
("kubernetes:yaml:decode", "4.6.0", True, {}),
|
|
|
|
("kubernetes:yaml:decode", "5.0.0", True, {}),
|
|
|
|
("kubernetes:yaml:decode", "", False, {"result": "mock"}),
|
|
|
|
("kubernetes:yaml:decode", "invalid", False, {"result": "mock"}),
|
|
|
|
("kubernetes:yaml:decode", "1.0.0", False, {"result": "mock"}),
|
|
|
|
("kubernetes:helm:template", "", True, None),
|
|
|
|
("kubernetes:helm:template", "invalid", True, None),
|
|
|
|
("kubernetes:helm:template", "1.0.0", True, None),
|
|
|
|
("kubernetes:helm:template", "4.5.3", True, None),
|
|
|
|
("kubernetes:helm:template", "4.5.4", True, None),
|
|
|
|
("kubernetes:helm:template", "4.5.5", True, {}),
|
|
|
|
("kubernetes:helm:template", "4.6.0", True, {}),
|
|
|
|
("kubernetes:helm:template", "5.0.0", True, {}),
|
|
|
|
("kubernetes:helm:template", "", False, {"result": "mock"}),
|
|
|
|
("kubernetes:helm:template", "invalid", False, {"result": "mock"}),
|
|
|
|
("kubernetes:helm:template", "1.0.0", False, {"result": "mock"}),
|
|
|
|
("kubernetes:kustomize:directory", "", True, None),
|
|
|
|
("kubernetes:kustomize:directory", "invalid", True, None),
|
|
|
|
("kubernetes:kustomize:directory", "1.0.0", True, None),
|
|
|
|
("kubernetes:kustomize:directory", "4.5.3", True, None),
|
|
|
|
("kubernetes:kustomize:directory", "4.5.4", True, None),
|
|
|
|
("kubernetes:kustomize:directory", "4.5.5", True, {}),
|
|
|
|
("kubernetes:kustomize:directory", "4.6.0", True, {}),
|
|
|
|
("kubernetes:kustomize:directory", "5.0.0", True, {}),
|
|
|
|
("kubernetes:kustomize:directory", "", False, {"result": "mock"}),
|
|
|
|
("kubernetes:kustomize:directory", "invalid", False, {"result": "mock"}),
|
|
|
|
("kubernetes:kustomize:directory", "1.0.0", False, {"result": "mock"}),
|
|
|
|
],
|
|
|
|
)
|
[sdk/python] Fix error on empty invoke returns (#14470)
When an empty struct is returned from an invoke, the Python SDK was
returning None, which causes the following error to be raised from
generated provider Python SDKs when calling the invoke:
```
AssertionError: get can only be used with classes decorated with @input_type or @output_type
```
This is because the Python SDK is returning None if the return value
isn't truthy, roughly:
```
ret_obj = getattr(resp, "return")
if ret_obj:
return ret_obj
return None
```
However, an empty struct isn't truthy, so we're returning None in that
case, and generated provider Python SDKs can't handle that.
Other SDKs like the Node.js and Go SDKs don't have this problem.
This commit fixes the issue by removing the `if ret_obj` check, as it's
not necessary. In practice, `ret_obj` is always going to be an instance
of `struct_pb2.Struct` because all monitors (CLI and mock) return an
instance, though the instance may be empty, which is ok.
Fixes #13985
2023-11-01 17:47:23 +00:00
|
|
|
@pulumi.runtime.test
|
2023-11-14 22:26:45 +00:00
|
|
|
def test_invoke_empty_return(tok: str, version: str, empty: bool, expected) -> None:
|
[sdk/python] Fix error on empty invoke returns (#14470)
When an empty struct is returned from an invoke, the Python SDK was
returning None, which causes the following error to be raised from
generated provider Python SDKs when calling the invoke:
```
AssertionError: get can only be used with classes decorated with @input_type or @output_type
```
This is because the Python SDK is returning None if the return value
isn't truthy, roughly:
```
ret_obj = getattr(resp, "return")
if ret_obj:
return ret_obj
return None
```
However, an empty struct isn't truthy, so we're returning None in that
case, and generated provider Python SDKs can't handle that.
Other SDKs like the Node.js and Go SDKs don't have this problem.
This commit fixes the issue by removing the `if ret_obj` check, as it's
not necessary. In practice, `ret_obj` is always going to be an instance
of `struct_pb2.Struct` because all monitors (CLI and mock) return an
instance, though the instance may be empty, which is ok.
Fixes #13985
2023-11-01 17:47:23 +00:00
|
|
|
pulumi.runtime.mocks.set_mocks(MyMocks())
|
|
|
|
|
2023-11-14 22:26:45 +00:00
|
|
|
props = {"empty": True} if empty else {}
|
|
|
|
opts = pulumi.InvokeOptions(version=version) if version else None
|
|
|
|
assert pulumi.runtime.invoke(tok, props, opts).value == expected
|