8000 fix: Pager typing · Der-Henning/server-client-python@7dc5ad4 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 7dc5ad4

Browse files
committed
fix: Pager typing
Pager Protocols were missing the generic flags. Added those in so the Pager correctly passes through the typing information. Also removes the kwargs from the function signature of the Endpoint.get protocol to make the Workbook endpoint match. Adding a return annotation of `None` to the tests is very important because it is what enables static type checkers, like mypy, to inspect those functions. With these annotations now in place, users of TSC should more transparently be able to carry through typing information when using the Pager.
1 parent 8b728a5 commit 7dc5ad4

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

tableauserverclient/server/pager.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1+
from collections.abc import Iterable, Iterator
12
import copy
23
from functools import partial
3-
from typing import Generic, Iterable, Iterator, List, Optional, Protocol, Tuple, TypeVar, Union, runtime_checkable
4+
from typing import List, Optional, Protocol, Tuple, TypeVar, Union, runtime_checkable
45

56
from tableauserverclient.models.pagination_item import PaginationItem
67
from tableauserverclient.server.request_options import RequestOptions
78

89

910
T = TypeVar("T")
10-
ReturnType = Tuple[List[T], PaginationItem]
1111

1212

1313
@runtime_checkable
14-
class Endpoint(Protocol):
15-
def get(self, req_options: Optional[RequestOptions], **kwargs) -> ReturnType:
14+
class Endpoint(Protocol[T]):
15+
def get(self, req_options: Optional[RequestOptions]) -> Tuple[List[T], PaginationItem]:
1616
...
1717

1818

1919
@runtime_checkable
20-
class CallableEndpoint(Protocol):
21-
def __call__(self, __req_options: Optional[RequestOptions], **kwargs) -> ReturnType:
20+
class CallableEndpoint(Protocol[T]):
21+
def __call__(self, __req_options: Optional[RequestOptions], **kwargs) -> Tuple[List[T], PaginationItem]:
2222
...
2323

2424

@@ -33,7 +33,7 @@ class Pager(Iterable[T]):
3333

3434
def __init__(
3535
self,
36-
endpoint: Union[CallableEndpoint, Endpoint],
36+
endpoint: Union[CallableEndpoint[T], Endpoint[T]],
3737
request_opts: Optional[RequestOptions] = None,
3838
**kwargs,
3939
) -> None:

test/test_pager.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
1111

12+
GET_VIEW_XML = os.path.join(TEST_ASSET_DIR, "view_get.xml")
1213
GET_XML_PAGE1 = os.path.join(TEST_ASSET_DIR, "workbook_get_page_1.xml")
1314
GET_XML_PAGE2 = os.path.join(TEST_ASSET_DIR, "workbook_get_page_2.xml")
1415
GET_XML_PAGE3 = os.path.join(TEST_ASSET_DIR, "workbook_get_page_3.xml")
@@ -35,7 +36,7 @@ def setUp(self):
3536

3637
self.baseurl = self.server.workbooks.baseurl
3738

38-
def test_pager_with_no_options(self):
39+
def test_pager_with_no_options(self) -> None:
3940
with open(GET_XML_PAGE1, "rb") as f:
4041
page_1 = f.read().decode("utf-8")
4142
with open(GET_XML_PAGE2, "rb") as f:
@@ -61,7 +62,7 @@ def test_pager_with_no_options(self):
6162
self.assertEqual(wb2.name, "Page2Workbook")
6263
self.assertEqual(wb3.name, "Page3Workbook")
6364

64-
def test_pager_with_options(self):
65+
def test_pager_with_options(self) -> None:
6566
with open(GET_XML_PAGE1, "rb") as f:
6667
page_1 = f.read().decode("utf-8")
6768
with open(GET_XML_PAGE2, "rb") as f:
@@ -102,14 +103,22 @@ def test_pager_with_options(self):
102103
wb3 = workbooks.pop()
103104
self.assertEqual(wb3.name, "Page3Workbook")
104105

105-
def test_pager_with_env_var(self):
106+
def test_pager_with_env_var(self) -> None:
106107
with set_env(TSC_PAGE_SIZE="1000"):
107108
assert config.PAGE_SIZE == 1000
108109
loop = TSC.Pager(self.server.workbooks)
109110
assert loop._options.pagesize == 1000
110111

111-
def test_queryset_with_env_var(self):
112+
def test_queryset_with_env_var(self) -> None:
112113
with set_env(TSC_PAGE_SIZE="1000"):
113114
assert config.PAGE_SIZE == 1000
114115
loop = self.server.workbooks.all()
115116
assert loop.request_options.pagesize == 1000
117+
118+
def test_pager_view(self) -> None:
119+
with open(GET_VIEW_XML, "rb") as f:
120+
view_xml = f.read().decode("utf-8")
121+
with requests_mock.mock() as m:
122+
m.get(self.baseurl, text=view_xml)
123+
for view in TSC.Pager(self.server.views):
124+
assert view.name == "Test View"

0 commit comments

Comments
 (0)
0