Skip to content

Commit

Permalink
Fix overloads for all and from_input to not return Never types (#16139
Browse files Browse the repository at this point in the history
)

<!--- 
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->

Fixes #16027.

Seems mypy was having issues with Outputs being passed into these
methods and tracking that they should have just been returning the same
Output in the result. Adding these extra overloads seems to have
appeased the type checker when using these methods.

## Checklist

- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
  - [x] I have formatted my code using `gofumpt`

<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
  • Loading branch information
Frassle committed May 7, 2024
1 parent b90a9bd commit cffdfd1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: sdk/python
description: Fix typings for `from_input` and `all` to not return `Never` types.
33 changes: 22 additions & 11 deletions sdk/python/lib/pulumi/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def is_secret(self) -> Awaitable[bool]:
return self._is_secret

def apply(
self, func: Callable[[T_co], Input[U]], run_with_unknowns: Optional[bool] = None
self, func: Callable[[T_co], Input[U]], run_with_unknowns: bool = False
) -> "Output[U]":
"""
Transforms the data of the output with the provided func. The result remains an
Expand Down Expand Up @@ -285,6 +285,14 @@ def __iter__(self) -> Any:
"'Output' object is not iterable, consider iterating the underlying value inside an 'apply'"
)

@overload
@staticmethod
def from_input(val: "Output[T_co]") -> "Output[T_co]": ...

@overload
@staticmethod
def from_input(val: Input[T_co]) -> "Output[T_co]": ...

@staticmethod
def from_input(val: Input[T_co]) -> "Output[T_co]":
"""
Expand Down Expand Up @@ -425,15 +433,22 @@ def secret(val: Input[T]) -> "Output[T]":
# https://mypy.readthedocs.io/en/stable/more_types.html#type-checking-the-variants:~:text=considered%20unsafely%20overlapping
@overload
@staticmethod
def all(*args: Input[T]) -> "Output[List[T]]": # type: ignore
...
def all(*args: "Output[T_co]") -> "Output[List[T_co]]": ... # type: ignore

@overload
@staticmethod
def all(**kwargs: "Output[T_co]") -> "Output[Dict[str, T_co]]": ... # type: ignore

@overload
@staticmethod
def all(**kwargs: Input[T]) -> "Output[Dict[str, T]]": ...
def all(*args: Input[T_co]) -> "Output[List[T_co]]": ... # type: ignore

@overload
@staticmethod
def all(*args: Input[T], **kwargs: Input[T]):
def all(**kwargs: Input[T_co]) -> "Output[Dict[str, T_co]]": ... # type: ignore

@staticmethod
def all(*args: Input[T_co], **kwargs: Input[T_co]):
"""
Produces an Output of a list (if args i.e a list of inputs are supplied)
or dict (if kwargs i.e. keyworded arguments are supplied).
Expand Down Expand Up @@ -486,19 +501,15 @@ async def gather_futures(outputs: Union[dict, list]):
}
return await _gather_from_dict(value_futures_dict)

from_input = cast(
Callable[[Union[T, Awaitable[T], Output[T]]], Output[T]], Output.from_input
)

if args and kwargs:
raise ValueError(
"Output.all() was supplied a mix of named and unnamed inputs"
)
# First, map all inputs to outputs using `from_input`.
all_outputs: Union[list, dict] = (
{k: from_input(v) for k, v in kwargs.items()}
{k: Output.from_input(v) for k, v in kwargs.items()}
if kwargs
else [from_input(x) for x in args]
else [Output.from_input(x) for x in args]
)

# Aggregate the list or dict of futures into a future of list or dict.
Expand Down

0 comments on commit cffdfd1

Please sign in to comment.