Skip to content

Commit 9627bc5

Browse files
committed
Do not raise KeyError when discarding dict items
1 parent 8c57009 commit 9627bc5

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

src/graphql/error/located_error.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
__all__ = ["located_error"]
1515

16+
suppress_attribute_error = suppress(AttributeError)
17+
1618

1719
def located_error(
1820
original_error: Exception,
@@ -45,6 +47,6 @@ def located_error(
4547
except AttributeError:
4648
positions = None
4749

48-
with suppress(AttributeError):
50+
with suppress_attribute_error:
4951
nodes = original_error.nodes or nodes # type: ignore
5052
return GraphQLError(message, nodes, source, positions, path, original_error)

src/graphql/execution/execute.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ async def anext(iterator: AsyncIterator) -> Any: # noqa: A001
119119
"Middleware",
120120
]
121121

122+
suppress_exceptions = suppress(Exception)
123+
suppress_timeout_error = suppress(TimeoutError)
124+
122125

123126
# Terminology
124127
#
@@ -971,7 +974,7 @@ async def complete_async_iterator_value(
971974
and isinstance(stream.initial_count, int)
972975
and index >= stream.initial_count
973976
):
974-
with suppress(TimeoutError):
977+
with suppress_timeout_error:
975978
await wait_for(
976979
shield(
977980
self.execute_stream_async_iterator(
@@ -1709,7 +1712,7 @@ async def execute_stream_async_iterator(
17091712
incremental_data_record, None
17101713
)
17111714
if async_iterator: # pragma: no cover else
1712-
with suppress(Exception):
1715+
with suppress_exceptions:
17131716
await async_iterator.aclose() # type: ignore
17141717
# running generators cannot be closed since Python 3.8,
17151718
# so we need to remember that this iterator is already canceled

src/graphql/execution/incremental_publisher.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
from asyncio import Event, ensure_future, gather
6+
from contextlib import suppress
67
from typing import (
78
TYPE_CHECKING,
89
Any,
@@ -48,6 +49,8 @@
4849

4950
ASYNC_DELAY = 1 / 512 # wait time in seconds for deferring execution
5051

52+
suppress_key_error = suppress(KeyError)
53+
5154

5255
class FormattedIncrementalDeferResult(TypedDict, total=False):
5356
"""Formatted incremental deferred execution result"""
@@ -386,7 +389,7 @@ async def subscribe(
386389
while not is_done:
387390
released = self._released
388391
for item in released:
389-
if item in pending:
392+
with suppress_key_error:
390393
del pending[item]
391394
self._released = {}
392395

@@ -487,7 +490,8 @@ def filter(
487490

488491
self._delete(child)
489492
parent = child.parent_context or self._initial_result
490-
del parent.children[child]
493+
with suppress_key_error:
494+
del parent.children[child]
491495

492496
if isinstance(child, StreamItemsRecord):
493497
async_iterator = child.async_iterator
@@ -501,10 +505,7 @@ def filter(
501505

502506
def _trigger(self) -> None:
503507
self._resolve.set()
504-
self._reset()
505-
506-
def _reset(self) -> None:
507-
self._resolve.clear()
508+
self._resolve = Event()
508509

509510
def _introduce(self, item: IncrementalDataRecord) -> None:
510511
self._pending[item] = None
@@ -520,8 +521,10 @@ def _push(self, item: IncrementalDataRecord) -> None:
520521
self._trigger()
521522

522523
def _delete(self, item: IncrementalDataRecord) -> None:
523-
del self._released[item]
524-
del self._pending[item]
524+
with suppress_key_error:
525+
del self._released[item]
526+
with suppress_key_error:
527+
del self._pending[item]
525528
self._trigger()
526529

527530
def _get_incremental_result(

tests/execution/test_stream.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ def get_friends(_info):
12341234
{
12351235
"incremental": [
12361236
{
1237-
"items": [None],
1237+
"items": None,
12381238
"path": ["nonNullFriendList", 1],
12391239
"errors": [
12401240
{
@@ -1292,7 +1292,7 @@ def get_friends(_info):
12921292
{
12931293
"message": "Oops",
12941294
"locations": [{"line": 4, "column": 17}],
1295-
"path": ["friendList", 1, "nonNullName"],
1295+
"path": ["nonNullFriendList", 1, "nonNullName"],
12961296
},
12971297
],
12981298
},
@@ -1406,13 +1406,13 @@ async def get_friends(_info):
14061406
{
14071407
"incremental": [
14081408
{
1409-
"items": [None],
1409+
"items": None,
14101410
"path": ["nonNullFriendList", 1],
14111411
"errors": [
14121412
{
14131413
"message": "Oops",
14141414
"locations": [{"line": 4, "column": 17}],
1415-
"path": ["friendList", 1, "nonNullName"],
1415+
"path": ["nonNullFriendList", 1, "nonNullName"],
14161416
},
14171417
],
14181418
},
@@ -1579,8 +1579,9 @@ async def friend_list(_info):
15791579
"path": ["nestedObject", "nestedFriendList", 0],
15801580
},
15811581
],
1582-
"hasNext": False,
1582+
"hasNext": True,
15831583
},
1584+
{"hasNext": False},
15841585
]
15851586

15861587
@pytest.mark.asyncio()

0 commit comments

Comments
 (0)