Skip to content

Commit 9598515

Browse files
committed
Modernize code to make ruff happy
1 parent 9e16a0b commit 9598515

File tree

92 files changed

+531
-467
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+531
-467
lines changed

docs/conf.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,10 @@
116116
"validation": ["rules", "validation_context"],
117117
}
118118

119-
# be nitpicky (handle all possible problems in on_missing_reference)
119+
# be nitpicky
120120
nitpicky = True
121-
nitpick_ignore = [
122-
# TypeVars and internal types that can't be resolved
123-
("py:obj", "graphql.pyutils.ref_map.K"),
124-
("py:obj", "graphql.pyutils.ref_map.V"),
125-
("py:obj", "graphql.pyutils.awaitable_or_value.T"),
126-
("py:obj", "graphql.pyutils.boxed_awaitable_or_value.T"),
127-
("py:obj", "graphql.pyutils.ref_set.T"),
128-
("py:obj", "graphql.type.definition.T"),
129-
("py:obj", "graphql.type.definition.GT_co"),
130-
("py:obj", "graphql.type.definition.GNT_co"),
131-
("py:obj", "graphql.type.definition.TContext"),
132-
]
133-
134-
suppress_warnings = ["ref.class", "ref.python"]
121+
# but not too nitpicky
122+
suppress_warnings = ["ref.class", "ref.obj", "ref.python"]
135123

136124

137125
# The reST default role (used for this markup: `text`) to use for all

pyproject.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ select = [
8181
"A", # flake8-builtins
8282
"ANN", # flake8-annotations
8383
"ARG", # flake8-unused-arguments
84+
"ASYNC", # flake8-async
8485
"B", # flake8-bugbear
8586
"BLE", # flake8-blind-except
8687
"C4", # flake8-comprehensions
@@ -101,6 +102,7 @@ select = [
101102
"INT", # flake8-gettext
102103
"ISC", # flake8-implicit-str-concat
103104
"N", # pep8-naming
105+
"PERF", # perflint
104106
"PGH", # pygrep-hooks
105107
"PIE", # flake8-pie
106108
"PL", # Pylint
@@ -110,6 +112,7 @@ select = [
110112
"Q", # flake8-quotes
111113
"RET", # flake8-return
112114
"RSE", # flake8-raise
115+
"FURB", # refurb
113116
"RUF", # Ruff-specific rules
114117
"S", # flake8-bandit
115118
"SLF", # flake8-self
@@ -119,8 +122,7 @@ select = [
119122
"TCH", # flake8-type-checking
120123
"TID", # flake8-tidy-imports
121124
"TRY", # tryceratops
122-
# TODO: enable again after upgradesc
123-
#"UP", # pyupgrade
125+
"UP", # pyupgrade
124126
"W", # pycodestyle
125127
"YTT", # flake8-2020
126128
]

src/graphql/error/graphql_error.py

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

55
from sys import exc_info
6-
from typing import TYPE_CHECKING, Any, Collection, Dict, TypeAlias, TypedDict
6+
from typing import TYPE_CHECKING, Any, TypeAlias, TypedDict
77

88
if TYPE_CHECKING:
9+
from collections.abc import Collection
10+
911
from ..language.ast import Node
1012
from ..language.location import (
1113
FormattedSourceLocation,
@@ -17,7 +19,7 @@
1719

1820

1921
# Custom extensions
20-
GraphQLErrorExtensions: TypeAlias = Dict[str, Any]
22+
GraphQLErrorExtensions: TypeAlias = dict[str, Any]
2123
# Use a unique identifier name for your extension, for example the name of
2224
# your library or project. Do not use a shortened identifier as this increases
2325
# the risk of conflicts. We recommend you add at most one extension key,
@@ -176,13 +178,12 @@ def __str__(self) -> str:
176178
output = [self.message]
177179

178180
if self.nodes:
179-
for node in self.nodes:
180-
if node.loc:
181-
output.append(print_location(node.loc))
181+
output.extend(print_location(node.loc) for node in self.nodes if node.loc)
182182
elif self.source and self.locations:
183183
source = self.source
184-
for location in self.locations:
185-
output.append(print_source_location(source, location))
184+
output.extend(
185+
print_source_location(source, location) for location in self.locations
186+
)
186187

187188
return "\n\n".join(output)
188189

src/graphql/error/located_error.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
from __future__ import annotations
44

55
from contextlib import suppress
6-
from typing import TYPE_CHECKING, Collection
6+
from typing import TYPE_CHECKING
77

88
from ..language.source import Source, is_source
99
from ..pyutils import inspect
1010
from .graphql_error import GraphQLError
1111

1212
if TYPE_CHECKING:
13+
from collections.abc import Collection
14+
1315
from ..language.ast import Node
1416

1517
__all__ = ["located_error"]

src/graphql/execution/async_iterables.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@
22

33
from __future__ import annotations
44

5+
from collections.abc import AsyncGenerator, AsyncIterable, Awaitable, Callable
56
from contextlib import AbstractAsyncContextManager, suppress
6-
from typing import (
7-
AsyncGenerator,
8-
AsyncIterable,
9-
Awaitable,
10-
Callable,
11-
Generic,
12-
TypeVar,
13-
Union,
14-
)
7+
from typing import Generic, TypeVar
158

169
__all__ = ["aclosing", "map_async_iterable"]
1710

1811
T = TypeVar("T")
1912
V = TypeVar("V")
2013

21-
AsyncIterableOrGenerator = Union[AsyncGenerator[T, None], AsyncIterable[T]]
14+
AsyncIterableOrGenerator = AsyncGenerator[T, None] | AsyncIterable[T]
2215

2316
suppress_exceptions = suppress(Exception)
2417

src/graphql/execution/execute.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,24 @@
77
ensure_future,
88
sleep,
99
)
10-
from contextlib import suppress
11-
from copy import copy
12-
from typing import (
13-
TYPE_CHECKING,
14-
Any,
10+
from collections.abc import (
1511
AsyncGenerator,
1612
AsyncIterable,
1713
AsyncIterator,
1814
Awaitable,
1915
Callable,
20-
Generic,
2116
Iterable,
22-
List,
2317
Mapping,
24-
NamedTuple,
25-
Optional,
2618
Sequence,
27-
Tuple,
19+
)
20+
from contextlib import suppress
21+
from copy import copy
22+
from typing import (
23+
TYPE_CHECKING,
24+
Any,
25+
Generic,
26+
NamedTuple,
2827
TypeVar,
29-
Union,
3028
cast,
3129
)
3230

@@ -147,7 +145,7 @@
147145
# 3) inline fragment "spreads" e.g. "...on Type { a }"
148146

149147

150-
Middleware: TypeAlias = Optional[Union[Tuple, List, MiddlewareManager]]
148+
Middleware: TypeAlias = tuple | list | MiddlewareManager | None
151149

152150

153151
class StreamUsage(NamedTuple):
@@ -1339,7 +1337,7 @@ async def await_complete_object_value() -> Any:
13391337
return value # pragma: no cover
13401338

13411339
return await_complete_object_value()
1342-
runtime_type = cast("Optional[str]", runtime_type)
1340+
runtime_type = cast("str | None", runtime_type)
13431341

13441342
return self.complete_object_value(
13451343
self.ensure_valid_runtime_type(

src/graphql/execution/incremental_graph.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,7 @@
1111
isfuture,
1212
sleep,
1313
)
14-
from typing import (
15-
TYPE_CHECKING,
16-
Any,
17-
AsyncGenerator,
18-
Awaitable,
19-
Generator,
20-
Iterable,
21-
Sequence,
22-
Union,
23-
cast,
24-
)
14+
from typing import TYPE_CHECKING, Any, cast
2515

2616
from ..pyutils import BoxedAwaitableOrValue, Undefined
2717
from .types import (
@@ -33,6 +23,7 @@
3323
)
3424

3525
if TYPE_CHECKING:
26+
from collections.abc import AsyncGenerator, Awaitable, Generator, Iterable, Sequence
3627
from typing import TypeGuard
3728

3829
from ..error.graphql_error import GraphQLError
@@ -71,7 +62,7 @@ def __init__(self, deferred_fragment_record: DeferredFragmentRecord) -> None:
7162
self.children = []
7263

7364

74-
SubsequentResultNode = Union[DeferredFragmentNode, StreamRecord]
65+
SubsequentResultNode = DeferredFragmentNode | StreamRecord
7566

7667

7768
def is_deferred_fragment_node(

src/graphql/execution/incremental_publisher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
from typing import (
88
TYPE_CHECKING,
99
Any,
10-
AsyncGenerator,
1110
NamedTuple,
1211
Protocol,
13-
Sequence,
1412
cast,
1513
)
1614

@@ -29,6 +27,8 @@
2927
)
3028

3129
if TYPE_CHECKING:
30+
from collections.abc import AsyncGenerator, Sequence
31+
3232
from ..error import GraphQLError
3333
from .types import (
3434
CancellableStreamRecord,

src/graphql/execution/middleware.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
from __future__ import annotations
44

5+
from collections.abc import Callable, Iterator
56
from functools import partial, reduce
67
from inspect import isfunction
7-
from typing import Any, Callable, Iterator, TypeAlias
8+
from typing import Any, TypeAlias
89

910
__all__ = ["MiddlewareManager"]
1011

src/graphql/execution/types.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
from __future__ import annotations
44

5+
from collections.abc import AsyncGenerator, Awaitable, Callable, Iterator
56
from typing import (
67
TYPE_CHECKING,
78
Any,
8-
AsyncGenerator,
9-
Awaitable,
10-
Callable,
11-
Iterator,
129
NamedTuple,
1310
TypeAlias,
1411
TypedDict,
1512
TypeVar,
16-
Union,
1713
)
1814

1915
from ..pyutils import BoxedAwaitableOrValue, Undefined
@@ -573,11 +569,11 @@ class FormattedIncrementalStreamResult(TypedDict):
573569

574570
T = TypeVar("T") # declare T for generic aliases
575571

576-
IncrementalResult: TypeAlias = Union[IncrementalDeferResult, IncrementalStreamResult]
572+
IncrementalResult: TypeAlias = IncrementalDeferResult | IncrementalStreamResult
577573

578-
FormattedIncrementalResult: TypeAlias = Union[
579-
FormattedIncrementalDeferResult, FormattedIncrementalStreamResult
580-
]
574+
FormattedIncrementalResult: TypeAlias = (
575+
FormattedIncrementalDeferResult | FormattedIncrementalStreamResult
576+
)
581577

582578

583579
class PendingResult: # noqa: PLW1641
@@ -758,10 +754,10 @@ def is_non_reconcilable_deferred_grouped_field_set_result(
758754
)
759755

760756

761-
DeferredGroupedFieldSetResult: TypeAlias = Union[
762-
ReconcilableDeferredGroupedFieldSetResult,
763-
NonReconcilableDeferredGroupedFieldSetResult,
764-
]
757+
DeferredGroupedFieldSetResult: TypeAlias = (
758+
ReconcilableDeferredGroupedFieldSetResult
759+
| NonReconcilableDeferredGroupedFieldSetResult
760+
)
765761

766762

767763
def is_deferred_grouped_field_set_result(
@@ -775,9 +771,9 @@ def is_deferred_grouped_field_set_result(
775771
)
776772

777773

778-
ThunkIncrementalResult: TypeAlias = Union[
779-
BoxedAwaitableOrValue[T], Callable[[], BoxedAwaitableOrValue[T]]
780-
]
774+
ThunkIncrementalResult: TypeAlias = (
775+
BoxedAwaitableOrValue[T] | Callable[[], BoxedAwaitableOrValue[T]]
776+
)
781777

782778

783779
class DeferredGroupedFieldSetRecord:
@@ -873,7 +869,7 @@ def __repr__(self) -> str:
873869
return f"{name}({', '.join(args)})"
874870

875871

876-
SubsequentResultRecord: TypeAlias = Union[DeferredFragmentRecord, StreamRecord]
872+
SubsequentResultRecord: TypeAlias = DeferredFragmentRecord | StreamRecord
877873

878874

879875
class CancellableStreamRecord(StreamRecord):
@@ -910,8 +906,8 @@ class StreamItemsResult(NamedTuple):
910906
errors: list[GraphQLError] | None = None
911907

912908

913-
IncrementalDataRecord: TypeAlias = Union[DeferredGroupedFieldSetRecord, StreamRecord]
909+
IncrementalDataRecord: TypeAlias = DeferredGroupedFieldSetRecord | StreamRecord
914910

915-
IncrementalDataRecordResult: TypeAlias = Union[
916-
DeferredGroupedFieldSetResult, StreamItemsResult
917-
]
911+
IncrementalDataRecordResult: TypeAlias = (
912+
DeferredGroupedFieldSetResult | StreamItemsResult
913+
)

0 commit comments

Comments
 (0)