Skip to content

Commit 0dacf4f

Browse files
committed
Fix mypy 2.3.0 type errors in the test suite
mypy 2.3.0 (released 2026-07-13) is stricter and surfaces 7 errors in the test suite on unmodified main (CI's `mypy<3,>=2.1` floats to it), failing the Lint job on every open PR. All test-only, no behavioural change. - tests/utils/assert_equal_awaitables_or_values.py: the awaitable branch returns a coroutine where the declared TypeVar `T` is expected; cast it (the function returns an awaitable iff its inputs are awaitables). - tests/execution/test_abstract.py: the conditionally-defined `is_type_of` and `type_error` (sync vs async variants) now must have identical signatures; pre-declare each with an explicit `Callable[..., Any]`. - tests/execution/test_stream.py: `name` is assigned either a coroutine (`throw()`) or a `str`; annotate it `Any`. - tests/execution/test_middleware.py: the awaited `subscribe(...)` item is inferred as `object`; cast it to `ExecutionResult` before reading `.data`. mypy src tests, ruff check src tests and ruff format --check src tests are all clean under the pinned tool versions (mypy 2.3.0, ruff 0.15.21).
1 parent 56d4338 commit 0dacf4f

4 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎tests/execution/test_abstract.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, NamedTuple
3+
from typing import TYPE_CHECKING, Any, NamedTuple
44

55
import pytest
66

@@ -19,6 +19,9 @@
1919
)
2020
from graphql.utilities import build_schema
2121

22+
if TYPE_CHECKING:
23+
from collections.abc import Callable
24+
2225

2326
def sync_and_async(spec):
2427
"""Decorator for running a test synchronously and asynchronously."""
@@ -51,6 +54,7 @@ async def execute_query(
5154

5255
def get_is_type_of(type_, sync=True):
5356
"""Get a sync or async is_type_of function for the given type."""
57+
is_type_of: Callable[[Any, Any], Any]
5458
if sync:
5559

5660
def is_type_of(obj, _info):
@@ -67,6 +71,7 @@ async def is_type_of(obj, _info):
6771
def get_type_error(sync=True):
6872
"""Get a sync or async is_type_of or type resolver function that raises an error."""
6973
error = RuntimeError("We are testing this error")
74+
type_error: Callable[..., Any]
7075
if sync:
7176

7277
def type_error(*_args):

‎tests/execution/test_middleware.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
import pytest
66

7-
from graphql.execution import Middleware, MiddlewareManager, execute, subscribe
7+
from graphql.execution import (
8+
ExecutionResult,
9+
Middleware,
10+
MiddlewareManager,
11+
execute,
12+
subscribe,
13+
)
814
from graphql.language.parser import parse
915
from graphql.type import GraphQLField, GraphQLObjectType, GraphQLSchema, GraphQLString
1016

@@ -262,9 +268,9 @@ async def reverse_middleware(next_, value, info, **kwargs):
262268
middleware=MiddlewareManager(reverse_middleware),
263269
)
264270
assert inspect.isasyncgen(agen)
265-
data = (await agen.__anext__()).data
271+
data = cast("ExecutionResult", await agen.__anext__()).data
266272
assert data == {"bar": "rab"}
267-
data = (await agen.__anext__()).data
273+
data = cast("ExecutionResult", await agen.__anext__()).data
268274
assert data == {"bar": "foo"}
269275

270276
def describe_without_manager():

‎tests/execution/test_stream.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,7 @@ def __aiter__(self):
16911691
async def __anext__(self):
16921692
count = self.count
16931693
self.count += 1
1694+
name: Any
16941695
if count == 1:
16951696
name = throw()
16961697
else:
@@ -1757,6 +1758,7 @@ async def __anext__(self):
17571758
raise StopAsyncIteration # pragma: no cover
17581759
count = self.count
17591760
self.count += 1
1761+
name: Any
17601762
if count == 1:
17611763
name = throw()
17621764
else:

‎tests/utils/assert_equal_awaitables_or_values.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def assert_equal_awaitables_or_values(*items: T) -> T:
2323
async def assert_matching_awaitables():
2424
return assert_matching_values(*(await asyncio.gather(*awaitable_items)))
2525

26-
return assert_matching_awaitables()
26+
return cast("T", assert_matching_awaitables())
2727

2828
if all(not is_awaitable(item) for item in items):
2929
return assert_matching_values(*items)

0 commit comments

Comments
 (0)