Skip to content

Commit 7c648fe

Browse files
committed
Closes #5213: add TypeGuards to dtypes module
1 parent 2d41a18 commit 7c648fe

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

arkouda/numpy/dtypes.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
import sys
55

66
from enum import Enum
7-
from typing import Literal, Union, cast
7+
from typing import ( # noqa: F401
8+
Literal,
9+
TypeAlias,
10+
TypeGuard,
11+
Union,
12+
cast,
13+
)
814

915
import numpy as np
1016

@@ -686,8 +692,10 @@ def _val_isinstance_of_union(val, union_type) -> builtins.bool:
686692
bitType = uint64
687693

688694
# Union aliases used for static and runtime type checking
689-
bool_scalars = Union[builtins.bool, np.bool_]
690-
float_scalars = Union[float, np.float64, np.float32]
695+
bool_scalars = Union[builtins.bool, np.bool_] # type: TypeAlias
696+
697+
float_scalars = Union[float, np.float64, np.float32] # type: TypeAlias
698+
691699
int_scalars = Union[
692700
int,
693701
np.int8,
@@ -698,9 +706,12 @@ def _val_isinstance_of_union(val, union_type) -> builtins.bool:
698706
np.uint16,
699707
np.uint32,
700708
np.uint64,
701-
]
702-
numeric_scalars = Union[float_scalars, int_scalars]
703-
numeric_and_bool_scalars = Union[bool_scalars, numeric_scalars]
709+
] # type: TypeAlias
710+
711+
numeric_scalars = Union[float_scalars, int_scalars] # type: TypeAlias
712+
713+
numeric_and_bool_scalars = Union[bool_scalars, numeric_scalars] # type: TypeAlias
714+
704715
numpy_scalars = Union[
705716
np.float64,
706717
np.float32,
@@ -714,9 +725,11 @@ def _val_isinstance_of_union(val, union_type) -> builtins.bool:
714725
np.uint16,
715726
np.uint32,
716727
np.uint64,
717-
]
718-
str_scalars = Union[str, np.str_]
719-
all_scalars = Union[bool_scalars, numeric_scalars, numpy_scalars, str_scalars]
728+
] # type: TypeAlias
729+
730+
str_scalars = Union[str, np.str_] # type: TypeAlias
731+
732+
all_scalars = Union[bool_scalars, numeric_scalars, numpy_scalars, str_scalars] # type: TypeAlias
720733

721734
"""
722735
The DType enum defines the supported Arkouda data types in string form.
@@ -827,7 +840,7 @@ def __repr__(self) -> str:
827840
ScalarDTypes = frozenset(["bool_", "float64", "int64"])
828841

829842

830-
def isSupportedInt(num):
843+
def isSupportedInt(num) -> "TypeGuard[int_scalars]":
831844
"""
832845
Whether a scalar is an arkouda supported integer dtype.
833846
@@ -853,7 +866,7 @@ def isSupportedInt(num):
853866
return isinstance(num, ARKOUDA_SUPPORTED_INTS)
854867

855868

856-
def isSupportedFloat(num):
869+
def isSupportedFloat(num) -> "TypeGuard[float_scalars]":
857870
"""
858871
Whether a scalar is an arkouda supported float dtype.
859872
@@ -879,7 +892,7 @@ def isSupportedFloat(num):
879892
return isinstance(num, ARKOUDA_SUPPORTED_FLOATS)
880893

881894

882-
def isSupportedNumber(num):
895+
def isSupportedNumber(num) -> "TypeGuard[numeric_scalars]":
883896
"""
884897
Whether a scalar is an arkouda supported numeric dtype.
885898
@@ -905,7 +918,7 @@ def isSupportedNumber(num):
905918
return isinstance(num, ARKOUDA_SUPPORTED_NUMBERS)
906919

907920

908-
def isSupportedBool(num):
921+
def isSupportedBool(num) -> "TypeGuard[bool_scalars]":
909922
"""
910923
Whether a scalar is an arkouda supported boolean dtype.
911924

arkouda/numpy/pdarrayclass.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ def format_other(self, other) -> str:
802802
return fmt.format(other)
803803

804804
# binary operators
805-
def _binop(self, other: pdarray, op: str) -> pdarray:
805+
def _binop(self, other: Union[pdarray, numeric_scalars], op: str) -> pdarray:
806806
"""
807807
Execute binary operation specified by the op string.
808808
@@ -882,7 +882,7 @@ def _binop(self, other: pdarray, op: str) -> pdarray:
882882

883883
# reverse binary operators
884884
# pdarray binop pdarray: taken care of by binop function
885-
def _r_binop(self, other: pdarray, op: str) -> pdarray:
885+
def _r_binop(self, other: Union[pdarray, numeric_scalars], op: str) -> pdarray:
886886
"""
887887
Execute reverse binary operation specified by the op string.
888888

pydoc/preprocess/generate_import_stubs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ def get_parent_class_str(obj):
5353
def write_formatted_docstring(f, doc_string, spaces):
5454
doc_string = insert_spaces_after_newlines(doc_string, spaces)
5555
if doc_string is not None and len(doc_string) > 0:
56-
f.write(spaces + "r'''\n")
56+
# AutoApi cannot parse "def" inside a docstring, so replace:
57+
doc_string = doc_string.replace("def ", "def\\ ")
58+
59+
f.write(spaces + 'r"""\n')
5760
f.write(f"{doc_string}\n")
58-
f.write(spaces + "'''")
61+
f.write(spaces + '"""')
5962
f.write("\n" + spaces + "...")
6063
else:
6164
f.write("\n" + spaces + "...")

0 commit comments

Comments
 (0)