Skip to content

Commit

Permalink
Convert Optional[a] to a | None
Browse files Browse the repository at this point in the history
This is allowed with `from __future__ import annotations`
  • Loading branch information
mhostetter committed Sep 11, 2022
1 parent a9a765e commit 0a4ec79
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 108 deletions.
10 changes: 5 additions & 5 deletions src/galois/_codes/_bch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from __future__ import annotations

import math
from typing import Tuple, List, Optional, Type, overload
from typing import Tuple, List, Type, overload
from typing_extensions import Literal

import numba
Expand All @@ -27,8 +27,8 @@ def _check_and_compute_field(
n: int,
k: int,
c: int,
primitive_poly: Optional[PolyLike] = None,
primitive_element: Optional[PolyLike] = None
primitive_poly: PolyLike | None = None,
primitive_element: PolyLike | None = None
) -> Type[FieldArray]:
verify_isinstance(n, int)
verify_isinstance(k, int)
Expand Down Expand Up @@ -174,8 +174,8 @@ def __init__(
self,
n: int,
k: int,
primitive_poly: Optional[PolyLike] = None,
primitive_element: Optional[PolyLike] = None,
primitive_poly: PolyLike | None = None,
primitive_element: PolyLike | None = None,
systematic: bool = True
):
r"""
Expand Down
6 changes: 3 additions & 3 deletions src/galois/_codes/_reed_solomon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from __future__ import annotations

from typing import Tuple, Optional, Type, overload
from typing import Tuple, Type, overload
from typing_extensions import Literal

import numba
Expand Down Expand Up @@ -74,8 +74,8 @@ def __init__(
n: int,
k: int,
c: int = 1,
primitive_poly: Optional[PolyLike] = None,
primitive_element: Optional[PolyLike] = None,
primitive_poly: PolyLike | None = None,
primitive_element: PolyLike | None = None,
systematic: bool = True
):
r"""
Expand Down
18 changes: 9 additions & 9 deletions src/galois/_domains/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import abc
import contextlib
import random
from typing import Optional, Generator
from typing import Generator
from typing_extensions import Literal

import numpy as np
Expand Down Expand Up @@ -35,7 +35,7 @@ class Array(LinalgFunctionMixin, FunctionMixin, UFuncMixin, np.ndarray, metaclas
def __new__(
cls,
x: ElementLike | ArrayLike,
dtype: Optional[DTypeLike] = None,
dtype: DTypeLike | None = None,
copy: bool = True,
order: Literal["K", "A", "C", "F"] = "K",
ndmin: int = 0
Expand Down Expand Up @@ -144,7 +144,7 @@ def _view_without_verification(cls):
###############################################################################

@classmethod
def Zeros(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> Array:
def Zeros(cls, shape: ShapeLike, dtype: DTypeLike | None = None) -> Array:
"""
Creates an array of all zeros.
Expand All @@ -166,7 +166,7 @@ def Zeros(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> Array:
return cls._view(array)

@classmethod
def Ones(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> Array:
def Ones(cls, shape: ShapeLike, dtype: DTypeLike | None = None) -> Array:
"""
Creates an array of all ones.
Expand All @@ -193,7 +193,7 @@ def Range(
start: ElementLike,
stop: ElementLike,
step: int = 1,
dtype: Optional[DTypeLike] = None
dtype: DTypeLike | None = None
) -> Array:
"""
Creates a 1-D array with a range of elements.
Expand Down Expand Up @@ -236,9 +236,9 @@ def Random(
cls,
shape: ShapeLike = (),
low: ElementLike = 0,
high: Optional[ElementLike] = None,
seed: Optional[int | np.random.Generator] = None,
dtype: Optional[DTypeLike] = None
high: ElementLike | None = None,
seed: int | np.random.Generator | None = None,
dtype: DTypeLike | None = None
) -> Array:
"""
Creates an array with random elements.
Expand Down Expand Up @@ -303,7 +303,7 @@ def Random(
return cls._view(array)

@classmethod
def Identity(cls, size: int, dtype: Optional[DTypeLike] = None) -> Array:
def Identity(cls, size: int, dtype: DTypeLike | None = None) -> Array:
r"""
Creates an :math:`n \times n` identity matrix.
Expand Down
4 changes: 2 additions & 2 deletions src/galois/_domains/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
from __future__ import annotations

from typing import Tuple, Optional, Type, TYPE_CHECKING
from typing import Tuple, Type, TYPE_CHECKING

import numba
from numba import int64
Expand Down Expand Up @@ -245,7 +245,7 @@ class row_reduce_jit(Function):
Converts the matrix into its row-reduced echelon form using Gaussian elimination.
"""

def __call__(self, A: Array, ncols: Optional[int] = None) -> Tuple[Array, int]:
def __call__(self, A: Array, ncols: int | None = None) -> Tuple[Array, int]:
verify_isinstance(A, self.field)
if not A.ndim == 2:
raise ValueError(f"Only 2-D matrices can be converted to reduced row echelon form, not {A.ndim}-D.")
Expand Down
36 changes: 18 additions & 18 deletions src/galois/_fields/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from __future__ import annotations

from typing import Tuple, Optional
from typing import Tuple
from typing_extensions import Literal

import numpy as np
Expand Down Expand Up @@ -56,7 +56,7 @@ class FieldArray(Array, metaclass=FieldArrayMeta):
def __new__(
cls,
x: ElementLike | ArrayLike,
dtype: Optional[DTypeLike] = None,
dtype: DTypeLike | None = None,
copy: bool = True,
order: Literal["K", "A", "C", "F"] = "K",
ndmin: int = 0
Expand All @@ -68,7 +68,7 @@ def __new__(
def __init__(
self,
x: ElementLike | ArrayLike,
dtype: Optional[DTypeLike] = None,
dtype: DTypeLike | None = None,
copy: bool = True,
order: Literal["K", "A", "C", "F"] = "K",
ndmin: int = 0
Expand Down Expand Up @@ -195,7 +195,7 @@ def _convert_iterable_to_elements(cls, iterable: IterableLike) -> np.ndarray:
GF.Zeros((2, 5))
"""
)
def Zeros(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> FieldArray:
def Zeros(cls, shape: ShapeLike, dtype: DTypeLike | None = None) -> FieldArray:
return super().Zeros(shape, dtype=dtype)

@classmethod
Expand All @@ -209,7 +209,7 @@ def Zeros(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> FieldArra
GF.Ones((2, 5))
"""
)
def Ones(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> FieldArray:
def Ones(cls, shape: ShapeLike, dtype: DTypeLike | None = None) -> FieldArray:
return super().Ones(shape, dtype=dtype)

@classmethod
Expand Down Expand Up @@ -241,7 +241,7 @@ def Range(
start: ElementLike,
stop: ElementLike,
step: int = 1,
dtype: Optional[DTypeLike] = None
dtype: DTypeLike | None = None
) -> FieldArray:
return super().Range(start, stop, step=step, dtype=dtype)

Expand Down Expand Up @@ -277,9 +277,9 @@ def Random(
cls,
shape: ShapeLike = (),
low: ElementLike = 0,
high: Optional[ElementLike] = None,
seed: Optional[int | np.random.Generator] = None,
dtype: Optional[DTypeLike] = None
high: ElementLike | None = None,
seed: int | np.random.Generator | None = None,
dtype: DTypeLike | None = None
) -> FieldArray:
return super().Random(shape=shape, low=low, high=high, seed=seed, dtype=dtype)

Expand All @@ -294,11 +294,11 @@ def Random(
GF.Identity(4)
"""
)
def Identity(cls, size: int, dtype: Optional[DTypeLike] = None) -> FieldArray:
def Identity(cls, size: int, dtype: DTypeLike | None = None) -> FieldArray:
return super().Identity(size, dtype=dtype)

@classmethod
def Vandermonde(cls, element: ElementLike, rows: int, cols: int, dtype: Optional[DTypeLike] = None) -> FieldArray:
def Vandermonde(cls, element: ElementLike, rows: int, cols: int, dtype: DTypeLike | None = None) -> FieldArray:
r"""
Creates an :math:`m \times n` Vandermonde matrix of :math:`a \in \mathrm{GF}(q)`.
Expand Down Expand Up @@ -348,7 +348,7 @@ def Vandermonde(cls, element: ElementLike, rows: int, cols: int, dtype: Optional
return V

@classmethod
def Vector(cls, array: ArrayLike, dtype: Optional[DTypeLike] = None) -> FieldArray:
def Vector(cls, array: ArrayLike, dtype: DTypeLike | None = None) -> FieldArray:
r"""
Creates an array over :math:`\mathrm{GF}(p^m)` from length-:math:`m` vectors over the prime subfield :math:`\mathrm{GF}(p)`.
Expand Down Expand Up @@ -402,7 +402,7 @@ def Vector(cls, array: ArrayLike, dtype: Optional[DTypeLike] = None) -> FieldArr
###############################################################################

@classmethod
def repr_table(cls, element: Optional[ElementLike] = None, sort: Literal["power", "poly", "vector", "int"] = "power") -> str:
def repr_table(cls, element: ElementLike | None = None, sort: Literal["power", "poly", "vector", "int"] = "power") -> str:
r"""
Generates a finite field element representation table comparing the power, polynomial, vector, and integer representations.
Expand Down Expand Up @@ -497,8 +497,8 @@ def repr_table(cls, element: Optional[ElementLike] = None, sort: Literal["power"
def arithmetic_table(
cls,
operation: Literal["+", "-", "*", "/"],
x: Optional[FieldArray] = None,
y: Optional[FieldArray] = None
x: FieldArray | None = None,
y: FieldArray | None = None
) -> str:
r"""
Generates the specified arithmetic table for the finite field.
Expand Down Expand Up @@ -927,7 +927,7 @@ def is_square(self) -> np.bool_ | np.ndarray:
# Compute the Legendre symbol on each element
return x ** ((field.order - 1)//2) != field.characteristic - 1

def vector(self, dtype: Optional[DTypeLike] = None) -> FieldArray:
def vector(self, dtype: DTypeLike | None = None) -> FieldArray:
r"""
Converts an array over :math:`\mathrm{GF}(p^m)` to length-:math:`m` vectors over the prime subfield :math:`\mathrm{GF}(p)`.
Expand Down Expand Up @@ -979,7 +979,7 @@ def vector(self, dtype: Optional[DTypeLike] = None) -> FieldArray:

return y

def row_reduce(self, ncols: Optional[int] = None) -> FieldArray:
def row_reduce(self, ncols: int | None = None) -> FieldArray:
r"""
Performs Gaussian elimination on the matrix to achieve reduced row echelon form (RREF).
Expand Down Expand Up @@ -1501,7 +1501,7 @@ def minimal_poly(self) -> Poly:
else:
raise ValueError(f"The array must be either 0-D to return the minimal polynomial of a single element or 2-D to return the minimal polynomial of a square matrix, not have shape {self.shape}.")

def log(self, base: Optional[ElementLike | ArrayLike] = None) -> np.ndarray:
def log(self, base: ElementLike | ArrayLike | None = None) -> np.ndarray:
r"""
Computes the logarithm of the array :math:`x` base :math:`\beta`.
Expand Down
32 changes: 16 additions & 16 deletions src/galois/_fields/_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import sys
import types
from typing import Optional, Type
from typing import Type
from typing_extensions import Literal

from .._helper import export, verify_isinstance
Expand All @@ -23,11 +23,11 @@
@export
def GF(
order: int,
irreducible_poly: Optional[PolyLike] = None,
primitive_element: Optional[int | PolyLike] = None, # pylint: disable=redefined-outer-name
irreducible_poly: PolyLike | None = None,
primitive_element: int | PolyLike | None = None, # pylint: disable=redefined-outer-name
verify: bool = True,
compile: Optional[Literal["auto", "jit-lookup", "jit-calculate", "python-calculate"]] = None, # pylint: disable=redefined-builtin
display: Optional[Literal["int", "poly", "power"]] = None
compile: Literal["auto", "jit-lookup", "jit-calculate", "python-calculate"] | None = None, # pylint: disable=redefined-builtin
display: Literal["int", "poly", "power"] | None = None
) -> Type[FieldArray]:
r"""
Creates a :obj:`~galois.FieldArray` subclass for :math:`\mathrm{GF}(p^m)`.
Expand Down Expand Up @@ -222,11 +222,11 @@ def GF(
@export
def Field(
order: int,
irreducible_poly: Optional[PolyLike] = None,
primitive_element: Optional[int | PolyLike] = None, # pylint: disable=redefined-outer-name
irreducible_poly: PolyLike | None = None,
primitive_element: int | PolyLike | None = None, # pylint: disable=redefined-outer-name
verify: bool = True,
compile: Optional[Literal["auto", "jit-lookup", "jit-calculate", "python-calculate"]] = None, # pylint: disable=redefined-builtin
display: Optional[Literal["int", "poly", "power"]] = None
compile: Literal["auto", "jit-lookup", "jit-calculate", "python-calculate"] | None = None, # pylint: disable=redefined-builtin
display: Literal["int", "poly", "power"] | None = None
) -> Type[FieldArray]:
"""
Alias of :func:`~galois.GF`.
Expand All @@ -238,10 +238,10 @@ def Field(

def _GF_prime(
p: int,
alpha: Optional[int] = None,
alpha: int | None = None,
verify: bool = True,
compile: Optional[Literal["auto", "jit-lookup", "jit-calculate", "python-calculate"]] = None, # pylint: disable=redefined-builtin
display: Optional[Literal["int", "poly", "power"]] = None
compile: Literal["auto", "jit-lookup", "jit-calculate", "python-calculate"] | None = None, # pylint: disable=redefined-builtin
display: Literal["int", "poly", "power"] | None = None
) -> Type[FieldArray]:
"""
Class factory for prime fields GF(p).
Expand Down Expand Up @@ -302,11 +302,11 @@ def _GF_prime(
def _GF_extension(
p: int,
m: int,
irreducible_poly_: Optional[PolyLike] = None,
alpha: Optional[PolyLike] = None,
irreducible_poly_: PolyLike | None = None,
alpha: PolyLike | None = None,
verify: bool = True,
compile: Optional[Literal["auto", "jit-lookup", "jit-calculate", "python-calculate"]] = None, # pylint: disable=redefined-builtin
display: Optional[Literal["int", "poly", "power"]] = None
compile: Literal["auto", "jit-lookup", "jit-calculate", "python-calculate"] | None = None, # pylint: disable=redefined-builtin
display: Literal["int", "poly", "power"] | None = None
) -> Type[FieldArray]:
"""
Class factory for extension fields GF(p^m).
Expand Down
Loading

0 comments on commit 0a4ec79

Please sign in to comment.