From 8f4bd17df90663573a1ef8a2ca21dfaab9ac8edf Mon Sep 17 00:00:00 2001 From: ajpotts Date: Wed, 4 Sep 2024 19:07:58 -0400 Subject: [PATCH] Closes #3746 remove ForwardRef (#3747) Co-authored-by: Amanda Potts --- arkouda/categorical.py | 8 ++++---- arkouda/numeric.py | 14 ++++++++++---- arkouda/pdarraysetops.py | 35 +++++++++++++++++++++++------------ 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/arkouda/categorical.py b/arkouda/categorical.py index a081fcc5e1..940b744f4c 100644 --- a/arkouda/categorical.py +++ b/arkouda/categorical.py @@ -20,15 +20,15 @@ from typeguard import typechecked from arkouda.client import generic_msg -from arkouda.numpy.dtypes import bool_ as akbool -from arkouda.numpy.dtypes import dtype as akdtype -from arkouda.numpy.dtypes import int64 as akint64 -from arkouda.numpy.dtypes import int_scalars, resolve_scalar_dtype, str_, str_scalars from arkouda.groupbyclass import GroupBy, unique from arkouda.infoclass import information from arkouda.logger import getArkoudaLogger from arkouda.numeric import cast as akcast from arkouda.numeric import where +from arkouda.numpy.dtypes import bool_ as akbool +from arkouda.numpy.dtypes import dtype as akdtype +from arkouda.numpy.dtypes import int64 as akint64 +from arkouda.numpy.dtypes import int_scalars, resolve_scalar_dtype, str_, str_scalars from arkouda.pdarrayclass import RegistrationError from arkouda.pdarrayclass import all as akall from arkouda.pdarrayclass import create_pdarray, pdarray diff --git a/arkouda/numeric.py b/arkouda/numeric.py index 5811d3e2ee..41c0e638e2 100644 --- a/arkouda/numeric.py +++ b/arkouda/numeric.py @@ -1,6 +1,6 @@ import json from enum import Enum -from typing import ForwardRef, List, Optional, Sequence, Tuple, Union +from typing import TYPE_CHECKING, List, Sequence, Tuple, TypeVar, Union from typing import cast as type_cast from typing import no_type_check @@ -27,8 +27,12 @@ from arkouda.sorting import sort from arkouda.strings import Strings -Categorical = ForwardRef("Categorical") -SegArray = ForwardRef("SegArray") +if TYPE_CHECKING: + from arkouda.categorical import Categorical + from arkouda.segarray import SegArray +else: + Categorical = TypeVar("Categorical") + SegArray = TypeVar("SegArray") __all__ = [ "cast", @@ -1981,7 +1985,9 @@ def histogramdd( @typechecked def value_counts( pda: pdarray, -) -> Union[Categorical, Tuple[Union[pdarray, Strings], Optional[pdarray]]]: # type: ignore +) -> Tuple[ + Union[Union[pdarray, Strings, Categorical], Sequence[Union[pdarray, Strings, Categorical]]], pdarray +]: """ Count the occurrences of the unique values of an array. diff --git a/arkouda/pdarraysetops.py b/arkouda/pdarraysetops.py index 0755bf2b3b..294de2826f 100644 --- a/arkouda/pdarraysetops.py +++ b/arkouda/pdarraysetops.py @@ -1,24 +1,29 @@ from __future__ import annotations -from typing import ForwardRef, Sequence, Union, cast +from typing import Sequence, TypeVar, Union, cast import numpy as np from typeguard import typechecked + from arkouda.client import generic_msg from arkouda.client_dtypes import BitVector +from arkouda.groupbyclass import GroupBy, groupable, groupable_element_type, unique +from arkouda.logger import getArkoudaLogger from arkouda.numpy.dtypes import bigint from arkouda.numpy.dtypes import bool_ as akbool from arkouda.numpy.dtypes import int64 as akint64 from arkouda.numpy.dtypes import uint64 as akuint64 -from arkouda.groupbyclass import GroupBy, groupable, groupable_element_type, unique -from arkouda.logger import getArkoudaLogger from arkouda.pdarrayclass import create_pdarray, pdarray from arkouda.pdarraycreation import array, ones, zeros, zeros_like from arkouda.sorting import argsort from arkouda.strings import Strings +from typing import TYPE_CHECKING -Categorical = ForwardRef("Categorical") +if TYPE_CHECKING: + from arkouda.categorical import Categorical +else: + Categorical = TypeVar("Categorical") __all__ = ["in1d", "concatenate", "union1d", "intersect1d", "setdiff1d", "setxor1d", "indexof1d"] @@ -26,8 +31,8 @@ def _in1d_single( - pda1: Union[pdarray, Strings, "Categorical"], # type: ignore - pda2: Union[pdarray, Strings, "Categorical"], # type: ignore + pda1: Union[pdarray, Strings, "Categorical"], + pda2: Union[pdarray, Strings, "Categorical"], invert: bool = False, ) -> pdarray: """ @@ -131,7 +136,7 @@ def in1d( assume_unique: bool = False, symmetric: bool = False, invert: bool = False, -) -> Union[pdarray, groupable]: +) -> groupable: """ Test whether each element of a 1-D array is also present in a second array. @@ -176,10 +181,16 @@ def in1d( raise TypeError("Arguments must have compatible types, Strings/Categorical") elif isinstance(pda1, pdarray) and not isinstance(pda2, pdarray): raise TypeError("If pda1 is pdarray, pda2 must also be pda2") - if symmetric: - return _in1d_single(pda1, pda2), _in1d_single(pda2, pda1, invert) - else: + elif isinstance(pda2, (pdarray, Strings, Categorical_)): + if symmetric: + return _in1d_single(pda1, pda2), _in1d_single(pda2, pda1, invert) return _in1d_single(pda1, pda2, invert) + else: + raise TypeError( + "Inputs should both be Union[pdarray, Strings, Categorical] or both be " + "Sequence[pdarray, Strings, Categorical]." + " (Do not mix and match.)" + ) atypes = np.array([ai.dtype for ai in pda1]) btypes = np.array([bi.dtype for bi in pda2]) if not (atypes == btypes).all(): @@ -291,9 +302,9 @@ def indexof1d(query: groupable, space: groupable) -> pdarray: # fmt: off @typechecked def concatenate( - arrays: Sequence[Union[pdarray, Strings, "Categorical", ]], # type: ignore + arrays: Sequence[Union[pdarray, Strings, "Categorical", ]], ordered: bool = True, -) -> Union[pdarray, Strings, "Categorical"]: # type: ignore +) -> Union[pdarray, Strings, Categorical, Sequence[Categorical]]: """ Concatenate a list or tuple of ``pdarray`` or ``Strings`` objects into one ``pdarray`` or ``Strings`` object, respectively.