Repository: PrefectHQ/prefect
File: src/prefect/futures.py (Python)

Code before:
    """
    A list of Prefect futures.

    This class provides methods to wait for all futures
    in the list to complete and to retrieve the results of all task runs.
    """

    def wait(self, timeout: float | None = None) -> None:
        """
        Wait for all futures in the list to complete.

        Args:
            timeout: The maximum number of seconds to wait for all futures to
                complete. This method will not raise if the timeout is reached.
        """
        wait(self, timeout=timeout)

    def result(
        self: Self,
        timeout: float | None = None,
        raise_on_failure: bool = True,
    ) -> list[R]:
        """
        Get the results of all task runs associated with the futures in the list.

        Uses ``as_completed`` internally so that failures are raised as soon as
        they occur rather than waiting for earlier, still-running futures to
        finish first.

        Args:
            timeout: The maximum number of seconds to wait for all futures to
                complete.
            raise_on_failure: If `True`, an exception will be raised if any task run fails.

        Returns:
            A list of results of the task runs, in the same order as the
            futures in the list.

        Raises:
            TimeoutError: If the timeout is reached before all futures complete.
        """
        # Build a mapping from each unique future to every index it occupies
        # so that we can handle duplicates and preserve ordering.
        future_to_indices: dict[PrefectFuture[R], list[int]] = {}
        for idx, future in enumerate(self):
            future_to_indices.setdefault(future, []).append(idx)

        results: list[R] = [None] * len(self)  # type: ignore[list-item]

        try:
            # Wrap the entire loop in timeout_context so that both the wait

Review comment (bug):
```suggestion
        Uses `as_completed` internally so that failures are raised as soon as
```

Code after:
    """
    A list of Prefect futures.

    This class provides methods to wait for all futures
    in the list to complete and to retrieve the results of all task runs.
    """

    def wait(self, timeout: float | None = None) -> None:
        """
        Wait for all futures in the list to complete.

        Args:
            timeout: The maximum number of seconds to wait for all futures to
                complete. This method will not raise if the timeout is reached.
        """
        wait(self, timeout=timeout)

    def result(
        self: Self,
        timeout: float | None = None,
        raise_on_failure: bool = True,
    ) -> list[R]:
        """
        Get the results of all task runs associated with the futures in the list.

        Uses `as_completed` internally so that failures are raised as soon as
        they occur rather than waiting for earlier, still-running futures to
        finish first.

        Args:
            timeout: The maximum number of seconds to wait for all futures to
                complete.
            raise_on_failure: If `True`, an exception will be raised if any task run fails.

        Returns:
            A list of results of the task runs, in the same order as the
            futures in the list.

        Raises:
            TimeoutError: If the timeout is reached before all futures complete.
        """
        # Build a mapping from each unique future to every index it occupies
        # so that we can handle duplicates and preserve ordering.
        future_to_indices: dict[PrefectFuture[R], list[int]] = {}
        for idx, future in enumerate(self):
            future_to_indices.setdefault(future, []).append(idx)

        results: list[R] = [None] * len(self)  # type: ignore[list-item]

        try:
            # Wrap the entire loop in timeout_context so that both the wait