Skip to content

Commit c47b00e

Browse files
committed
Appease mypy
1 parent 3c24953 commit c47b00e

5 files changed

Lines changed: 16 additions & 29 deletions

File tree

python/lib/sift_py/data_import/_parquet_test.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
from sift_py.data_import.config import ParquetConfig
77
from sift_py.data_import.parquet import ParquetUploadService, _extract_parquet_footer
8+
from sift_py.data_import.parquet_complex_types import ParquetComplexTypesImportModeType
89
from sift_py.data_import.status import DataImportService
10+
from sift_py.data_import.time_format import TimeFormatType
911
from sift_py.rest import SiftRestConfig
1012

1113

@@ -81,10 +83,7 @@ def test_upload_config_request_failed(mocker: MockFixture, parquet_config):
8183

8284
def test_upload_invalid_config_response(mocker: MockFixture, parquet_config):
8385
mock_requests_post = mocker.patch("sift_py.rest.requests.Session.post")
84-
mock_requests_post.return_value = MockResponse(status_code=200, text="not json")
85-
mock_requests_post.return_value.json = lambda: (_ for _ in ()).throw(
86-
Exception("Invalid response")
87-
)
86+
mock_requests_post.return_value = MockResponse(status_code=200, json_data=None)
8887
svc = ParquetUploadService(rest_config)
8988
with pytest.raises(Exception, match="Invalid response"):
9089
svc.upload("file.parquet", parquet_config)
@@ -142,10 +141,7 @@ def test_upload_from_url_failed(mocker: MockFixture, parquet_config):
142141

143142
def test_upload_from_url_invalid_response(mocker: MockFixture, parquet_config):
144143
mock_requests_post = mocker.patch("sift_py.rest.requests.Session.post")
145-
mock_requests_post.return_value = MockResponse(status_code=200, text="not json")
146-
mock_requests_post.return_value.json = lambda: (_ for _ in ()).throw(
147-
Exception("Invalid response")
148-
)
144+
mock_requests_post.return_value = MockResponse(status_code=200, json_data=None)
149145
svc = ParquetUploadService(rest_config)
150146
with pytest.raises(Exception, match="Invalid response"):
151147
svc.upload_from_url("http://file.parquet", parquet_config)
@@ -269,8 +265,8 @@ def test_flat_dataset_upload_overrides_success(mocker: MockFixture, parquet_conf
269265
"asset",
270266
"file.parquet",
271267
"time",
272-
time_format="TIME_FORMAT_RELATIVE_SECONDS",
273-
complex_types_import_mode="PARQUET_COMPLEX_TYPES_IMPORT_MODE_BYTES",
268+
time_format=TimeFormatType.RELATIVE_SECONDS,
269+
complex_types_import_mode=ParquetComplexTypesImportModeType.BYTES,
274270
run_id="run_42",
275271
relative_start_time="2024-01-01T00:00:00Z",
276272
)
@@ -388,8 +384,7 @@ def test_detect_config_flat_dataset_invalid_json(mocker: MockFixture):
388384
"sift_py.data_import.parquet._extract_parquet_footer", return_value=(b"footerbytes", 123)
389385
)
390386
mock_post = mocker.patch("sift_py.rest.requests.Session.post")
391-
mock_post.return_value = MockResponse(status_code=200, text="not json")
392-
mock_post.return_value.json = lambda: (_ for _ in ()).throw(Exception("Invalid response"))
387+
mock_post.return_value = MockResponse(status_code=200, json_data=None)
393388
svc = ParquetUploadService(rest_config)
394389
svc._detect_config_uri = "http://detect.com"
395390
with pytest.raises(Exception, match="Invalid response"):

python/lib/sift_py/data_import/_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pathlib import Path
55
from typing import Callable, List, Optional, Tuple, Union
66

7-
from alive_progress import alive_bar
7+
from alive_progress import alive_bar # type: ignore
88

99

1010
def mime_and_content_type_from_path(path: Path) -> Tuple[str, Optional[str], Optional[str]]:
@@ -32,7 +32,7 @@ def validate_file_type(path: Union[str, Path], valid_file_types: List[str]) -> O
3232
return content_encoding
3333

3434

35-
def convert_keys_to_snake_case(obj):
35+
def convert_keys_to_snake_case(obj: dict) -> dict:
3636
"""Recursively convert all dict keys from camelCase to snake_case."""
3737

3838
def camel_to_snake(name: str) -> str:

python/lib/sift_py/data_import/parquet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def upload(
4545
parquet_config: The Parquet config.
4646
show_progress: Whether to show the status bar or not.
4747
"""
48-
if not path.endswith(".parquet"):
48+
if not str(path).endswith(".parquet"):
4949
raise Exception("Must use an uncompressed parquet file")
5050

5151
response = self._session.post(
@@ -154,7 +154,7 @@ def flat_dataset_upload(
154154
Override `run_id` to specify the id of the run to add this data to. Default is None.
155155
Override `relative_start_time` if a relative time format is used. Default is None.
156156
"""
157-
if not path.endswith(".parquet"):
157+
if not str(path).endswith(".parquet"):
158158
raise Exception("Must use an uncompressed parquet file")
159159

160160
config_info = self._detect_config_flat_dataset(path)

python/lib/sift_py/data_import/status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class DataImport(BaseModel):
4242
source_url: str = ""
4343
status: Union[str, DataImportStatusType]
4444
error_message: str = ""
45-
csv_config: Optional[dict]
46-
parquet_config: Optional[dict]
45+
csv_config: Optional[dict] = None
46+
parquet_config: Optional[dict] = None
4747

4848
@field_validator("status", mode="before")
4949
@classmethod

python/lib/sift_py/file_attachment/_internal/upload.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import mimetypes
21
from pathlib import Path
3-
from typing import Any, Dict, Optional, Tuple, Union
2+
from typing import Any, Dict, Optional, Union
43
from urllib.parse import urljoin
54

65
from requests_toolbelt import MultipartEncoder
76

87
from sift_py._internal.convert.json import to_json
8+
from sift_py.data_import._utils import mime_and_content_type_from_path
99
from sift_py.file_attachment.entity import Entity
1010
from sift_py.file_attachment.metadata import Metadata
1111
from sift_py.rest import SiftRestConfig, _RestService
@@ -41,9 +41,7 @@ def upload_attachment(
4141
if not posix_path.is_file():
4242
raise Exception(f"Provided path, '{path}', does not point to a regular file.")
4343

44-
file_name, mimetype, content_encoding = self.__class__._mime_and_content_type_from_path(
45-
posix_path
46-
)
44+
file_name, mimetype, content_encoding = mime_and_content_type_from_path(posix_path)
4745

4846
if not mimetype:
4947
raise Exception(f"The MIME-type of '{posix_path}' could not be computed.")
@@ -93,9 +91,3 @@ def upload_attachment(
9391
)
9492

9593
return response.json().get("remoteFile").get("remoteFileId")
96-
97-
@staticmethod
98-
def _mime_and_content_type_from_path(path: Path) -> Tuple[str, Optional[str], Optional[str]]:
99-
file_name = path.name
100-
mime, encoding = mimetypes.guess_type(path)
101-
return file_name, mime, encoding

0 commit comments

Comments
 (0)