Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pagination of empty response for PaginationType.NONE #524

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion alpaca/common/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,10 @@ def _return_paginated_result(
"""
if handle_pagination == PaginationType.NONE:
# user wants no pagination, so just do a single page
return next(iterator)
try:
return next(iterator)
except StopIteration:
return []
Copy link
Contributor

@hiohiohio hiohiohio Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dtatarkin thank you for this PR.
I have noticed that PaginationType.NONE might not work and should not be used without fixed for the current methods/responses.
The issue is that alpaca-py does not return page_token in a response of methods. Therefore, it might easily cause an issue of not noticing actual length of records/data. It might cause a breaking change as well. In the case, raw data/response could be dict instead of list.
I am now considering removing/deprecating PagenationType.NONE rather than fixing with the current spec as we might not have a way to return page_token in a raw data format.

fyi. the other consideration is that this change causes the behaviour change and might cause surprise to existing users. But I wanted to point out the issue above more.

elif handle_pagination == PaginationType.FULL:
# the iterator returns "pages", so we use chain to flatten them all into 1 list
return list(chain.from_iterable(iterator))
Expand Down
21 changes: 20 additions & 1 deletion tests/broker/broker_client/test_rebalancing_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
GetSubscriptionsRequest,
UpdatePortfolioRequest,
)
from alpaca.common.enums import BaseURL
from alpaca.common.enums import BaseURL, PaginationType


def test_create_portfolio(reqmock: Mocker, client: BrokerClient) -> None:
Expand Down Expand Up @@ -403,6 +403,25 @@ def test_get_all_subscriptions(reqmock: Mocker, client: BrokerClient) -> None:
assert isinstance(response[0], Subscription)


def test_get_all_subscriptions_empty_no_pagination(
reqmock: Mocker, client: BrokerClient
) -> None:
"""Test the get_all_subscriptions method."""
reqmock.get(
f"{BaseURL.BROKER_SANDBOX.value}/v1/rebalancing/subscriptions",
text="""{
"subscriptions": [],
"next_page_token": null
}""",
)
response = client.get_all_subscriptions(
filter=GetSubscriptionsRequest(), handle_pagination=PaginationType.NONE
)

assert reqmock.called_once
assert len(response) == 0


def test_get_subscription_by_id(reqmock: Mocker, client: BrokerClient) -> None:
"""Test the get_subscription_by_id method."""
sub_id = UUID("9341be15-8786-4d23-ba1a-fc10ef4f90f4")
Expand Down
Loading