Skip to content

Commit 275bed0

Browse files
authored
Pyupgrade (#1972)
1 parent 28f14f8 commit 275bed0

File tree

120 files changed

+400
-481
lines changed

Some content is hidden

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

120 files changed

+400
-481
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jobs:
7373
strategy:
7474
fail-fast: false
7575
matrix:
76-
python-version: ['3.9', '3.10', '3.11', '3.12']
76+
python-version: ['3.10', '3.11', '3.12', '3.13']
7777
shard: [0, 1, 2, 3]
7878

7979
steps:

returns/_internal/futures/_future.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import TYPE_CHECKING, Awaitable, Callable, TypeVar
1+
from collections.abc import Awaitable, Callable
2+
from typing import TYPE_CHECKING, TypeVar
23

34
from returns.io import IO
45
from returns.primitives.hkt import Kind1, dekind

returns/_internal/futures/_future_result.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import TYPE_CHECKING, Any, Awaitable, Callable, TypeVar
1+
from collections.abc import Awaitable, Callable
2+
from typing import TYPE_CHECKING, Any, TypeVar
23

34
from returns.io import IO, IOResult
45
from returns.primitives.hkt import Kind2, dekind

returns/_internal/futures/_reader_future_result.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import TYPE_CHECKING, Awaitable, Callable, TypeVar
1+
from collections.abc import Awaitable, Callable
2+
from typing import TYPE_CHECKING, TypeVar
23

34
from returns.primitives.hkt import Kind3, dekind
45
from returns.result import Result, Success

returns/_internal/pipeline/managed.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Callable, TypeVar
1+
from collections.abc import Callable
2+
from typing import TypeVar
23

34
from returns.interfaces.specific.ioresult import IOResultLikeN
45
from returns.primitives.hkt import Kinded, KindN, kinded

returns/context/requires_context.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Any, Callable, ClassVar, TypeVar, final
4-
5-
from typing_extensions import TypeAlias
3+
from collections.abc import Callable
4+
from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias, TypeVar, final
65

76
from returns.functions import identity
87
from returns.future import FutureResult

returns/context/requires_context_future_result.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
from __future__ import annotations
22

3-
from typing import (
4-
TYPE_CHECKING,
5-
Any,
6-
Awaitable,
7-
Callable,
8-
ClassVar,
9-
TypeVar,
10-
final,
11-
)
12-
13-
from typing_extensions import TypeAlias
3+
from collections.abc import Awaitable, Callable
4+
from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias, TypeVar, final
145

156
from returns._internal.futures import _reader_future_result
167
from returns.context import NoDeps

returns/context/requires_context_ioresult.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Any, Callable, ClassVar, TypeVar, final
4-
5-
from typing_extensions import TypeAlias
3+
from collections.abc import Callable
4+
from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias, TypeVar, final
65

76
from returns.context import NoDeps
87
from returns.interfaces.specific import reader_ioresult

returns/context/requires_context_result.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Any, Callable, ClassVar, TypeVar, final
4-
5-
from typing_extensions import TypeAlias
3+
from collections.abc import Callable
4+
from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias, TypeVar, final
65

76
from returns.context import NoDeps
87
from returns.interfaces.specific import reader_result

returns/contrib/hypothesis/_entrypoint.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
from __future__ import annotations
1212

13-
from typing import TYPE_CHECKING, Any, Callable, Sequence, Type, TypeVar
13+
from collections.abc import Callable, Sequence
14+
from typing import TYPE_CHECKING, Any, TypeVar
1415

1516
if TYPE_CHECKING:
1617
from returns.primitives.laws import Lawful
@@ -33,7 +34,7 @@ def _setup_hook() -> None:
3334
from returns.result import Result
3435

3536
def factory(
36-
container_type: Type[_Inst],
37+
container_type: type[_Inst],
3738
) -> Callable[[Any], st.SearchStrategy[_Inst]]:
3839
def decorator(thing: Any) -> st.SearchStrategy[_Inst]:
3940
from returns.contrib.hypothesis.containers import (
@@ -44,7 +45,7 @@ def decorator(thing: Any) -> st.SearchStrategy[_Inst]:
4445

4546
#: Our types that we register in hypothesis
4647
#: to be working with ``st.from_type``
47-
registered_types: Sequence[Type[Lawful]] = (
48+
registered_types: Sequence[type[Lawful]] = (
4849
Result,
4950
Maybe,
5051
IO,

returns/contrib/hypothesis/containers.py

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

3-
from typing import TYPE_CHECKING, Any, Callable, TypeVar
3+
from collections.abc import Callable
4+
from typing import TYPE_CHECKING, Any, TypeVar
45

56
from hypothesis import strategies as st
67

returns/contrib/hypothesis/laws.py

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import inspect
2+
from collections.abc import Callable, Iterator
23
from contextlib import ExitStack, contextmanager
3-
from typing import (
4-
Any,
5-
Callable,
6-
Dict,
7-
Iterator,
8-
NamedTuple,
9-
Optional,
10-
Type,
11-
TypeVar,
12-
final,
13-
)
4+
from typing import Any, NamedTuple, TypeVar, final
145

156
import pytest
167
from hypothesis import given
@@ -26,14 +17,14 @@
2617
class _Settings(NamedTuple):
2718
"""Settings that we provide to an end user."""
2819

29-
settings_kwargs: Dict[str, Any]
20+
settings_kwargs: dict[str, Any]
3021
use_init: bool
3122

3223

3324
def check_all_laws(
34-
container_type: Type[Lawful],
25+
container_type: type[Lawful],
3526
*,
36-
settings_kwargs: Optional[Dict[str, Any]] = None,
27+
settings_kwargs: dict[str, Any] | None = None,
3728
use_init: bool = False,
3829
) -> None:
3930
"""
@@ -79,7 +70,7 @@ def check_all_laws(
7970

8071
@contextmanager
8172
def container_strategies(
82-
container_type: Type[Lawful],
73+
container_type: type[Lawful],
8374
*,
8475
settings: _Settings,
8576
) -> Iterator[None]:
@@ -123,7 +114,7 @@ def container_strategies(
123114

124115
@contextmanager
125116
def register_container(
126-
container_type: Type['Lawful'],
117+
container_type: type['Lawful'],
127118
*,
128119
use_init: bool,
129120
) -> Iterator[None]:
@@ -160,30 +151,25 @@ def factory(thing) -> st.SearchStrategy:
160151
if len(thing.__args__) == 1
161152
else (lambda *args, **kwargs: None)
162153
)
154+
return_type = thing.__args__[-1]
163155
return st.functions(
164156
like=like,
165-
returns=st.from_type(thing.__args__[-1]),
157+
returns=st.from_type(
158+
return_type
159+
if return_type is not None
160+
else type(None),
161+
),
166162
pure=True,
167163
)
168164

169-
callable_type = _get_callable_type()
170-
used = types._global_type_lookup[callable_type]
171-
st.register_type_strategy(callable_type, factory)
165+
used = types._global_type_lookup[Callable] # type: ignore[index]
166+
st.register_type_strategy(Callable, factory) # type: ignore[arg-type]
172167

173168
try:
174169
yield
175170
finally:
176-
types._global_type_lookup.pop(callable_type)
177-
st.register_type_strategy(callable_type, used)
178-
179-
180-
def _get_callable_type() -> Any:
181-
# Helper to accommodate changes in `hypothesis@6.79.0`
182-
if Callable.__origin__ in types._global_type_lookup: # type: ignore
183-
return Callable.__origin__ # type: ignore
184-
elif Callable in types._global_type_lookup: # type: ignore
185-
return Callable
186-
raise RuntimeError('Failed to find Callable type strategy')
171+
types._global_type_lookup.pop(Callable) # type: ignore[call-overload]
172+
st.register_type_strategy(Callable, used) # type: ignore[arg-type]
187173

188174

189175
@contextmanager
@@ -242,7 +228,7 @@ def _clean_caches() -> None:
242228

243229

244230
def _run_law(
245-
container_type: Type[Lawful],
231+
container_type: type[Lawful],
246232
law: Law,
247233
*,
248234
settings: _Settings,
@@ -263,8 +249,8 @@ def factory(source: st.DataObject) -> None:
263249

264250

265251
def _create_law_test_case(
266-
container_type: Type[Lawful],
267-
interface: Type[Lawful],
252+
container_type: type[Lawful],
253+
interface: type[Lawful],
268254
law: Law,
269255
*,
270256
settings: _Settings,

returns/contrib/mypy/_features/curry.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from collections.abc import Iterator
12
from itertools import groupby, product
23
from operator import itemgetter
3-
from typing import Iterator, List, Optional, Tuple, cast, final
4+
from typing import cast, final
45

56
from mypy.nodes import ARG_STAR, ARG_STAR2
67
from mypy.plugin import FunctionContext
@@ -15,7 +16,7 @@
1516
)
1617

1718
#: Raw material to build `_ArgTree`.
18-
_RawArgTree = List[List[List[FuncArg]]]
19+
_RawArgTree = list[list[list[FuncArg]]]
1920

2021

2122
def analyze(ctx: FunctionContext) -> MypyType:
@@ -34,9 +35,9 @@ def analyze(ctx: FunctionContext) -> MypyType:
3435
class _ArgTree:
3536
"""Represents a node in tree of arguments."""
3637

37-
def __init__(self, case: Optional[CallableType]) -> None:
38+
def __init__(self, case: CallableType | None) -> None:
3839
self.case = case
39-
self.children: List['_ArgTree'] = []
40+
self.children: list['_ArgTree'] = []
4041

4142

4243
@final
@@ -62,7 +63,7 @@ def __init__(self, original: CallableType, ctx: FunctionContext) -> None:
6263
"""
6364
self._original = original
6465
self._ctx = ctx
65-
self._overloads: List[CallableType] = []
66+
self._overloads: list[CallableType] = []
6667
self._args = FuncArg.from_callable(self._original)
6768

6869
# We need to get rid of generics here.
@@ -112,7 +113,7 @@ def _build_argtree(
112113
"""
113114
def factory(
114115
args: _RawArgTree,
115-
) -> Iterator[Tuple[List[FuncArg], _RawArgTree]]:
116+
) -> Iterator[tuple[list[FuncArg], _RawArgTree]]:
116117
if not args or not args[0]:
117118
return # we have reached an end of arguments
118119
yield from (
@@ -159,7 +160,7 @@ def _build_overloads_from_argtree(self, argtree: _ArgTree) -> None:
159160
else: # Root is reached, we need to save the result:
160161
self._overloads.append(child.case)
161162

162-
def _slices(self, source: List[FuncArg]) -> Iterator[List[List[FuncArg]]]:
163+
def _slices(self, source: list[FuncArg]) -> Iterator[list[list[FuncArg]]]:
163164
"""
164165
Generate all possible slices of a source list.
165166

returns/contrib/mypy/_features/do_notation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Final, Optional, Tuple
1+
from typing import Final
22

33
from mypy.maptype import map_instance_to_supertype
44
from mypy.nodes import Expression, GeneratorExpr, TypeInfo
@@ -86,7 +86,7 @@ def _try_fetch_error_type(
8686
type_info: TypeInfo,
8787
seq: Expression,
8888
ctx: MethodContext,
89-
) -> Optional[MypyType]:
89+
) -> MypyType | None:
9090
inst = Instance(
9191
type_info,
9292
[
@@ -110,7 +110,7 @@ def _try_fetch_error_type(
110110
def _extract_error_type(
111111
typ: MypyType,
112112
type_info: TypeInfo,
113-
) -> Tuple[bool, Optional[MypyType]]:
113+
) -> tuple[bool, MypyType | None]:
114114
typ = get_proper_type(typ)
115115
if isinstance(typ, Instance):
116116
return True, _decide_error_type(
@@ -129,7 +129,7 @@ def _extract_error_type(
129129
return False, None
130130

131131

132-
def _decide_error_type(typ: Instance) -> Optional[MypyType]:
132+
def _decide_error_type(typ: Instance) -> MypyType | None:
133133
if len(typ.args) < 2:
134134
return None
135135
if isinstance(get_proper_type(typ.args[1]), AnyType):

returns/contrib/mypy/_features/kind.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
from collections.abc import Sequence
12
from enum import Enum, unique
2-
from typing import Optional, Sequence, Tuple
33

44
from mypy.checkmember import analyze_member_access
55
from mypy.plugin import (
@@ -159,8 +159,8 @@ class _KindErrors(str, Enum): # noqa: WPS600
159159

160160
def _crop_kind_args(
161161
kind: Instance,
162-
limit: Optional[Sequence[MypyType]] = None,
163-
) -> Tuple[MypyType, ...]:
162+
limit: Sequence[MypyType] | None = None,
163+
) -> tuple[MypyType, ...]:
164164
"""Returns the correct amount of type arguments for a kind."""
165165
if limit is None:
166166
limit = kind.args[0].args # type: ignore

returns/contrib/mypy/_features/partial.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Final, Iterator, List, Optional, Tuple, final
1+
from collections.abc import Iterator
2+
from typing import Final, final
23

34
from mypy.nodes import ARG_STAR, ARG_STAR2
45
from mypy.plugin import FunctionContext
@@ -104,7 +105,7 @@ def __init__(
104105
self,
105106
default_return_type: FunctionLike,
106107
original: FunctionLike,
107-
applied_args: List[FuncArg],
108+
applied_args: list[FuncArg],
108109
ctx: FunctionContext,
109110
) -> None:
110111
"""
@@ -122,8 +123,8 @@ def __init__(
122123
self._applied_args = applied_args
123124
self._ctx = ctx
124125

125-
self._case_functions: List[CallableType] = []
126-
self._fallbacks: List[CallableType] = []
126+
self._case_functions: list[CallableType] = []
127+
self._fallbacks: list[CallableType] = []
127128

128129
def new_partial(self) -> ProperType:
129130
"""
@@ -149,7 +150,7 @@ def new_partial(self) -> ProperType:
149150
def _create_intermediate(
150151
self,
151152
case_function: CallableType,
152-
) -> Tuple[CallableType, Optional[CallableType]]:
153+
) -> tuple[CallableType, CallableType | None]:
153154
intermediate = Intermediate(case_function).with_applied_args(
154155
self._applied_args,
155156
)
@@ -234,7 +235,7 @@ def get_callable_from_context(self) -> ProperType:
234235
self._function_ctx,
235236
))
236237

237-
def build_from_context(self) -> Tuple[bool, List[FuncArg]]:
238+
def build_from_context(self) -> tuple[bool, list[FuncArg]]:
238239
"""
239240
Builds handy arguments structures from the context.
240241

0 commit comments

Comments
 (0)