Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "5.4.0"
".": "5.4.1"
}
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 48
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/imagekit-inc%2Fimagekit-70c42eda2bee929830b2537f758400a58dded1f1ef5686a286e2469c35a041a0.yml
openapi_spec_hash: cdaeed824e91657b45092765cf55eb42
config_hash: e3c2679d25f6235381dfb11962fbf3d9
config_hash: 83967503e501e4199b4042d0d0f2b615
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 5.4.1 (2026-04-17)

Full Changelog: [v5.4.0...v5.4.1](https://github.com/imagekit-developer/imagekit-python/compare/v5.4.0...v5.4.1)

### Performance Improvements

* **client:** optimize file structure copying in multipart requests ([b22ab86](https://github.com/imagekit-developer/imagekit-python/commit/b22ab86e45de2bbc479942eaa27dd0c9b89cbc91))

## 5.4.0 (2026-04-13)

Full Changelog: [v5.3.0...v5.4.0](https://github.com/imagekit-developer/imagekit-python/compare/v5.3.0...v5.4.0)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "imagekitio"
version = "5.4.0"
version = "5.4.1"
description = "The official Python library for the ImageKit API"
dynamic = ["readme"]
license = "Apache-2.0"
Expand Down
56 changes: 53 additions & 3 deletions src/imagekitio/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]:
Expand Down Expand Up @@ -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": <content>}
}
}
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] == "<array>"]

# 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
1 change: 0 additions & 1 deletion src/imagekitio/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 0 additions & 15 deletions src/imagekitio/_utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,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)
Expand Down
2 changes: 1 addition & 1 deletion src/imagekitio/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

__title__ = "imagekitio"
__version__ = "5.4.0" # x-release-please-version
__version__ = "5.4.1" # x-release-please-version
13 changes: 8 additions & 5 deletions src/imagekitio/resources/beta/v2/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import httpx

from ...._files import deepcopy_with_paths
from ...._types import (
Body,
Omit,
Expand All @@ -18,7 +19,7 @@
omit,
not_given,
)
from ...._utils import extract_files, maybe_transform, deepcopy_minimal, async_maybe_transform
from ...._utils import extract_files, maybe_transform, async_maybe_transform
from ...._compat import cached_property
from ...._resource import SyncAPIResource, AsyncAPIResource
from ...._response import (
Expand Down Expand Up @@ -248,7 +249,7 @@ def upload(

timeout: Override the client-level default timeout for this request, in seconds
"""
body = deepcopy_minimal(
body = deepcopy_with_paths(
{
"file": file,
"file_name": file_name,
Expand All @@ -270,7 +271,8 @@ def upload(
"transformation": transformation,
"use_unique_file_name": use_unique_file_name,
"webhook_url": webhook_url,
}
},
[["file"]],
)
body = serialize_upload_options(body)
files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
Expand Down Expand Up @@ -503,7 +505,7 @@ async def upload(

timeout: Override the client-level default timeout for this request, in seconds
"""
body = deepcopy_minimal(
body = deepcopy_with_paths(
{
"file": file,
"file_name": file_name,
Expand All @@ -525,7 +527,8 @@ async def upload(
"transformation": transformation,
"use_unique_file_name": use_unique_file_name,
"webhook_url": webhook_url,
}
},
[["file"]],
)
body = serialize_upload_options(body)
files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
Expand Down
13 changes: 8 additions & 5 deletions src/imagekitio/resources/files/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
file_update_params,
file_upload_params,
)
from ..._files import deepcopy_with_paths
from ..._types import (
Body,
Omit,
Expand All @@ -34,7 +35,7 @@
omit,
not_given,
)
from ..._utils import extract_files, path_template, maybe_transform, deepcopy_minimal, async_maybe_transform
from ..._utils import extract_files, path_template, maybe_transform, async_maybe_transform
from .metadata import (
MetadataResource,
AsyncMetadataResource,
Expand Down Expand Up @@ -700,7 +701,7 @@ def upload(

timeout: Override the client-level default timeout for this request, in seconds
"""
body = deepcopy_minimal(
body = deepcopy_with_paths(
{
"file": file,
"file_name": file_name,
Expand All @@ -725,7 +726,8 @@ def upload(
"transformation": transformation,
"use_unique_file_name": use_unique_file_name,
"webhook_url": webhook_url,
}
},
[["file"]],
)
body = serialize_upload_options(body)
files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
Expand Down Expand Up @@ -1374,7 +1376,7 @@ async def upload(

timeout: Override the client-level default timeout for this request, in seconds
"""
body = deepcopy_minimal(
body = deepcopy_with_paths(
{
"file": file,
"file_name": file_name,
Expand All @@ -1399,7 +1401,8 @@ async def upload(
"transformation": transformation,
"use_unique_file_name": use_unique_file_name,
"webhook_url": webhook_url,
}
},
[["file"]],
)
body = serialize_upload_options(body)
files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
Expand Down
58 changes: 0 additions & 58 deletions tests/test_deepcopy.py

This file was deleted.

Loading
Loading