8000 chore: fix typing on TaggingMixin · tableau/server-client-python@a80b505 · GitHub
[go: up one dir, main page]

Skip to content

Commit a80b505

Browse files
committed
chore: fix typing on TaggingMixin
1 parent 8d6b3f2 commit a80b505

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

tableauserverclient/server/endpoint/datasources_endpoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
PathOrFileW = Union[FilePath, FileObjectW]
5656

5757

58-
class Datasources(QuerysetEndpoint[DatasourceItem], TaggingMixin):
58+
class Datasources(QuerysetEndpoint[DatasourceItem], TaggingMixin[DatasourceItem]):
5959
def __init__(self, parent_srv: "Server") -> None:
6060
super(Datasources, self).__init__(parent_srv)
6161
self._permissions = _PermissionsEndpoint(parent_srv, lambda: self.baseurl)

tableauserverclient/server/endpoint/flows_endpoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
PathOrFileW = Union[FilePath, FileObjectW]
5252

5353

54-
class Flows(QuerysetEndpoint[FlowItem], TaggingMixin):
54+
class Flows(QuerysetEndpoint[FlowItem], TaggingMixin[FlowItem]):
5555
def __init__(self, parent_srv):
5656
super(Flows, self).__init__(parent_srv)
5757
self._resource_tagger = _ResourceTagger(parent_srv)

tableauserverclient/server/endpoint/resource_tagger.py

Lines changed: 21 additions & 21 deletions
-
def add_tags(self, item: Union[HasID, Taggable, str], tags: Union[Iterable[str], str]) -> Set[str]:
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22
import copy
3-
from typing import Iterable, Optional, Protocol, Set, Union, TYPE_CHECKING, runtime_checkable
3+
from typing import Generic, Iterable, Optional, Protocol, Set, TypeVar, Union, TYPE_CHECKING, runtime_checkable
44
import urllib.parse
55

66
from tableauserverclient.server.endpoint.endpoint import Endpoint, api
@@ -61,28 +61,22 @@ def update_tags(self, baseurl, resource_item):
6161
resource_item._initial_tags = copy.copy(resource_item.tags)
6262
logger.info("Updated tags to {0}".format(resource_item.tags))
6363

64-
65-
class HasID(Protocol):
66-
@property
67-
def id(self) -> Optional[str]:
68-
pass
69-
64+
class Response(Protocol):
65+
content: bytes
7066

7167
@runtime_checkable
7268
class Taggable(Protocol):
73-
_initial_tags: Set[str]
7469
tags: Set[str]
70+
_initial_tags: Set[str]
7571

7672
@property
7773
def id(self) -> Optional[str]:
7874
pass
7975

80-
81-
class Response(Protocol):
82-
content: bytes
76+
T = TypeVar("T")
8377

8478

85-
class TaggingMixin(abc.ABC):
79+
class TaggingMixin(abc.ABC, Generic[T]):
8680
parent_srv: "Server"
8781

8882
@property
@@ -98,7 +92,7 @@ def put_request(self, url, request) -> Response:
9892
def delete_request(self, url) -> None:
9993
pass
10094

101
95+
def add_tags(self, item: Union[T, str], tags: Union[Iterable[str], str]) -> Set[str]:
10296
item_id = getattr(item, "id", item)
10397

10498
if not isinstance(item_id, str):
@@ -114,7 +108,7 @@ def add_tags(self, item: Union[HasID, Taggable, str], tags: Union[Iterable[str],
114108
server_response = self.put_request(url, add_req)
115109
return TagItem.from_response(server_response.content, self.parent_srv.namespace)
116110

117-
def delete_tags(self, item: Union[HasID, Taggable, str], tags: Union[Iterable[str], str]) -> None:
111+
def delete_tags(self, item: Union[T, str], tags: Union[Iterable[str], str]) -> None:
118112
item_id = getattr(item, "id", item)
119113

120114
if not isinstance(item_id, str):
@@ -130,17 +124,23 @@ def delete_tags(self, item: Union[HasID, Taggable, str], tags: Union[Iterable[st
130124
url = f"{self.baseurl}/{item_id}/tags/{encoded_tag_name}"
131125
self.delete_request(url)
132126

133-
def update_tags(self, item: Taggable) -> None:
134-
if item.tags == item._initial_tags:
127+
def update_tags(self, item: T) -> None:
128+
if (initial_tags := getattr(item, "_initial_tags", None)) is None:
129+
raise ValueError(f"{item} does not have initial tags.")
130+
if (tags := getattr(item, "tags", None)) is None:
131+
raise ValueError(f"{item} does not have tags.")
132+
if tags == initial_tags:
135133
return
136134

137-
add_set = item.tags - item._initial_tags
138-
remove_set = item._initial_tags - item.tags
135+
add_set = tags - initial_tags
136+
remove_set = initial_tags - tags
139137
self.delete_tags(item, remove_set)
140138
if add_set:
141-
item.tags = self.add_tags(item, add_set)
142-
item._initial_tags = copy.copy(item.tags)
143-
logger.info(f"Updated tags to {item.tags}")
139+
tags = self.add_tags(item, add_set)
140+
setattr(item, "tags", tags)
141+
142+
setattr(item, "_initial_tags", copy.copy(tags))
143+
logger.info(f"Updated tags to {tags}")
144144

145145

146146
content = Iterable[Union["ColumnItem", "DatabaseItem", "DatasourceItem", "FlowItem", "TableItem", "WorkbookItem"]]

tableauserverclient/server/endpoint/tables_endpoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from tableauserverclient.helpers.logging import logger
1414

1515

16-
class Tables(Endpoint, TaggingMixin):
16+
class Tables(Endpoint, TaggingMixin[TableItem]):
1717
def __init__(self, parent_srv):
1818
super(Tables, self).__init__(parent_srv)
1919

tableauserverclient/server/endpoint/views_endpoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
)
2424

2525

26-
class Views(QuerysetEndpoint[ViewItem], TaggingMixin):
26+
class Views(QuerysetEndpoint[ViewItem], TaggingMixin[ViewItem]):
2727
def __init__(self, parent_srv):
2828
super(Views, self).__init__(parent_srv)
2929
self._permissions = _PermissionsEndpoint(parent_srv, lambda: self.baseurl)

tableauserverclient/server/endpoint/workbooks_endpoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
PathOrFileW = Union[FilePath, FileObjectW]
6060

6161

62-
class Workbooks(QuerysetEndpoint[WorkbookItem], TaggingMixin):
62+
class Workbooks(QuerysetEndpoint[WorkbookItem], TaggingMixin[WorkbookItem]):
6363
def __init__(self, parent_srv: "Server") -> None:
6464
super(Workbooks, self).__init__(parent_srv)
6565
self._permissions = _PermissionsEndpoint(parent_srv, lambda: self.baseurl)

0 commit comments

Comments
 (0)
0