Skip to content

Commit 39ac22e

Browse files
refactor: Evaluate page_size in __post_init__ instead of get_page_size()
This change evaluates the interpolated page_size once during initialization rather than on every get_page_size() call, matching the pattern used by PageIncrement. This is more efficient and catches configuration errors earlier at initialization time. Co-Authored-By: alfredo.garcia@airbyte.io <freddy.garcia7.fg@gmail.com>
1 parent 54d4e2d commit 39ac22e

2 files changed

Lines changed: 14 additions & 21 deletions

File tree

airbyte_cdk/sources/declarative/requesters/paginators/strategies/cursor_pagination_strategy.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ def __post_init__(self, parameters: Mapping[str, Any]) -> None:
5454
else:
5555
self._stop_condition = self.stop_condition
5656

57-
page_size = str(self.page_size) if isinstance(self.page_size, int) else self.page_size
58-
if page_size:
59-
self._page_size: Optional[InterpolatedString] = InterpolatedString(
60-
page_size, parameters=parameters
61-
)
57+
if isinstance(self.page_size, int) or (self.page_size is None):
58+
self._page_size = self.page_size
6259
else:
63-
self._page_size = None
60+
page_size = InterpolatedString(self.page_size, parameters=parameters).eval(self.config)
61+
if not isinstance(page_size, int):
62+
raise Exception(f"{page_size} is of type {type(page_size)}. Expected {int}")
63+
self._page_size = page_size
6464

6565
@property
6666
def initial_token(self) -> Optional[Any]:
@@ -103,10 +103,4 @@ def next_page_token(
103103
return token if token else None
104104

105105
def get_page_size(self) -> Optional[int]:
106-
if self._page_size:
107-
page_size = self._page_size.eval(self.config)
108-
if not isinstance(page_size, int):
109-
raise Exception(f"{page_size} is of type {type(page_size)}. Expected {int}")
110-
return page_size
111-
else:
112-
return None
106+
return self._page_size

unit_tests/sources/declarative/requesters/paginators/test_cursor_pagination_strategy.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,11 @@ def test_interpolated_page_size(page_size_input, config, expected_page_size):
144144

145145

146146
def test_interpolated_page_size_raises_on_non_integer():
147-
"""Test that get_page_size raises an exception when interpolation resolves to a non-integer."""
148-
strategy = CursorPaginationStrategy(
149-
page_size="{{ config['page_size'] }}",
150-
cursor_value="token",
151-
config={"page_size": "invalid"},
152-
parameters={},
153-
)
147+
"""Test that initialization raises an exception when interpolation resolves to a non-integer."""
154148
with pytest.raises(Exception, match="is of type .* Expected"):
155-
strategy.get_page_size()
149+
CursorPaginationStrategy(
150+
page_size="{{ config['page_size'] }}",
151+
cursor_value="token",
152+
config={"page_size": "invalid"},
153+
parameters={},
154+
)

0 commit comments

Comments
 (0)