Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump required Python to 3.10 #287

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions arraycontext/container/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,20 @@
THE SOFTWARE.
"""

from collections.abc import Hashable, Sequence
from functools import singledispatch
from typing import (
TYPE_CHECKING,
Any,
Hashable,
Optional,
Protocol,
Sequence,
Tuple,
TypeAlias,
TypeVar,
)

# For use in singledispatch type annotations, because sphinx can't figure out
# what 'np' is.
import numpy
import numpy as np
from typing_extensions import TypeAlias

from arraycontext.context import ArrayContext

Expand Down Expand Up @@ -162,7 +159,7 @@ class NotAnArrayContainerError(TypeError):


SerializationKey: TypeAlias = Hashable
SerializedContainer: TypeAlias = Sequence[Tuple[SerializationKey, "ArrayOrContainer"]]
SerializedContainer: TypeAlias = Sequence[tuple[SerializationKey, "ArrayOrContainer"]]


@singledispatch
Expand Down Expand Up @@ -249,7 +246,7 @@ def is_array_container(ary: Any) -> bool:


@singledispatch
def get_container_context_opt(ary: ArrayContainer) -> Optional[ArrayContext]:
def get_container_context_opt(ary: ArrayContainer) -> ArrayContext | None:
"""Retrieves the :class:`ArrayContext` from the container, if any.

This function is not recursive, so it will only search at the root level
Expand Down Expand Up @@ -303,7 +300,7 @@ def _deserialize_ndarray_container( # type: ignore[misc]
# {{{ get_container_context_recursively

def get_container_context_recursively_opt(
ary: ArrayContainer) -> Optional[ArrayContext]:
ary: ArrayContainer) -> ArrayContext | None:
"""Walks the :class:`ArrayContainer` hierarchy to find an
:class:`ArrayContext` associated with it.

Expand Down Expand Up @@ -337,7 +334,7 @@ def get_container_context_recursively_opt(
return actx


def get_container_context_recursively(ary: ArrayContainer) -> Optional[ArrayContext]:
def get_container_context_recursively(ary: ArrayContainer) -> ArrayContext | None:
"""Walks the :class:`ArrayContainer` hierarchy to find an
:class:`ArrayContext` associated with it.

Expand Down
42 changes: 20 additions & 22 deletions arraycontext/container/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"""

import enum
from typing import Any, Callable, Optional, Tuple, TypeVar, Union
from collections.abc import Callable
from typing import Any, TypeVar
from warnings import warn

import numpy as np
Expand Down Expand Up @@ -90,7 +91,7 @@ class _OpClass(enum.Enum):
]


def _format_unary_op_str(op_str: str, arg1: Union[Tuple[str, ...], str]) -> str:
def _format_unary_op_str(op_str: str, arg1: tuple[str, ...] | str) -> str:
if isinstance(arg1, tuple):
arg1_entry, arg1_container = arg1
return (f"{op_str.format(arg1_entry)} "
Expand All @@ -100,14 +101,10 @@ def _format_unary_op_str(op_str: str, arg1: Union[Tuple[str, ...], str]) -> str:


def _format_binary_op_str(op_str: str,
arg1: Union[Tuple[str, str], str],
arg2: Union[Tuple[str, str], str]) -> str:
arg1: tuple[str, str] | str,
arg2: tuple[str, str] | str) -> str:
if isinstance(arg1, tuple) and isinstance(arg2, tuple):
import sys
if sys.version_info >= (3, 10):
strict_arg = ", strict=__debug__"
else:
strict_arg = ""
strict_arg = ", strict=__debug__"

arg1_entry, arg1_container = arg1
arg2_entry, arg2_container = arg2
Expand Down Expand Up @@ -160,23 +157,23 @@ class ComplainingNumpyNonObjectArray(metaclass=ComplainingNumpyNonObjectArrayMet

def with_container_arithmetic(
*,
number_bcasts_across: Optional[bool] = None,
bcasts_across_obj_array: Optional[bool] = None,
container_types_bcast_across: Optional[Tuple[type, ...]] = None,
number_bcasts_across: bool | None = None,
bcasts_across_obj_array: bool | None = None,
container_types_bcast_across: tuple[type, ...] | None = None,
arithmetic: bool = True,
matmul: bool = False,
bitwise: bool = False,
shift: bool = False,
_cls_has_array_context_attr: Optional[bool] = None,
eq_comparison: Optional[bool] = None,
rel_comparison: Optional[bool] = None,
_cls_has_array_context_attr: bool | None = None,
eq_comparison: bool | None = None,
rel_comparison: bool | None = None,

# deprecated:
bcast_number: Optional[bool] = None,
bcast_obj_array: Optional[bool] = None,
bcast_number: bool | None = None,
bcast_obj_array: bool | None = None,
bcast_numpy_array: bool = False,
_bcast_actx_array_type: Optional[bool] = None,
bcast_container_types: Optional[Tuple[type, ...]] = None,
_bcast_actx_array_type: bool | None = None,
bcast_container_types: tuple[type, ...] | None = None,
) -> Callable[[type], type]:
"""A class decorator that implements built-in operators for array containers
by propagating the operations to the elements of the container.
Expand Down Expand Up @@ -482,7 +479,7 @@ def same_key(k1: T, k2: T) -> T:
assert k1 == k2
return k1

def tup_str(t: Tuple[str, ...]) -> str:
def tup_str(t: tuple[str, ...]) -> str:
if not t:
return "()"
else:
Expand Down Expand Up @@ -544,8 +541,9 @@ def {fname}(arg1):
_format_binary_op_str(op_str, expr_arg1, expr_arg2)
for (key_arg1, expr_arg1), (key_arg2, expr_arg2) in zip(
cls._serialize_init_arrays_code("arg1").items(),
cls._serialize_init_arrays_code("arg2").items())
})
cls._serialize_init_arrays_code("arg2").items(),
strict=True,
)})
bcast_init_args_arg1_is_outer = cls._deserialize_init_arrays_code("arg1", {
key_arg1: _format_binary_op_str(op_str, expr_arg1, "arg2")
for key_arg1, expr_arg1 in
Expand Down
8 changes: 4 additions & 4 deletions arraycontext/container/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"""

from dataclasses import Field, fields, is_dataclass
from typing import Tuple, Union, get_args, get_origin
from typing import Union, get_args, get_origin

from arraycontext.container import is_array_container_type

Expand Down Expand Up @@ -100,7 +100,7 @@ def is_array_field(f: Field) -> bool:
_BaseGenericAlias,
_SpecialForm,
)
if isinstance(f.type, (_BaseGenericAlias, _SpecialForm)):
if isinstance(f.type, _BaseGenericAlias | _SpecialForm):
# NOTE: anything except a Union is not allowed
raise TypeError(
f"Typing annotation not supported on field '{f.name}': "
Expand All @@ -125,8 +125,8 @@ def is_array_field(f: Field) -> bool:

def inject_dataclass_serialization(
cls: type,
array_fields: Tuple[Field, ...],
non_array_fields: Tuple[Field, ...]) -> type:
array_fields: tuple[Field, ...],
non_array_fields: tuple[Field, ...]) -> type:
"""Implements :func:`~arraycontext.serialize_container` and
:func:`~arraycontext.deserialize_container` for the given dataclass *cls*.

Expand Down
Loading
Loading