Skip to content

Commit c99e52e

Browse files
TYP: improve type annotations and remove unnecessary type ignores (#62315)
1 parent 2f26644 commit c99e52e

File tree

6 files changed

+16
-6
lines changed

6 files changed

+16
-6
lines changed

pandas/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ def render(pieces, style):
640640
}
641641

642642

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

pandas/core/algorithms.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from typing import (
1212
TYPE_CHECKING,
1313
Literal,
14+
TypeVar,
1415
cast,
16+
overload,
1517
)
1618
import warnings
1719

@@ -104,6 +106,8 @@
104106
ExtensionArray,
105107
)
106108

109+
T = TypeVar("T", bound=Index | Categorical | ExtensionArray)
110+
107111

108112
# --------------- #
109113
# dtype access #
@@ -314,6 +318,12 @@ def _check_object_for_strings(values: np.ndarray) -> str:
314318
# --------------- #
315319

316320

321+
@overload
322+
def unique(values: T) -> T: ...
323+
@overload
324+
def unique(values: np.ndarray | Series) -> np.ndarray: ...
325+
326+
317327
def unique(values):
318328
"""
319329
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
@@ -1102,7 +1102,7 @@ def unique(self):
11021102
# i.e. ExtensionArray
11031103
result = values.unique()
11041104
else:
1105-
result = algorithms.unique1d(values)
1105+
result = algorithms.unique1d(values) # type: ignore[assignment]
11061106
return result
11071107

11081108
@final

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3304,7 +3304,7 @@ def _intersection(self, other: Index, sort: bool = False):
33043304
res = algos.unique1d(res_indexer)
33053305
else:
33063306
result = self.take(indexer)
3307-
res = result.drop_duplicates()
3307+
res = result.drop_duplicates() # type: ignore[assignment]
33083308
return ensure_wrapped_if_datetimelike(res)
33093309

33103310
res_values = self._intersection_via_get_indexer(other, sort=sort)

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)