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

Skip to content

Commit 9f7bfaa

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

File tree

6 files changed

+26
-23
lines changed

6 files changed

+26
-23
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 & 18 deletions
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
@@ -62,27 +62,24 @@ def update_tags(self, baseurl, resource_item):
6262
logger.info("Updated tags to {0}".format(resource_item.tags))
6363

6464

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

7068

7169
@runtime_checkable
7270
class Taggable(Protocol):
73-
_initial_tags: Set[str]
7471
tags: Set[str]
72+
_initial_tags: Set[str]
7573

7674
@property
7775
def id(self) -> Optional[str]:
7876
pass
7977

8078

81-
class Response(Protocol):
82-
content: bytes
79+
T = TypeVar("T")
8380

8481

85-
class TaggingMixin(abc.ABC):
82+
class TaggingMixin(abc.ABC, Generic[T]):
8683
parent_srv: "Server"
8784

8885
@property
@@ -98,7 +95,7 @@ def put_request(self, url, request) -> Response:
9895
def delete_request(self, url) -> None:
9996
pass
10097

101-
def add_tags(self, item: Union[HasID, Taggable, str], tags: Union[Iterable[str], str]) -> Set[str]:
98+
def add_tags(self, item: Union[T, str], tags: Union[Iterable[str], str]) -> Set[str]:
10299
item_id = getattr(item, "id", item)
103100

104101
if not isinstance(item_id, str):
@@ -114,7 +111,7 @@ def add_tags(self, item: Union[HasID, Taggable, str], tags: Union[Iterable[str],
114111
server_response = self.put_request(url, add_req)
115112
return TagItem.from_response(server_response.content, self.parent_srv.namespace)
116113

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

120117
if not isinstance(item_id, str):
@@ -130,17 +127,23 @@ def delete_tags(self, item: Union[HasID, Taggable, str], tags: Union[Iterable[st
130127
url = f"{self.baseurl}/{item_id}/tags/{encoded_tag_name}"
131128
self.delete_request(url)
132129

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

137-
add_set = item.tags - item._initial_tags
138-
remove_set = item._initial_tags - item.tags
138+
add_set = tags - initial_tags
139+
remove_set = initial_tags - tags
139140
self.delete_tags(item, remove_set)
140141
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}")
142+
tags = self.add_tags(item, add_set)
143+
setattr(item, "tags", tags)
144+
145+
setattr(item, "_initial_tags", copy.copy(tags))
146+
logger.info(f"Updated tags to {tags}")
144147

145148

146149
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