File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -63,16 +63,16 @@ def to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles | None:
6363
6464
6565def _transform_file (file : FileTypes ) -> HttpxFileTypes :
66+ if is_tuple_t (file ):
67+ return (file [0 ], read_file_content (file [1 ]), * file [2 :])
68+
6669 if is_file_content (file ):
6770 if isinstance (file , os .PathLike ):
6871 path = pathlib .Path (file )
6972 return (path .name , path .read_bytes ())
7073
7174 return file
7275
73- if is_tuple_t (file ):
74- return (file [0 ], read_file_content (file [1 ]), * file [2 :])
75-
7676 raise TypeError (f"Expected file types input to be a FileContent type or to be a tuple" )
7777
7878
@@ -105,16 +105,16 @@ async def async_to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles
105105
106106
107107async def _async_transform_file (file : FileTypes ) -> HttpxFileTypes :
108+ if is_tuple_t (file ):
109+ return (file [0 ], await async_read_file_content (file [1 ]), * file [2 :])
110+
108111 if is_file_content (file ):
109112 if isinstance (file , os .PathLike ):
110113 path = anyio .Path (file )
111114 return (path .name , await path .read_bytes ())
112115
113116 return file
114117
115- if is_tuple_t (file ):
116- return (file [0 ], await async_read_file_content (file [1 ]), * file [2 :])
117-
118118 raise TypeError (f"Expected file types input to be a FileContent type or to be a tuple" )
119119
120120
Original file line number Diff line number Diff line change @@ -19,6 +19,11 @@ def test_tuple_input() -> None:
1919 assert result == [("file" , ("README.md" , readme_path .read_bytes ()))]
2020
2121
22+ def test_tuple_input_reads_pathlike_content () -> None :
23+ result = to_httpx_files ({"file" : ("custom.txt" , readme_path )})
24+ assert result == {"file" : ("custom.txt" , readme_path .read_bytes ())}
25+
26+
2227@pytest .mark .asyncio
2328async def test_async_pathlib_includes_file_name () -> None :
2429 result = await async_to_httpx_files ({"file" : readme_path })
@@ -37,6 +42,12 @@ async def test_async_tuple_input() -> None:
3742 assert result == [("file" , ("README.md" , readme_path .read_bytes ()))]
3843
3944
45+ @pytest .mark .asyncio
46+ async def test_async_tuple_input_reads_pathlike_content () -> None :
47+ result = await async_to_httpx_files ({"file" : ("custom.txt" , readme_path )})
48+ assert result == {"file" : ("custom.txt" , readme_path .read_bytes ())}
49+
50+
4051def test_string_not_allowed () -> None :
4152 with pytest .raises (TypeError , match = "Expected file types input to be a FileContent type or to be a tuple" ):
4253 to_httpx_files (
You can’t perform that action at this time.
0 commit comments