Skip to content

Commit

Permalink
Pyupgrade (#1972)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Dec 1, 2024
1 parent 28f14f8 commit 275bed0
Show file tree
Hide file tree
Showing 120 changed files with 400 additions and 481 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12']
python-version: ['3.10', '3.11', '3.12', '3.13']
shard: [0, 1, 2, 3]

steps:
Expand Down
3 changes: 2 additions & 1 deletion returns/_internal/futures/_future.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import TYPE_CHECKING, Awaitable, Callable, TypeVar
from collections.abc import Awaitable, Callable
from typing import TYPE_CHECKING, TypeVar

from returns.io import IO
from returns.primitives.hkt import Kind1, dekind
Expand Down
3 changes: 2 additions & 1 deletion returns/_internal/futures/_future_result.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import TYPE_CHECKING, Any, Awaitable, Callable, TypeVar
from collections.abc import Awaitable, Callable
from typing import TYPE_CHECKING, Any, TypeVar

from returns.io import IO, IOResult
from returns.primitives.hkt import Kind2, dekind
Expand Down
3 changes: 2 additions & 1 deletion returns/_internal/futures/_reader_future_result.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import TYPE_CHECKING, Awaitable, Callable, TypeVar
from collections.abc import Awaitable, Callable
from typing import TYPE_CHECKING, TypeVar

from returns.primitives.hkt import Kind3, dekind
from returns.result import Result, Success
Expand Down
3 changes: 2 additions & 1 deletion returns/_internal/pipeline/managed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Callable, TypeVar
from collections.abc import Callable
from typing import TypeVar

from returns.interfaces.specific.ioresult import IOResultLikeN
from returns.primitives.hkt import Kinded, KindN, kinded
Expand Down
5 changes: 2 additions & 3 deletions returns/context/requires_context.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, ClassVar, TypeVar, final

from typing_extensions import TypeAlias
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias, TypeVar, final

from returns.functions import identity
from returns.future import FutureResult
Expand Down
13 changes: 2 additions & 11 deletions returns/context/requires_context_future_result.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
from __future__ import annotations

from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
ClassVar,
TypeVar,
final,
)

from typing_extensions import TypeAlias
from collections.abc import Awaitable, Callable
from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias, TypeVar, final

from returns._internal.futures import _reader_future_result
from returns.context import NoDeps
Expand Down
5 changes: 2 additions & 3 deletions returns/context/requires_context_ioresult.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, ClassVar, TypeVar, final

from typing_extensions import TypeAlias
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias, TypeVar, final

from returns.context import NoDeps
from returns.interfaces.specific import reader_ioresult
Expand Down
5 changes: 2 additions & 3 deletions returns/context/requires_context_result.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, ClassVar, TypeVar, final

from typing_extensions import TypeAlias
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias, TypeVar, final

from returns.context import NoDeps
from returns.interfaces.specific import reader_result
Expand Down
7 changes: 4 additions & 3 deletions returns/contrib/hypothesis/_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, Sequence, Type, TypeVar
from collections.abc import Callable, Sequence
from typing import TYPE_CHECKING, Any, TypeVar

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

def factory(
container_type: Type[_Inst],
container_type: type[_Inst],
) -> Callable[[Any], st.SearchStrategy[_Inst]]:
def decorator(thing: Any) -> st.SearchStrategy[_Inst]:
from returns.contrib.hypothesis.containers import (
Expand All @@ -44,7 +45,7 @@ def decorator(thing: Any) -> st.SearchStrategy[_Inst]:

#: Our types that we register in hypothesis
#: to be working with ``st.from_type``
registered_types: Sequence[Type[Lawful]] = (
registered_types: Sequence[type[Lawful]] = (
Result,
Maybe,
IO,
Expand Down
3 changes: 2 additions & 1 deletion returns/contrib/hypothesis/containers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, TypeVar
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, TypeVar

from hypothesis import strategies as st

Expand Down
54 changes: 20 additions & 34 deletions returns/contrib/hypothesis/laws.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import inspect
from collections.abc import Callable, Iterator
from contextlib import ExitStack, contextmanager
from typing import (
Any,
Callable,
Dict,
Iterator,
NamedTuple,
Optional,
Type,
TypeVar,
final,
)
from typing import Any, NamedTuple, TypeVar, final

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

settings_kwargs: Dict[str, Any]
settings_kwargs: dict[str, Any]
use_init: bool


def check_all_laws(
container_type: Type[Lawful],
container_type: type[Lawful],
*,
settings_kwargs: Optional[Dict[str, Any]] = None,
settings_kwargs: dict[str, Any] | None = None,
use_init: bool = False,
) -> None:
"""
Expand Down Expand Up @@ -79,7 +70,7 @@ def check_all_laws(

@contextmanager
def container_strategies(
container_type: Type[Lawful],
container_type: type[Lawful],
*,
settings: _Settings,
) -> Iterator[None]:
Expand Down Expand Up @@ -123,7 +114,7 @@ def container_strategies(

@contextmanager
def register_container(
container_type: Type['Lawful'],
container_type: type['Lawful'],
*,
use_init: bool,
) -> Iterator[None]:
Expand Down Expand Up @@ -160,30 +151,25 @@ def factory(thing) -> st.SearchStrategy:
if len(thing.__args__) == 1
else (lambda *args, **kwargs: None)
)
return_type = thing.__args__[-1]
return st.functions(
like=like,
returns=st.from_type(thing.__args__[-1]),
returns=st.from_type(
return_type
if return_type is not None
else type(None),
),
pure=True,
)

callable_type = _get_callable_type()
used = types._global_type_lookup[callable_type]
st.register_type_strategy(callable_type, factory)
used = types._global_type_lookup[Callable] # type: ignore[index]
st.register_type_strategy(Callable, factory) # type: ignore[arg-type]

try:
yield
finally:
types._global_type_lookup.pop(callable_type)
st.register_type_strategy(callable_type, used)


def _get_callable_type() -> Any:
# Helper to accommodate changes in `[email protected]`
if Callable.__origin__ in types._global_type_lookup: # type: ignore
return Callable.__origin__ # type: ignore
elif Callable in types._global_type_lookup: # type: ignore
return Callable
raise RuntimeError('Failed to find Callable type strategy')
types._global_type_lookup.pop(Callable) # type: ignore[call-overload]
st.register_type_strategy(Callable, used) # type: ignore[arg-type]


@contextmanager
Expand Down Expand Up @@ -242,7 +228,7 @@ def _clean_caches() -> None:


def _run_law(
container_type: Type[Lawful],
container_type: type[Lawful],
law: Law,
*,
settings: _Settings,
Expand All @@ -263,8 +249,8 @@ def factory(source: st.DataObject) -> None:


def _create_law_test_case(
container_type: Type[Lawful],
interface: Type[Lawful],
container_type: type[Lawful],
interface: type[Lawful],
law: Law,
*,
settings: _Settings,
Expand Down
15 changes: 8 additions & 7 deletions returns/contrib/mypy/_features/curry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections.abc import Iterator
from itertools import groupby, product
from operator import itemgetter
from typing import Iterator, List, Optional, Tuple, cast, final
from typing import cast, final

from mypy.nodes import ARG_STAR, ARG_STAR2
from mypy.plugin import FunctionContext
Expand All @@ -15,7 +16,7 @@
)

#: Raw material to build `_ArgTree`.
_RawArgTree = List[List[List[FuncArg]]]
_RawArgTree = list[list[list[FuncArg]]]


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

def __init__(self, case: Optional[CallableType]) -> None:
def __init__(self, case: CallableType | None) -> None:
self.case = case
self.children: List['_ArgTree'] = []
self.children: list['_ArgTree'] = []


@final
Expand All @@ -62,7 +63,7 @@ def __init__(self, original: CallableType, ctx: FunctionContext) -> None:
"""
self._original = original
self._ctx = ctx
self._overloads: List[CallableType] = []
self._overloads: list[CallableType] = []
self._args = FuncArg.from_callable(self._original)

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

def _slices(self, source: List[FuncArg]) -> Iterator[List[List[FuncArg]]]:
def _slices(self, source: list[FuncArg]) -> Iterator[list[list[FuncArg]]]:
"""
Generate all possible slices of a source list.
Expand Down
8 changes: 4 additions & 4 deletions returns/contrib/mypy/_features/do_notation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Final, Optional, Tuple
from typing import Final

from mypy.maptype import map_instance_to_supertype
from mypy.nodes import Expression, GeneratorExpr, TypeInfo
Expand Down Expand Up @@ -86,7 +86,7 @@ def _try_fetch_error_type(
type_info: TypeInfo,
seq: Expression,
ctx: MethodContext,
) -> Optional[MypyType]:
) -> MypyType | None:
inst = Instance(
type_info,
[
Expand All @@ -110,7 +110,7 @@ def _try_fetch_error_type(
def _extract_error_type(
typ: MypyType,
type_info: TypeInfo,
) -> Tuple[bool, Optional[MypyType]]:
) -> tuple[bool, MypyType | None]:
typ = get_proper_type(typ)
if isinstance(typ, Instance):
return True, _decide_error_type(
Expand All @@ -129,7 +129,7 @@ def _extract_error_type(
return False, None


def _decide_error_type(typ: Instance) -> Optional[MypyType]:
def _decide_error_type(typ: Instance) -> MypyType | None:
if len(typ.args) < 2:
return None
if isinstance(get_proper_type(typ.args[1]), AnyType):
Expand Down
6 changes: 3 additions & 3 deletions returns/contrib/mypy/_features/kind.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Sequence
from enum import Enum, unique
from typing import Optional, Sequence, Tuple

from mypy.checkmember import analyze_member_access
from mypy.plugin import (
Expand Down Expand Up @@ -159,8 +159,8 @@ class _KindErrors(str, Enum): # noqa: WPS600

def _crop_kind_args(
kind: Instance,
limit: Optional[Sequence[MypyType]] = None,
) -> Tuple[MypyType, ...]:
limit: Sequence[MypyType] | None = None,
) -> tuple[MypyType, ...]:
"""Returns the correct amount of type arguments for a kind."""
if limit is None:
limit = kind.args[0].args # type: ignore
Expand Down
13 changes: 7 additions & 6 deletions returns/contrib/mypy/_features/partial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Final, Iterator, List, Optional, Tuple, final
from collections.abc import Iterator
from typing import Final, final

from mypy.nodes import ARG_STAR, ARG_STAR2
from mypy.plugin import FunctionContext
Expand Down Expand Up @@ -104,7 +105,7 @@ def __init__(
self,
default_return_type: FunctionLike,
original: FunctionLike,
applied_args: List[FuncArg],
applied_args: list[FuncArg],
ctx: FunctionContext,
) -> None:
"""
Expand All @@ -122,8 +123,8 @@ def __init__(
self._applied_args = applied_args
self._ctx = ctx

self._case_functions: List[CallableType] = []
self._fallbacks: List[CallableType] = []
self._case_functions: list[CallableType] = []
self._fallbacks: list[CallableType] = []

def new_partial(self) -> ProperType:
"""
Expand All @@ -149,7 +150,7 @@ def new_partial(self) -> ProperType:
def _create_intermediate(
self,
case_function: CallableType,
) -> Tuple[CallableType, Optional[CallableType]]:
) -> tuple[CallableType, CallableType | None]:
intermediate = Intermediate(case_function).with_applied_args(
self._applied_args,
)
Expand Down Expand Up @@ -234,7 +235,7 @@ def get_callable_from_context(self) -> ProperType:
self._function_ctx,
))

def build_from_context(self) -> Tuple[bool, List[FuncArg]]:
def build_from_context(self) -> tuple[bool, list[FuncArg]]:
"""
Builds handy arguments structures from the context.
Expand Down
Loading

0 comments on commit 275bed0

Please sign in to comment.