Skip to content

Commit

Permalink
Fix overloads for all and from_input to not return Never types
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Frassle committed May 7, 2024
1 parent b90a9bd commit b024522
Showing 1 changed file with 22 additions and 11 deletions.
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 b024522

Please sign in to comment.