fix(files): resolve PathLike inside FileTypes tuples#1332
Open
qozle wants to merge 1 commit intoanthropics:mainfrom
Open
fix(files): resolve PathLike inside FileTypes tuples#1332qozle wants to merge 1 commit intoanthropics:mainfrom
qozle wants to merge 1 commit intoanthropics:mainfrom
Conversation
`is_file_content()` included `isinstance(obj, tuple)` in its guard, but `FileContent` is `Union[IO[bytes], bytes, PathLike[str]]` — tuples are not file content, they're `FileTypes` variants. This made the `is_tuple_t(file)` branch in `_transform_file` and `_async_transform_file` unreachable for tuple inputs, so `PathLike` objects in the second position of a `(name, PathLike, content_type)` tuple were returned as-is instead of being read with `read_file_content()`. httpx then received a `PathLike` where it expected `bytes` or `IO[bytes]`. Removing `isinstance(obj, tuple)` from `is_file_content` lets tuples fall through to the correct `is_tuple_t` branch, which calls `read_file_content(file[1])` and resolves the `PathLike`. Also corrects the `assert_is_file_content` error message which incorrectly listed tuples as valid `FileContent`. Fixes anthropics#1318
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #1318.
is_file_content()in_files.pyincludedisinstance(obj, tuple)in its check, butFileContent = Union[IO[bytes], bytes, PathLike[str]]— tuples are not file content, they areFileTypesvariants (the(name, content, content_type[, headers])forms).This meant the
is_tuple_t(file)branch in_transform_fileand_async_transform_filewas unreachable for any tuple input. Instead of callingread_file_content(file[1])on the inner element, both functions returned the tuple unchanged. When that inner element was aPathLike, httpx received it as-is and failed.Why it worked before (sort of)
Passing
("name", b"bytes")as aFileTypesvalue still produced a usable result because httpx accepts raw tuples with bytes. But("name", Path("foo"), "text/plain")failed because httpx does not readPathLikeobjects — and the type annotation in_types.pyexplicitly promisesPathLikeis a validFileContent.Fix
Remove
isinstance(obj, tuple)fromis_file_content. Tuples now fall through to theis_tuple_tbranch and get processed correctly —read_file_content/async_read_file_contentresolves thePathLiketo bytes before passing to httpx.Also corrects the
assert_is_file_contenterror message, which listed "or a tuple" as a validFileContentform.Tests
Added
test_pathlike_in_tupleandtest_async_pathlike_in_tuplewhich exercise the previously broken path. All existing tests pass.