Skip to content

Commit 957efd0

Browse files
TYP: improve type annotations and remove unnecessary type ignores
- Add and refine type hints in core modules (algorithms, base, indexes, _version). - Remove redundant `# type: ignore[no-untyped-call]` comments in groupby/categorical.py. - Refactor variable annotations for clarity and consistency. - No functional changes; improves code quality and type safety.
1 parent 452c7fb commit 957efd0

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

pandas/_version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import re
1818
import subprocess
1919
import sys
20+
from typing import Any
2021

2122

2223
def get_keywords():
@@ -640,7 +641,7 @@ def render(pieces, style):
640641
}
641642

642643

643-
def get_versions():
644+
def get_versions() -> dict[str, Any]:
644645
"""Get version information or return default if unable to do so."""
645646
# I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have
646647
# __file__, we can work backwards from there to the root. Some

pandas/core/algorithms.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
TYPE_CHECKING,
1313
Literal,
1414
cast,
15+
overload,
1516
)
1617
import warnings
1718

@@ -314,6 +315,18 @@ def _check_object_for_strings(values: np.ndarray) -> str:
314315
# --------------- #
315316

316317

318+
@overload
319+
def unique(values: np.ndarray) -> np.ndarray: ...
320+
@overload
321+
def unique(values: Index) -> Index: ...
322+
@overload
323+
def unique(values: Series) -> np.ndarray: ...
324+
@overload
325+
def unique(values: Categorical) -> Categorical: ...
326+
@overload
327+
def unique(values: ExtensionArray) -> ExtensionArray: ...
328+
329+
317330
def unique(values):
318331
"""
319332
Return unique values based on a hash table.

pandas/core/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ def unique(self):
11001100
values = self._values
11011101
if not isinstance(values, np.ndarray):
11021102
# i.e. ExtensionArray
1103-
result = values.unique()
1103+
result: np.ndarray | ExtensionArray = values.unique()
11041104
else:
11051105
result = algorithms.unique1d(values)
11061106
return result

pandas/core/groupby/categorical.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def recode_for_groupby(c: Categorical, sort: bool, observed: bool) -> Categorica
4646
# In cases with c.ordered, this is equivalent to
4747
# return c.remove_unused_categories(), c
4848

49-
take_codes = unique1d(c.codes[c.codes != -1]) # type: ignore[no-untyped-call]
49+
take_codes = unique1d(c.codes[c.codes != -1])
5050

5151
if sort:
5252
take_codes = np.sort(take_codes)
@@ -68,7 +68,7 @@ def recode_for_groupby(c: Categorical, sort: bool, observed: bool) -> Categorica
6868

6969
# GH:46909: Re-ordering codes faster than using (set|add|reorder)_categories
7070
# GH 38140: exclude nan from indexer for categories
71-
unique_notnan_codes = unique1d(c.codes[c.codes != -1]) # type: ignore[no-untyped-call]
71+
unique_notnan_codes = unique1d(c.codes[c.codes != -1])
7272
if sort:
7373
unique_notnan_codes = np.sort(unique_notnan_codes)
7474
if (num_cat := len(c.categories)) > len(unique_notnan_codes):

pandas/core/indexes/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3301,7 +3301,9 @@ def _intersection(self, other: Index, sort: bool = False):
33013301
if is_numeric_dtype(self.dtype):
33023302
# This is faster, because Index.unique() checks for uniqueness
33033303
# before calculating the unique values.
3304-
res = algos.unique1d(res_indexer)
3304+
res: Index | ExtensionArray | np.ndarray = algos.unique1d(
3305+
res_indexer
3306+
)
33053307
else:
33063308
result = self.take(indexer)
33073309
res = result.drop_duplicates()

pandas/util/_print_versions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _get_commit_hash() -> str | None:
3232
except ImportError:
3333
from pandas._version import get_versions
3434

35-
versions = get_versions() # type: ignore[no-untyped-call]
35+
versions = get_versions()
3636
return versions["full-revisionid"]
3737

3838

0 commit comments

Comments
 (0)