Skip to content

Commit 655645b

Browse files
fix(perf): skip traversing types for NotGiven values
1 parent f9fb625 commit 655645b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: src/gitpod/_utils/_transform.py

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from ._utils import (
1414
is_list,
15+
is_given,
1516
is_mapping,
1617
is_iterable,
1718
)
@@ -258,6 +259,11 @@ def _transform_typeddict(
258259
result: dict[str, object] = {}
259260
annotations = get_type_hints(expected_type, include_extras=True)
260261
for key, value in data.items():
262+
if not is_given(value):
263+
# we don't need to include `NotGiven` values here as they'll
264+
# be stripped out before the request is sent anyway
265+
continue
266+
261267
type_ = annotations.get(key)
262268
if type_ is None:
263269
# we do not have a type annotation for this field, leave it as is
@@ -415,6 +421,11 @@ async def _async_transform_typeddict(
415421
result: dict[str, object] = {}
416422
annotations = get_type_hints(expected_type, include_extras=True)
417423
for key, value in data.items():
424+
if not is_given(value):
425+
# we don't need to include `NotGiven` values here as they'll
426+
# be stripped out before the request is sent anyway
427+
continue
428+
418429
type_ = annotations.get(key)
419430
if type_ is None:
420431
# we do not have a type annotation for this field, leave it as is

Diff for: 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 gitpod._types import Base64FileInput
11+
from gitpod._types import NOT_GIVEN, Base64FileInput
1212
from gitpod._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)