diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 86b0e83..6c7bc2e 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.21.0" + ".": "0.21.1" } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index ff2e00c..3c319af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,24 @@ # Changelog +## 0.21.1 (2026-04-18) + +Full Changelog: [v0.21.0...v0.21.1](https://github.com/isaacus-dev/isaacus-python/compare/v0.21.0...v0.21.1) + +### Bug Fixes + +* **client:** preserve hardcoded query params when merging with user params ([1e99aa4](https://github.com/isaacus-dev/isaacus-python/commit/1e99aa438e488d81e2e37ad5ea41a48e7b60f1eb)) +* ensure file data are only sent as 1 parameter ([32dab0e](https://github.com/isaacus-dev/isaacus-python/commit/32dab0e1abc9f4f5ea0446cb8bb17c9ee2b55b7a)) + + +### Performance Improvements + +* **client:** optimize file structure copying in multipart requests ([3f32843](https://github.com/isaacus-dev/isaacus-python/commit/3f32843f81d5115e1c495cc42fdd095ff99f3822)) + + +### Documentation + +* update examples ([4186943](https://github.com/isaacus-dev/isaacus-python/commit/4186943a4a7c4571c7ee2a097bd1715ec074e5dd)) + ## 0.21.0 (2026-03-27) Full Changelog: [v0.20.0...v0.21.0](https://github.com/isaacus-dev/isaacus-python/compare/v0.20.0...v0.21.0) diff --git a/pyproject.toml b/pyproject.toml index 43ce8b0..227c1c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "isaacus" -version = "0.21.0" +version = "0.21.1" description = "The official Python library for the isaacus API" dynamic = ["readme"] license = "Apache-2.0" diff --git a/src/isaacus/_base_client.py b/src/isaacus/_base_client.py index 967ce99..628eead 100644 --- a/src/isaacus/_base_client.py +++ b/src/isaacus/_base_client.py @@ -540,6 +540,10 @@ def _build_request( files = cast(HttpxRequestFiles, ForceMultipartDict()) prepared_url = self._prepare_url(options.url) + # preserve hard-coded query params from the url + if params and prepared_url.query: + params = {**dict(prepared_url.params.items()), **params} + prepared_url = prepared_url.copy_with(raw_path=prepared_url.raw_path.split(b"?", 1)[0]) if "_" in prepared_url.host: # work around https://github.com/encode/httpx/discussions/2880 kwargs["extensions"] = {"sni_hostname": prepared_url.host.replace("_", "-")} diff --git a/src/isaacus/_files.py b/src/isaacus/_files.py index cc14c14..0fdce17 100644 --- a/src/isaacus/_files.py +++ b/src/isaacus/_files.py @@ -3,8 +3,8 @@ import io import os import pathlib -from typing import overload -from typing_extensions import TypeGuard +from typing import Sequence, cast, overload +from typing_extensions import TypeVar, TypeGuard import anyio @@ -17,7 +17,9 @@ HttpxFileContent, HttpxRequestFiles, ) -from ._utils import is_tuple_t, is_mapping_t, is_sequence_t +from ._utils import is_list, is_mapping, is_tuple_t, is_mapping_t, is_sequence_t + +_T = TypeVar("_T") def is_base64_file_input(obj: object) -> TypeGuard[Base64FileInput]: @@ -121,3 +123,51 @@ async def async_read_file_content(file: FileContent) -> HttpxFileContent: return await anyio.Path(file).read_bytes() return file + + +def deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]]) -> _T: + """Copy only the containers along the given paths. + + Used to guard against mutation by extract_files without copying the entire structure. + Only dicts and lists that lie on a path are copied; everything else + is returned by reference. + + For example, given paths=[["foo", "files", "file"]] and the structure: + { + "foo": { + "bar": {"baz": {}}, + "files": {"file": } + } + } + The root dict, "foo", and "files" are copied (they lie on the path). + "bar" and "baz" are returned by reference (off the path). + """ + return _deepcopy_with_paths(item, paths, 0) + + +def _deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]], index: int) -> _T: + if not paths: + return item + if is_mapping(item): + key_to_paths: dict[str, list[Sequence[str]]] = {} + for path in paths: + if index < len(path): + key_to_paths.setdefault(path[index], []).append(path) + + # if no path continues through this mapping, it won't be mutated and copying it is redundant + if not key_to_paths: + return item + + result = dict(item) + for key, subpaths in key_to_paths.items(): + if key in result: + result[key] = _deepcopy_with_paths(result[key], subpaths, index + 1) + return cast(_T, result) + if is_list(item): + array_paths = [path for path in paths if index < len(path) and path[index] == ""] + + # if no path expects a list here, nothing will be mutated inside it - return by reference + if not array_paths: + return cast(_T, item) + return cast(_T, [_deepcopy_with_paths(entry, array_paths, index + 1) for entry in item]) + return item diff --git a/src/isaacus/_utils/__init__.py b/src/isaacus/_utils/__init__.py index 10cb66d..1c090e5 100644 --- a/src/isaacus/_utils/__init__.py +++ b/src/isaacus/_utils/__init__.py @@ -24,7 +24,6 @@ coerce_integer as coerce_integer, file_from_path as file_from_path, strip_not_given as strip_not_given, - deepcopy_minimal as deepcopy_minimal, get_async_library as get_async_library, maybe_coerce_float as maybe_coerce_float, get_required_header as get_required_header, diff --git a/src/isaacus/_utils/_utils.py b/src/isaacus/_utils/_utils.py index eec7f4a..771859f 100644 --- a/src/isaacus/_utils/_utils.py +++ b/src/isaacus/_utils/_utils.py @@ -86,8 +86,9 @@ def _extract_items( index += 1 if is_dict(obj): try: - # We are at the last entry in the path so we must remove the field - if (len(path)) == index: + # Remove the field if there are no more dict keys in the path, + # only "" traversal markers or end. + if all(p == "" for p in path[index:]): item = obj.pop(key) else: item = obj[key] @@ -176,21 +177,6 @@ def is_iterable(obj: object) -> TypeGuard[Iterable[object]]: return isinstance(obj, Iterable) -def deepcopy_minimal(item: _T) -> _T: - """Minimal reimplementation of copy.deepcopy() that will only copy certain object types: - - - mappings, e.g. `dict` - - list - - This is done for performance reasons. - """ - if is_mapping(item): - return cast(_T, {k: deepcopy_minimal(v) for k, v in item.items()}) - if is_list(item): - return cast(_T, [deepcopy_minimal(entry) for entry in item]) - return item - - # copied from https://github.com/Rapptz/RoboDanny def human_join(seq: Sequence[str], *, delim: str = ", ", final: str = "or") -> str: size = len(seq) diff --git a/src/isaacus/_version.py b/src/isaacus/_version.py index fe74c62..b42f9bf 100644 --- a/src/isaacus/_version.py +++ b/src/isaacus/_version.py @@ -1,4 +1,4 @@ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. __title__ = "isaacus" -__version__ = "0.21.0" # x-release-please-version +__version__ = "0.21.1" # x-release-please-version diff --git a/tests/api_resources/classifications/test_universal.py b/tests/api_resources/classifications/test_universal.py index 215db24..757e645 100644 --- a/tests/api_resources/classifications/test_universal.py +++ b/tests/api_resources/classifications/test_universal.py @@ -38,8 +38,8 @@ def test_method_create_with_all_params(self, client: Isaacus) -> None: scoring_method="auto", chunking_options={ "size": 512, - "overlap_ratio": 0.1, - "overlap_tokens": 10, + "overlap_ratio": None, + "overlap_tokens": None, }, ) assert_matches_type(UniversalClassificationResponse, universal, path=["response"]) @@ -101,8 +101,8 @@ async def test_method_create_with_all_params(self, async_client: AsyncIsaacus) - scoring_method="auto", chunking_options={ "size": 512, - "overlap_ratio": 0.1, - "overlap_tokens": 10, + "overlap_ratio": None, + "overlap_tokens": None, }, ) assert_matches_type(UniversalClassificationResponse, universal, path=["response"]) diff --git a/tests/api_resources/extractions/test_qa.py b/tests/api_resources/extractions/test_qa.py index a76e06d..4841f7f 100644 --- a/tests/api_resources/extractions/test_qa.py +++ b/tests/api_resources/extractions/test_qa.py @@ -41,9 +41,9 @@ def test_method_create_with_all_params(self, client: Isaacus) -> None: ignore_inextractability=False, top_k=1, chunking_options={ - "size": 512, - "overlap_ratio": 0.1, - "overlap_tokens": 10, + "size": None, + "overlap_ratio": None, + "overlap_tokens": None, }, ) assert_matches_type(AnswerExtractionResponse, qa, path=["response"]) @@ -112,9 +112,9 @@ async def test_method_create_with_all_params(self, async_client: AsyncIsaacus) - ignore_inextractability=False, top_k=1, chunking_options={ - "size": 512, - "overlap_ratio": 0.1, - "overlap_tokens": 10, + "size": None, + "overlap_ratio": None, + "overlap_tokens": None, }, ) assert_matches_type(AnswerExtractionResponse, qa, path=["response"]) diff --git a/tests/api_resources/test_embeddings.py b/tests/api_resources/test_embeddings.py index 5ae4bce..932e12c 100644 --- a/tests/api_resources/test_embeddings.py +++ b/tests/api_resources/test_embeddings.py @@ -34,7 +34,7 @@ def test_method_create_with_all_params(self, client: Isaacus) -> None: texts=["Are restraints of trade enforceable under English law?", "What is a non-compete clause?"], task="retrieval/query", overflow_strategy="drop_end", - dimensions=1, + dimensions=1792, ) assert_matches_type(EmbeddingResponse, embedding, path=["response"]) @@ -89,7 +89,7 @@ async def test_method_create_with_all_params(self, async_client: AsyncIsaacus) - texts=["Are restraints of trade enforceable under English law?", "What is a non-compete clause?"], task="retrieval/query", overflow_strategy="drop_end", - dimensions=1, + dimensions=1792, ) assert_matches_type(EmbeddingResponse, embedding, path=["response"]) diff --git a/tests/api_resources/test_enrichments.py b/tests/api_resources/test_enrichments.py index 18ab2d8..3b120d3 100644 --- a/tests/api_resources/test_enrichments.py +++ b/tests/api_resources/test_enrichments.py @@ -22,7 +22,9 @@ class TestEnrichments: def test_method_create(self, client: Isaacus) -> None: enrichment = client.enrichments.create( model="kanon-2-enricher", - texts=['1.5 You (the "User") agree to be bound by these Terms.'], + texts=[ + '[42] The U.S. Attorney General, Mr. McGill, argued at ¶ 21 of the Filing that "§ 206 of Title 29 of the U.S. Code (the "Labor Title") does not apply to the plaintiff, Ms. Moody, given the definition of an "employee" at §203(e)(4) of the Labor Title does not include volunteers, and, regardless, she lives in Austria."' + ], ) assert_matches_type(EnrichmentResponse, enrichment, path=["response"]) @@ -31,7 +33,9 @@ def test_method_create(self, client: Isaacus) -> None: def test_method_create_with_all_params(self, client: Isaacus) -> None: enrichment = client.enrichments.create( model="kanon-2-enricher", - texts=['1.5 You (the "User") agree to be bound by these Terms.'], + texts=[ + '[42] The U.S. Attorney General, Mr. McGill, argued at ¶ 21 of the Filing that "§ 206 of Title 29 of the U.S. Code (the "Labor Title") does not apply to the plaintiff, Ms. Moody, given the definition of an "employee" at §203(e)(4) of the Labor Title does not include volunteers, and, regardless, she lives in Austria."' + ], overflow_strategy="auto", ) assert_matches_type(EnrichmentResponse, enrichment, path=["response"]) @@ -41,7 +45,9 @@ def test_method_create_with_all_params(self, client: Isaacus) -> None: def test_raw_response_create(self, client: Isaacus) -> None: response = client.enrichments.with_raw_response.create( model="kanon-2-enricher", - texts=['1.5 You (the "User") agree to be bound by these Terms.'], + texts=[ + '[42] The U.S. Attorney General, Mr. McGill, argued at ¶ 21 of the Filing that "§ 206 of Title 29 of the U.S. Code (the "Labor Title") does not apply to the plaintiff, Ms. Moody, given the definition of an "employee" at §203(e)(4) of the Labor Title does not include volunteers, and, regardless, she lives in Austria."' + ], ) assert response.is_closed is True @@ -54,7 +60,9 @@ def test_raw_response_create(self, client: Isaacus) -> None: def test_streaming_response_create(self, client: Isaacus) -> None: with client.enrichments.with_streaming_response.create( model="kanon-2-enricher", - texts=['1.5 You (the "User") agree to be bound by these Terms.'], + texts=[ + '[42] The U.S. Attorney General, Mr. McGill, argued at ¶ 21 of the Filing that "§ 206 of Title 29 of the U.S. Code (the "Labor Title") does not apply to the plaintiff, Ms. Moody, given the definition of an "employee" at §203(e)(4) of the Labor Title does not include volunteers, and, regardless, she lives in Austria."' + ], ) as response: assert not response.is_closed assert response.http_request.headers.get("X-Stainless-Lang") == "python" @@ -75,7 +83,9 @@ class TestAsyncEnrichments: async def test_method_create(self, async_client: AsyncIsaacus) -> None: enrichment = await async_client.enrichments.create( model="kanon-2-enricher", - texts=['1.5 You (the "User") agree to be bound by these Terms.'], + texts=[ + '[42] The U.S. Attorney General, Mr. McGill, argued at ¶ 21 of the Filing that "§ 206 of Title 29 of the U.S. Code (the "Labor Title") does not apply to the plaintiff, Ms. Moody, given the definition of an "employee" at §203(e)(4) of the Labor Title does not include volunteers, and, regardless, she lives in Austria."' + ], ) assert_matches_type(EnrichmentResponse, enrichment, path=["response"]) @@ -84,7 +94,9 @@ async def test_method_create(self, async_client: AsyncIsaacus) -> None: async def test_method_create_with_all_params(self, async_client: AsyncIsaacus) -> None: enrichment = await async_client.enrichments.create( model="kanon-2-enricher", - texts=['1.5 You (the "User") agree to be bound by these Terms.'], + texts=[ + '[42] The U.S. Attorney General, Mr. McGill, argued at ¶ 21 of the Filing that "§ 206 of Title 29 of the U.S. Code (the "Labor Title") does not apply to the plaintiff, Ms. Moody, given the definition of an "employee" at §203(e)(4) of the Labor Title does not include volunteers, and, regardless, she lives in Austria."' + ], overflow_strategy="auto", ) assert_matches_type(EnrichmentResponse, enrichment, path=["response"]) @@ -94,7 +106,9 @@ async def test_method_create_with_all_params(self, async_client: AsyncIsaacus) - async def test_raw_response_create(self, async_client: AsyncIsaacus) -> None: response = await async_client.enrichments.with_raw_response.create( model="kanon-2-enricher", - texts=['1.5 You (the "User") agree to be bound by these Terms.'], + texts=[ + '[42] The U.S. Attorney General, Mr. McGill, argued at ¶ 21 of the Filing that "§ 206 of Title 29 of the U.S. Code (the "Labor Title") does not apply to the plaintiff, Ms. Moody, given the definition of an "employee" at §203(e)(4) of the Labor Title does not include volunteers, and, regardless, she lives in Austria."' + ], ) assert response.is_closed is True @@ -107,7 +121,9 @@ async def test_raw_response_create(self, async_client: AsyncIsaacus) -> None: async def test_streaming_response_create(self, async_client: AsyncIsaacus) -> None: async with async_client.enrichments.with_streaming_response.create( model="kanon-2-enricher", - texts=['1.5 You (the "User") agree to be bound by these Terms.'], + texts=[ + '[42] The U.S. Attorney General, Mr. McGill, argued at ¶ 21 of the Filing that "§ 206 of Title 29 of the U.S. Code (the "Labor Title") does not apply to the plaintiff, Ms. Moody, given the definition of an "employee" at §203(e)(4) of the Labor Title does not include volunteers, and, regardless, she lives in Austria."' + ], ) as response: assert not response.is_closed assert response.http_request.headers.get("X-Stainless-Lang") == "python" diff --git a/tests/api_resources/test_rerankings.py b/tests/api_resources/test_rerankings.py index 51cb025..7e349db 100644 --- a/tests/api_resources/test_rerankings.py +++ b/tests/api_resources/test_rerankings.py @@ -46,13 +46,13 @@ def test_method_create_with_all_params(self, client: Isaacus) -> None: "Negligence in tort law requires establishing a duty of care that the defendant owed to the plaintiff.", "The concept of negligence is central to tort law, with courts assessing whether a breach of duty caused harm.", ], - top_n=1, + top_n=None, is_iql=False, scoring_method="auto", chunking_options={ - "size": 512, - "overlap_ratio": 0.1, - "overlap_tokens": 10, + "size": None, + "overlap_ratio": None, + "overlap_tokens": None, }, ) assert_matches_type(RerankingResponse, reranking, path=["response"]) @@ -134,13 +134,13 @@ async def test_method_create_with_all_params(self, async_client: AsyncIsaacus) - "Negligence in tort law requires establishing a duty of care that the defendant owed to the plaintiff.", "The concept of negligence is central to tort law, with courts assessing whether a breach of duty caused harm.", ], - top_n=1, + top_n=None, is_iql=False, scoring_method="auto", chunking_options={ - "size": 512, - "overlap_ratio": 0.1, - "overlap_tokens": 10, + "size": None, + "overlap_ratio": None, + "overlap_tokens": None, }, ) assert_matches_type(RerankingResponse, reranking, path=["response"]) diff --git a/tests/test_client.py b/tests/test_client.py index ac88537..156ebfd 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -427,6 +427,30 @@ def test_default_query_option(self) -> None: client.close() + def test_hardcoded_query_params_in_url(self, client: Isaacus) -> None: + request = client._build_request(FinalRequestOptions(method="get", url="/foo?beta=true")) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true"} + + request = client._build_request( + FinalRequestOptions( + method="get", + url="/foo?beta=true", + params={"limit": "10", "page": "abc"}, + ) + ) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true", "limit": "10", "page": "abc"} + + request = client._build_request( + FinalRequestOptions( + method="get", + url="/files/a%2Fb?beta=true", + params={"limit": "10"}, + ) + ) + assert request.url.raw_path == b"/files/a%2Fb?beta=true&limit=10" + def test_request_extra_json(self, client: Isaacus) -> None: request = client._build_request( FinalRequestOptions( @@ -1335,6 +1359,30 @@ async def test_default_query_option(self) -> None: await client.close() + async def test_hardcoded_query_params_in_url(self, async_client: AsyncIsaacus) -> None: + request = async_client._build_request(FinalRequestOptions(method="get", url="/foo?beta=true")) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true"} + + request = async_client._build_request( + FinalRequestOptions( + method="get", + url="/foo?beta=true", + params={"limit": "10", "page": "abc"}, + ) + ) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true", "limit": "10", "page": "abc"} + + request = async_client._build_request( + FinalRequestOptions( + method="get", + url="/files/a%2Fb?beta=true", + params={"limit": "10"}, + ) + ) + assert request.url.raw_path == b"/files/a%2Fb?beta=true&limit=10" + def test_request_extra_json(self, client: Isaacus) -> None: request = client._build_request( FinalRequestOptions( diff --git a/tests/test_deepcopy.py b/tests/test_deepcopy.py deleted file mode 100644 index 1c2bf71..0000000 --- a/tests/test_deepcopy.py +++ /dev/null @@ -1,58 +0,0 @@ -from isaacus._utils import deepcopy_minimal - - -def assert_different_identities(obj1: object, obj2: object) -> None: - assert obj1 == obj2 - assert id(obj1) != id(obj2) - - -def test_simple_dict() -> None: - obj1 = {"foo": "bar"} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - - -def test_nested_dict() -> None: - obj1 = {"foo": {"bar": True}} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1["foo"], obj2["foo"]) - - -def test_complex_nested_dict() -> None: - obj1 = {"foo": {"bar": [{"hello": "world"}]}} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1["foo"], obj2["foo"]) - assert_different_identities(obj1["foo"]["bar"], obj2["foo"]["bar"]) - assert_different_identities(obj1["foo"]["bar"][0], obj2["foo"]["bar"][0]) - - -def test_simple_list() -> None: - obj1 = ["a", "b", "c"] - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - - -def test_nested_list() -> None: - obj1 = ["a", [1, 2, 3]] - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1[1], obj2[1]) - - -class MyObject: ... - - -def test_ignores_other_types() -> None: - # custom classes - my_obj = MyObject() - obj1 = {"foo": my_obj} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert obj1["foo"] is my_obj - - # tuples - obj3 = ("a", "b") - obj4 = deepcopy_minimal(obj3) - assert obj3 is obj4 diff --git a/tests/test_extract_files.py b/tests/test_extract_files.py index 7d5b442..7ef62a0 100644 --- a/tests/test_extract_files.py +++ b/tests/test_extract_files.py @@ -35,6 +35,15 @@ def test_multiple_files() -> None: assert query == {"documents": [{}, {}]} +def test_top_level_file_array() -> None: + query = {"files": [b"file one", b"file two"], "title": "hello"} + assert extract_files(query, paths=[["files", ""]]) == [ + ("files[]", b"file one"), + ("files[]", b"file two"), + ] + assert query == {"title": "hello"} + + @pytest.mark.parametrize( "query,paths,expected", [ diff --git a/tests/test_files.py b/tests/test_files.py index df07c03..332f21b 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -4,7 +4,8 @@ import pytest from dirty_equals import IsDict, IsList, IsBytes, IsTuple -from isaacus._files import to_httpx_files, async_to_httpx_files +from isaacus._files import to_httpx_files, deepcopy_with_paths, async_to_httpx_files +from isaacus._utils import extract_files readme_path = Path(__file__).parent.parent.joinpath("README.md") @@ -49,3 +50,99 @@ def test_string_not_allowed() -> None: "file": "foo", # type: ignore } ) + + +def assert_different_identities(obj1: object, obj2: object) -> None: + assert obj1 == obj2 + assert obj1 is not obj2 + + +class TestDeepcopyWithPaths: + def test_copies_top_level_dict(self) -> None: + original = {"file": b"data", "other": "value"} + result = deepcopy_with_paths(original, [["file"]]) + assert_different_identities(result, original) + + def test_file_value_is_same_reference(self) -> None: + file_bytes = b"contents" + original = {"file": file_bytes} + result = deepcopy_with_paths(original, [["file"]]) + assert_different_identities(result, original) + assert result["file"] is file_bytes + + def test_list_popped_wholesale(self) -> None: + files = [b"f1", b"f2"] + original = {"files": files, "title": "t"} + result = deepcopy_with_paths(original, [["files", ""]]) + assert_different_identities(result, original) + result_files = result["files"] + assert isinstance(result_files, list) + assert_different_identities(result_files, files) + + def test_nested_array_path_copies_list_and_elements(self) -> None: + elem1 = {"file": b"f1", "extra": 1} + elem2 = {"file": b"f2", "extra": 2} + original = {"items": [elem1, elem2]} + result = deepcopy_with_paths(original, [["items", "", "file"]]) + assert_different_identities(result, original) + result_items = result["items"] + assert isinstance(result_items, list) + assert_different_identities(result_items, original["items"]) + assert_different_identities(result_items[0], elem1) + assert_different_identities(result_items[1], elem2) + + def test_empty_paths_returns_same_object(self) -> None: + original = {"foo": "bar"} + result = deepcopy_with_paths(original, []) + assert result is original + + def test_multiple_paths(self) -> None: + f1 = b"file1" + f2 = b"file2" + original = {"a": f1, "b": f2, "c": "unchanged"} + result = deepcopy_with_paths(original, [["a"], ["b"]]) + assert_different_identities(result, original) + assert result["a"] is f1 + assert result["b"] is f2 + assert result["c"] is original["c"] + + def test_extract_files_does_not_mutate_original_top_level(self) -> None: + file_bytes = b"contents" + original = {"file": file_bytes, "other": "value"} + + copied = deepcopy_with_paths(original, [["file"]]) + extracted = extract_files(copied, paths=[["file"]]) + + assert extracted == [("file", file_bytes)] + assert original == {"file": file_bytes, "other": "value"} + assert copied == {"other": "value"} + + def test_extract_files_does_not_mutate_original_nested_array_path(self) -> None: + file1 = b"f1" + file2 = b"f2" + original = { + "items": [ + {"file": file1, "extra": 1}, + {"file": file2, "extra": 2}, + ], + "title": "example", + } + + copied = deepcopy_with_paths(original, [["items", "", "file"]]) + extracted = extract_files(copied, paths=[["items", "", "file"]]) + + assert extracted == [("items[][file]", file1), ("items[][file]", file2)] + assert original == { + "items": [ + {"file": file1, "extra": 1}, + {"file": file2, "extra": 2}, + ], + "title": "example", + } + assert copied == { + "items": [ + {"extra": 1}, + {"extra": 2}, + ], + "title": "example", + }