diff --git a/pandas/_version.py b/pandas/_version.py index 54d5bb8c2dc91..80a13ec607130 100644 --- a/pandas/_version.py +++ b/pandas/_version.py @@ -640,7 +640,7 @@ def render(pieces, style): } -def get_versions(): +def get_versions() -> dict: """Get version information or return default if unable to do so.""" # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have # __file__, we can work backwards from there to the root. Some diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 8126bd072a8dc..d8953da5490cd 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -11,7 +11,9 @@ from typing import ( TYPE_CHECKING, Literal, + TypeVar, cast, + overload, ) import warnings @@ -104,6 +106,8 @@ ExtensionArray, ) + T = TypeVar("T", bound=Index | Categorical | ExtensionArray) + # --------------- # # dtype access # @@ -314,6 +318,12 @@ def _check_object_for_strings(values: np.ndarray) -> str: # --------------- # +@overload +def unique(values: T) -> T: ... +@overload +def unique(values: np.ndarray | Series) -> np.ndarray: ... + + def unique(values): """ Return unique values based on a hash table. diff --git a/pandas/core/base.py b/pandas/core/base.py index 6d2ab581470bc..7d7e43808be5c 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1102,7 +1102,7 @@ def unique(self): # i.e. ExtensionArray result = values.unique() else: - result = algorithms.unique1d(values) + result = algorithms.unique1d(values) # type: ignore[assignment] return result @final diff --git a/pandas/core/groupby/categorical.py b/pandas/core/groupby/categorical.py index 806f34975b8a7..0daa6d8f8101b 100644 --- a/pandas/core/groupby/categorical.py +++ b/pandas/core/groupby/categorical.py @@ -46,7 +46,7 @@ def recode_for_groupby(c: Categorical, sort: bool, observed: bool) -> Categorica # In cases with c.ordered, this is equivalent to # return c.remove_unused_categories(), c - take_codes = unique1d(c.codes[c.codes != -1]) # type: ignore[no-untyped-call] + take_codes = unique1d(c.codes[c.codes != -1]) if sort: take_codes = np.sort(take_codes) @@ -68,7 +68,7 @@ def recode_for_groupby(c: Categorical, sort: bool, observed: bool) -> Categorica # GH:46909: Re-ordering codes faster than using (set|add|reorder)_categories # GH 38140: exclude nan from indexer for categories - unique_notnan_codes = unique1d(c.codes[c.codes != -1]) # type: ignore[no-untyped-call] + unique_notnan_codes = unique1d(c.codes[c.codes != -1]) if sort: unique_notnan_codes = np.sort(unique_notnan_codes) if (num_cat := len(c.categories)) > len(unique_notnan_codes): diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index ddde24e72c65c..bcf647acb98b1 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3304,7 +3304,7 @@ def _intersection(self, other: Index, sort: bool = False): res = algos.unique1d(res_indexer) else: result = self.take(indexer) - res = result.drop_duplicates() + res = result.drop_duplicates() # type: ignore[assignment] return ensure_wrapped_if_datetimelike(res) res_values = self._intersection_via_get_indexer(other, sort=sort) diff --git a/pandas/util/_print_versions.py b/pandas/util/_print_versions.py index efcb50bf4345c..00724c76b9ba8 100644 --- a/pandas/util/_print_versions.py +++ b/pandas/util/_print_versions.py @@ -32,7 +32,7 @@ def _get_commit_hash() -> str | None: except ImportError: from pandas._version import get_versions - versions = get_versions() # type: ignore[no-untyped-call] + versions = get_versions() return versions["full-revisionid"]