Skip to content

Commit 63aa054

Browse files
committed
chore: make datasource typing more specific
Use overloads to narrow return types of DatasourceEndpoint to reflect what users actually pass in.
1 parent 59eaebb commit 63aa054

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

tableauserverclient/server/endpoint/datasources_endpoint.py

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from contextlib import closing
88
from pathlib import Path
9-
from typing import Literal, Optional, TYPE_CHECKING, Union, overload
9+
from typing import Literal, Optional, TYPE_CHECKING, TypeVar, Union, overload
1010
from collections.abc import Iterable, Mapping, Sequence
1111

1212
from tableauserverclient.helpers.headers import fix_filename
@@ -50,7 +50,6 @@
5050
FileObject = Union[io.BufferedReader, io.BytesIO]
5151
PathOrFile = Union[FilePath, FileObject]
5252

53-
FilePath = Union[str, os.PathLike]
5453
FileObjectR = Union[io.BufferedReader, io.BytesIO]
5554
FileObjectW = Union[io.BufferedWriter, io.BytesIO]
5655
PathOrFileR = Union[FilePath, FileObjectR]
@@ -191,16 +190,34 @@ def delete(self, datasource_id: str) -> None:
191190
self.delete_request(url)
192191
logger.info(f"Deleted single datasource (ID: {datasource_id})")
193192

193+
T = TypeVar("T", bound=FileObjectW)
194+
195+
@overload
196+
def download(
197+
self,
198+
datasource_id: str,
199+
filepath: T,
200+
include_extract: bool = True,
201+
) -> T: ...
202+
203+
@overload
204+
def download(
205+
self,
206+
datasource_id: str,
207+
filepath: Optional[FilePath] = None,
208+
include_extract: bool = True,
209+
) -> str: ...
210+
194211
# Download 1 datasource by id
195212
@api(version="2.0")
196213
@parameter_added_in(no_extract="2.5")
197214
@parameter_added_in(include_extract="2.5")
198215
def download(
199216
self,
200-
datasource_id: str,
201-
filepath: Optional[PathOrFileW] = None,
202-
include_extract: bool = True,
203-
) -> PathOrFileW:
217+
datasource_id,
218+
filepath,
219+
include_extract=True,
220+
):
204221
"""
205222
Downloads the specified data source from a site. The data source is
206223
downloaded as a .tdsx file.
@@ -479,13 +496,13 @@ def publish(
479496
@parameter_added_in(as_job="3.0")
480497
def publish(
481498
self,
482-
datasource_item: DatasourceItem,
483-
file: PathOrFileR,
484-
mode: str,
485-
connection_credentials: Optional[ConnectionCredentials] = None,
486-
connections: Optional[Sequence[ConnectionItem]] = None,
487-
as_job: bool = False,
488-
) -> Union[DatasourceItem, JobItem]:
499+
datasource_item,
500+
file,
501+
mode,
502+
connection_credentials=None,
503+
connections=None,
504+
as_job=False,
505+
):
489506
"""
490507
Publishes a data source to a server, or appends data to an existing
491508
data source.
@@ -898,15 +915,35 @@ def _get_datasource_revisions(
898915
revisions = RevisionItem.from_response(server_response.content, self.parent_srv.namespace, datasource_item)
899916
return revisions
900917

901-
# Download 1 datasource revision by revision number
902-
@api(version="2.3")
918+
T = TypeVar("T", bound=FileObjectW)
919+
920+
@overload
921+
def download_revision(
922+
self,
923+
datasource_id: str,
924+
revision_number: Optional[str],
925+
filepath: T,
926+
include_extract: bool = True,
927+
) -> T: ...
928+
929+
@overload
903930
def download_revision(
904931
self,
905932
datasource_id: str,
906933
revision_number: Optional[str],
907-
filepath: Optional[PathOrFileW] = None,
934+
filepath: Optional[FilePath] = None,
908935
include_extract: bool = True,
909-
) -> PathOrFileW:
936+
) -> str: ...
937+
938+
# Download 1 datasource revision by revision number
939+
@api(version="2.3")
940+
def download_revision(
941+
self,
942+
datasource_id,
943+
revision_number,
944+
filepath,
945+
include_extract,
946+
):
910947
"""
911948
Downloads a specific version of a data source prior to the current one
912949
in .tdsx format. To download the current version of a data source set

0 commit comments

Comments
 (0)