Skip to content

Commit 095df29

Browse files
committed
style: mypy and black
1 parent 9897eed commit 095df29

File tree

4 files changed

+28
-87
lines changed

4 files changed

+28
-87
lines changed

tableauserverclient/server/endpoint/resource_tagger.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22
import copy
3-
from typing import Generic, Iterable, Set, TypeVar, Union
3+
from typing import Generic, Iterable, Optional, Protocol, Set, TypeVar, Union, runtime_checkable
44
import urllib.parse
55

66
from tableauserverclient.server.endpoint.endpoint import Endpoint
@@ -53,15 +53,15 @@ def update_tags(self, baseurl, resource_item):
5353
logger.info("Updated tags to {0}".format(resource_item.tags))
5454

5555

56-
T = TypeVar("T")
56+
@runtime_checkable
57+
class Taggable(Protocol):
58+
_initial_tags: Set[str]
59+
id: Optional[str] = None
60+
tags: Set[str]
5761

5862

59-
class TaggingMixin(Generic[T]):
60-
@abc.abstractmethod
61-
def baseurl(self) -> str:
62-
raise NotImplementedError("baseurl must be implemented.")
63-
64-
def add_tags(self, item: Union[T, str], tags: Union[Iterable[str], str]) -> Set[str]:
63+
class TaggingMixin:
64+
def add_tags(self, item: Union[Taggable, str], tags: Union[Iterable[str], str]) -> Set[str]:
6565
item_id = getattr(item, "id", item)
6666

6767
if not isinstance(item_id, str):
@@ -72,12 +72,12 @@ def add_tags(self, item: Union[T, str], tags: Union[Iterable[str], str]) -> Set[
7272
else:
7373
tag_set = set(tags)
7474

75-
url = f"{self.baseurl}/{item_id}/tags"
75+
url = f"{self.baseurl}/{item_id}/tags" # type: ignore
7676
add_req = RequestFactory.Tag.add_req(tag_set)
77-
server_response = self.put_request(url, add_req)
78-
return TagItem.from_response(server_response.content, self.parent_srv.namespace)
77+
server_response = self.put_request(url, add_req) # type: ignore
78+
return TagItem.from_response(server_response.content, self.parent_srv.namespace) # type: ignore
7979

80-
def delete_tags(self, item: Union[T, str], tags: Union[Iterable[str], str]) -> None:
80+
def delete_tags(self, item: Union[Taggable, str], tags: Union[Iterable[str], str]) -> None:
8181
item_id = getattr(item, "id", item)
8282

8383
if not isinstance(item_id, str):
@@ -90,10 +90,10 @@ def delete_tags(self, item: Union[T, str], tags: Union[Iterable[str], str]) -> N
9090

9191
for tag in tag_set:
9292
encoded_tag_name = urllib.parse.quote(tag)
93-
url = f"{self.baseurl}/{item_id}/tags/{encoded_tag_name}"
94-
self.delete_request(url)
93+
url = f"{self.baseurl}/{item_id}/tags/{encoded_tag_name}" # type: ignore
94+
self.delete_request(url) # type: ignore
9595

96-
def update_tags(self, item: T) -> None:
96+
def update_tags(self, item: Taggable) -> None:
9797
if item.tags == item._initial_tags:
9898
return
9999

tableauserverclient/server/endpoint/workbooks_endpoint.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
PathOrFileW = Union[FilePath, FileObjectW]
5959

6060

61-
class Workbooks(QuerysetEndpoint[WorkbookItem], TaggingMixin[WorkbookItem]):
61+
class Workbooks(QuerysetEndpoint[WorkbookItem], TaggingMixin):
6262
def __init__(self, parent_srv: "Server") -> None:
6363
super(Workbooks, self).__init__(parent_srv)
6464
self._resource_tagger = _ResourceTagger(parent_srv)
@@ -502,6 +502,6 @@ def schedule_extract_refresh(
502502
return self.parent_srv.schedules.add_to_schedule(schedule_id, workbook=item)
503503

504504

505-
Workbooks.add_tags = api(version="1.0")(Workbooks.add_tags)
506-
Workbooks.delete_tags = api(version="1.0")(Workbooks.delete_tags)
507-
Workbooks.update_tags = api(version="1.0")(Workbooks.update_tags)
505+
Workbooks.add_tags = api(version="1.0")(Workbooks.add_tags) # type: ignore
506+
Workbooks.delete_tags = api(version="1.0")(Workbooks.delete_tags) # type: ignore
507+
Workbooks.update_tags = api(version="1.0")(Workbooks.update_tags) # type: ignore

test/assets/workbook_add_tags.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
3+
<tags>
4+
<tag label="a" />
5+
<tag label="b" />
6+
<tag label="c" />
7+
<tag label="d" />
8+
</tags>
9+
</tsResponse>

test/test_workbook.py

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
2020

21-
ADD_TAG_XML = os.path.join(TEST_ASSET_DIR, "workbook_add_tag.xml")
2221
ADD_TAGS_XML = os.path.join(TEST_ASSET_DIR, "workbook_add_tags.xml")
2322
GET_BY_ID_XML = os.path.join(TEST_ASSET_DIR, "workbook_get_by_id.xml")
2423
GET_BY_ID_XML_PERSONAL = os.path.join(TEST_ASSET_DIR, "workbook_get_by_id_personal.xml")
@@ -895,70 +894,3 @@ def test_odata_connection(self) -> None:
895894

896895
assert xml_connection is not None
897896
self.assertEqual(xml_connection.get("serverAddress"), url)
898-
899-
def test_add_tags(self) -> None:
900-
workbook = TSC.WorkbookItem("project", "test")
901-
workbook._id = "06b944d2-959d-4604-9305-12323c95e70e"
902-
tags = list("abcd")
903-
904-
with requests_mock.mock() as m:
905-
m.put(
906-
f"{self.baseurl}/{workbook.id}/tags",
907-
status_code=200,
908-
text=Path(ADD_TAGS_XML).read_text(),
909-
)
910-
tag_result = self.server.workbooks.add_tags(workbook, tags)
911-
912-
for a, b in zip(sorted(tag_result), sorted(tags)):
913-
self.assertEqual(a, b)
914-
915-
def test_add_tag(self) -> None:
916-
workbook = TSC.WorkbookItem("project", "test")
917-
workbook._id = "06b944d2-959d-4604-9305-12323c95e70e"
918-
tags = "a"
919-
920-
with requests_mock.mock() as m:
921-
m.put(
922-
f"{self.baseurl}/{workbook.id}/tags",
923-
status_code=200,
924-
text=Path(ADD_TAG_XML).read_text(),
925-
)
926-
tag_result = self.server.workbooks.add_tags(workbook, tags)
927-
928-
for a, b in zip(sorted(tag_result), sorted(tags)):
929-
self.assertEqual(a, b)
930-
931-
def test_add_tag_id(self) -> None:
932-
workbook = TSC.WorkbookItem("project", "test")
933-
workbook._id = "06b944d2-959d-4604-9305-12323c95e70e"
934-
tags = "a"
935-
936-
with requests_mock.mock() as m:
937-
m.put(
938-
f"{self.baseurl}/{workbook.id}/tags",
939-
status_code=200,
940-
text=Path(ADD_TAG_XML).read_text(),
941-
)
942-
tag_result = self.server.workbooks.add_tags(workbook.id, tags)
943-
944-
for a, b in zip(sorted(tag_result), sorted(tags)):
945-
self.assertEqual(a, b)
946-
947-
def test_delete_tags(self) -> None:
948-
workbook = TSC.WorkbookItem("project", "test")
949-
workbook._id = "06b944d2-959d-4604-9305-12323c95e70e"
950-
tags = list("abcd")
951-
952-
matcher = re.compile(rf"{self.baseurl}\/{workbook.id}\/tags\/[abcd]")
953-
with requests_mock.mock() as m:
954-
m.delete(
955-
matcher,
956-
status_code=200,
957-
text="",
958-
)
959-
self.server.workbooks.delete_tags(workbook, tags)
960-
history = m.request_history
961-
962-
self.assertEqual(len(history), len(tags))
963-
urls = sorted([r.url.split("/")[-1] for r in history])
964-
self.assertEqual(urls, sorted(tags))

0 commit comments

Comments
 (0)