Skip to content

Commit 2370092

Browse files
committed
fix: resolve mypy type checking errors for Python 3.11
- Added type casts for TemporaryFileWrapper to BinaryIO - Added type: ignore comments for method assignments in tests - Fixed list[str] vs list[FileInput] type variance issues - Ensured all type annotations are compatible with strict mypy checking
1 parent 6571053 commit 2370092

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

tests/unit/test_direct_api.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Comprehensive unit tests for Direct API methods."""
22

33
import tempfile
4+
from typing import BinaryIO, cast
45
from unittest.mock import Mock, patch
56

67
import pytest
@@ -195,9 +196,9 @@ def test_merge_pdfs_returns_bytes(self, mock_save, mock_prepare):
195196
]
196197

197198
# Mock HTTP client
198-
self.client._http_client.post = Mock(return_value=self.mock_response)
199+
self.client._http_client.post = Mock(return_value=self.mock_response) # type: ignore
199200

200-
result = self.client.merge_pdfs(["file1.pdf", "file2.pdf"])
201+
result = self.client.merge_pdfs(["file1.pdf", "file2.pdf"]) # type: ignore[arg-type]
201202

202203
assert result == self.mock_response
203204
assert mock_prepare.call_count == 2
@@ -221,7 +222,7 @@ def test_merge_pdfs_saves_to_file(self, mock_save, mock_prepare):
221222
]
222223

223224
# Mock HTTP client
224-
self.client._http_client.post = Mock(return_value=self.mock_response)
225+
self.client._http_client.post = Mock(return_value=self.mock_response) # type: ignore
225226

226227
result = self.client.merge_pdfs(["file1.pdf", "file2.pdf"], "merged.pdf")
227228

@@ -247,10 +248,10 @@ def test_merge_pdfs_multiple_files(self, mock_prepare):
247248
]
248249

249250
# Mock HTTP client
250-
self.client._http_client.post = Mock(return_value=self.mock_response)
251+
self.client._http_client.post = Mock(return_value=self.mock_response) # type: ignore
251252

252253
files = ["file1.pdf", "file2.pdf", "file3.pdf"]
253-
result = self.client.merge_pdfs(files)
254+
result = self.client.merge_pdfs(files) # type: ignore[arg-type]
254255

255256
assert result == self.mock_response
256257
assert mock_prepare.call_count == 3
@@ -298,8 +299,8 @@ def test_direct_api_with_file_like_object(self, mock_process):
298299
temp_file.write(b"test content")
299300
temp_file.seek(0)
300301

301-
self.client.rotate_pages(temp_file, degrees=90)
302-
mock_process.assert_called_once_with("rotate-pages", temp_file, None, degrees=90)
302+
self.client.rotate_pages(cast(BinaryIO, temp_file), degrees=90)
303+
mock_process.assert_called_once_with("rotate-pages", cast(BinaryIO, temp_file), None, degrees=90)
303304

304305

305306
class TestDirectAPIErrorHandling:
@@ -456,9 +457,9 @@ def test_merge_pdfs_maximum_files(self, mock_prepare):
456457
]
457458

458459
# Mock HTTP client
459-
self.client._http_client.post = Mock(return_value=b"merged_result")
460+
self.client._http_client.post = Mock(return_value=b"merged_result") # type: ignore
460461

461-
result = self.client.merge_pdfs(files)
462+
result = self.client.merge_pdfs(files) # type: ignore[arg-type]
462463

463464
assert result == b"merged_result"
464465
assert mock_prepare.call_count == 10

tests/unit/test_file_handler.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import tempfile
66
from pathlib import Path
7+
from typing import BinaryIO, cast
78
from unittest.mock import Mock, patch
89

910
import pytest
@@ -73,7 +74,7 @@ def test_prepare_file_input_from_file_handle(self):
7374
temp_file.write(content)
7475
temp_file.seek(0)
7576

76-
result, filename = prepare_file_input(temp_file)
77+
result, filename = prepare_file_input(cast(BinaryIO, temp_file))
7778
assert result == content
7879
assert filename == os.path.basename(temp_file.name)
7980

@@ -83,7 +84,7 @@ def test_prepare_file_input_from_string_file_handle(self):
8384
string_file = io.StringIO(string_content)
8485
string_file.name = "test.txt"
8586

86-
result, filename = prepare_file_input(string_file)
87+
result, filename = prepare_file_input(cast(BinaryIO, string_file))
8788
assert result == string_content.encode()
8889
assert filename == "test.txt"
8990

@@ -111,9 +112,10 @@ def test_prepare_file_input_file_handle_with_path_name(self):
111112
temp_file.seek(0)
112113

113114
# Mock the name to be a path-like object
114-
temp_file.name = Path(temp_file.name)
115+
original_name = temp_file.name
116+
temp_file.name = Path(temp_file.name) # type: ignore
115117

116-
result, filename = prepare_file_input(temp_file)
118+
result, filename = prepare_file_input(cast(BinaryIO, temp_file))
117119
assert result == content
118120
assert filename == os.path.basename(str(temp_file.name))
119121

@@ -159,7 +161,8 @@ def test_prepare_file_for_upload_large_file(self):
159161
assert content_type == "application/octet-stream"
160162

161163
# Clean up the file handle
162-
file_handle.close()
164+
if hasattr(file_handle, "close"):
165+
file_handle.close()
163166
finally:
164167
os.unlink(temp_file.name)
165168

@@ -423,7 +426,7 @@ def test_get_file_size_seekable_file_object(self):
423426
temp_file.write(content)
424427
temp_file.seek(5) # Move to middle of file
425428

426-
size = get_file_size(temp_file)
429+
size = get_file_size(cast(BinaryIO, temp_file))
427430
assert size == len(content)
428431

429432
# Verify position was restored

0 commit comments

Comments
 (0)