Skip to content

Commit a183c96

Browse files
release: 1.21.1 (#651)
* fix(perf): skip traversing types for NotGiven values * fix(perf): optimize some hot paths * release: 1.21.1 --------- Co-authored-by: stainless-app[bot] <142633134+stainless-app[bot]@users.noreply.github.com>
1 parent 43ea95e commit a183c96

File tree

7 files changed

+46
-5
lines changed

7 files changed

+46
-5
lines changed

.release-please-manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "1.21.0"
2+
".": "1.21.1"
33
}

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 1.21.1 (2025-04-11)
4+
5+
Full Changelog: [v1.21.0...v1.21.1](https://github.com/Finch-API/finch-api-python/compare/v1.21.0...v1.21.1)
6+
7+
### Bug Fixes
8+
9+
* **perf:** optimize some hot paths ([4fe846e](https://github.com/Finch-API/finch-api-python/commit/4fe846ea14d90fb142eb1d2b3d7fa259326604f8))
10+
* **perf:** skip traversing types for NotGiven values ([c6df147](https://github.com/Finch-API/finch-api-python/commit/c6df1471d194dcad32a5ab771f8a7fe19e904f9f))
11+
312
## 1.21.0 (2025-04-09)
413

514
Full Changelog: [v1.20.1...v1.21.0](https://github.com/Finch-API/finch-api-python/compare/v1.20.1...v1.21.0)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "finch-api"
3-
version = "1.21.0"
3+
version = "1.21.1"
44
description = "The official Python library for the Finch API"
55
dynamic = ["readme"]
66
license = "Apache-2.0"

src/finch/_utils/_transform.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import pathlib
66
from typing import Any, Mapping, TypeVar, cast
77
from datetime import date, datetime
8-
from typing_extensions import Literal, get_args, override, get_type_hints
8+
from typing_extensions import Literal, get_args, override, get_type_hints as _get_type_hints
99

1010
import anyio
1111
import pydantic
1212

1313
from ._utils import (
1414
is_list,
15+
is_given,
16+
lru_cache,
1517
is_mapping,
1618
is_iterable,
1719
)
@@ -108,6 +110,7 @@ class Params(TypedDict, total=False):
108110
return cast(_T, transformed)
109111

110112

113+
@lru_cache(maxsize=8096)
111114
def _get_annotated_type(type_: type) -> type | None:
112115
"""If the given type is an `Annotated` type then it is returned, if not `None` is returned.
113116
@@ -258,6 +261,11 @@ def _transform_typeddict(
258261
result: dict[str, object] = {}
259262
annotations = get_type_hints(expected_type, include_extras=True)
260263
for key, value in data.items():
264+
if not is_given(value):
265+
# we don't need to include `NotGiven` values here as they'll
266+
# be stripped out before the request is sent anyway
267+
continue
268+
261269
type_ = annotations.get(key)
262270
if type_ is None:
263271
# we do not have a type annotation for this field, leave it as is
@@ -415,10 +423,25 @@ async def _async_transform_typeddict(
415423
result: dict[str, object] = {}
416424
annotations = get_type_hints(expected_type, include_extras=True)
417425
for key, value in data.items():
426+
if not is_given(value):
427+
# we don't need to include `NotGiven` values here as they'll
428+
# be stripped out before the request is sent anyway
429+
continue
430+
418431
type_ = annotations.get(key)
419432
if type_ is None:
420433
# we do not have a type annotation for this field, leave it as is
421434
result[key] = value
422435
else:
423436
result[_maybe_transform_key(key, type_)] = await _async_transform_recursive(value, annotation=type_)
424437
return result
438+
439+
440+
@lru_cache(maxsize=8096)
441+
def get_type_hints(
442+
obj: Any,
443+
globalns: dict[str, Any] | None = None,
444+
localns: Mapping[str, Any] | None = None,
445+
include_extras: bool = False,
446+
) -> dict[str, Any]:
447+
return _get_type_hints(obj, globalns=globalns, localns=localns, include_extras=include_extras)

src/finch/_utils/_typing.py

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
get_origin,
1414
)
1515

16+
from ._utils import lru_cache
1617
from .._types import InheritsGeneric
1718
from .._compat import is_union as _is_union
1819

@@ -66,6 +67,7 @@ def is_type_alias_type(tp: Any, /) -> TypeIs[typing_extensions.TypeAliasType]:
6667

6768

6869
# Extracts T from Annotated[T, ...] or from Required[Annotated[T, ...]]
70+
@lru_cache(maxsize=8096)
6971
def strip_annotated_type(typ: type) -> type:
7072
if is_required_type(typ) or is_annotated_type(typ):
7173
return strip_annotated_type(cast(type, get_args(typ)[0]))

src/finch/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
__title__ = "finch"
4-
__version__ = "1.21.0" # x-release-please-version
4+
__version__ = "1.21.1" # x-release-please-version

tests/test_transform.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from finch._types import Base64FileInput
11+
from finch._types import NOT_GIVEN, Base64FileInput
1212
from finch._utils import (
1313
PropertyInfo,
1414
transform as _transform,
@@ -444,3 +444,10 @@ async def test_transform_skipping(use_async: bool) -> None:
444444
# iterables of ints are converted to a list
445445
data = iter([1, 2, 3])
446446
assert await transform(data, Iterable[int], use_async) == [1, 2, 3]
447+
448+
449+
@parametrize
450+
@pytest.mark.asyncio
451+
async def test_strips_notgiven(use_async: bool) -> None:
452+
assert await transform({"foo_bar": "bar"}, Foo1, use_async) == {"fooBar": "bar"}
453+
assert await transform({"foo_bar": NOT_GIVEN}, Foo1, use_async) == {}

0 commit comments

Comments
 (0)