Skip to content

Commit 9612375

Browse files
authored
TYP: return values in core/*.py (pandas-dev#47587)
* TYP: return values in core/*.py * fix test * to_html * to_html part 2 * DataFrame.query * more overloads * fix query? * increase stacklevel by one * fix rename_axis * and an overload for DataFrame.eval * address comments * fix typevar
1 parent a9a496c commit 9612375

16 files changed

+1287
-223
lines changed

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ def value_counts(
948948
# Called once from SparseArray, otherwise could be private
949949
def value_counts_arraylike(
950950
values: np.ndarray, dropna: bool, mask: npt.NDArray[np.bool_] | None = None
951-
):
951+
) -> tuple[ArrayLike, npt.NDArray[np.int64]]:
952952
"""
953953
Parameters
954954
----------

pandas/core/arrays/masked.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -945,9 +945,9 @@ def value_counts(self, dropna: bool = True) -> Series:
945945
index = index.astype(self.dtype)
946946

947947
mask = np.zeros(len(counts), dtype="bool")
948-
counts = IntegerArray(counts, mask)
948+
counts_array = IntegerArray(counts, mask)
949949

950-
return Series(counts, index=index)
950+
return Series(counts_array, index=index)
951951

952952
@doc(ExtensionArray.equals)
953953
def equals(self, other) -> bool:

pandas/core/arrays/sparse/array.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -889,12 +889,20 @@ def value_counts(self, dropna: bool = True) -> Series:
889889
if mask.any():
890890
counts[mask] += fcounts
891891
else:
892-
keys = np.insert(keys, 0, self.fill_value)
892+
# error: Argument 1 to "insert" has incompatible type "Union[
893+
# ExtensionArray,ndarray[Any, Any]]"; expected "Union[
894+
# _SupportsArray[dtype[Any]], Sequence[_SupportsArray[dtype
895+
# [Any]]], Sequence[Sequence[_SupportsArray[dtype[Any]]]],
896+
# Sequence[Sequence[Sequence[_SupportsArray[dtype[Any]]]]], Sequence
897+
# [Sequence[Sequence[Sequence[_SupportsArray[dtype[Any]]]]]]]"
898+
keys = np.insert(keys, 0, self.fill_value) # type: ignore[arg-type]
893899
counts = np.insert(counts, 0, fcounts)
894900

895901
if not isinstance(keys, ABCIndex):
896-
keys = Index(keys)
897-
return Series(counts, index=keys)
902+
index = Index(keys)
903+
else:
904+
index = keys
905+
return Series(counts, index=index)
898906

899907
def _quantile(self, qs: npt.NDArray[np.float64], interpolation: str):
900908

pandas/core/base.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@
8181
NumpyValueArrayLike,
8282
)
8383

84-
from pandas import Categorical
84+
from pandas import (
85+
Categorical,
86+
Series,
87+
)
8588

8689

8790
_shared_docs: dict[str, str] = {}
@@ -161,7 +164,7 @@ def _freeze(self):
161164
object.__setattr__(self, "__frozen", True)
162165

163166
# prevent adding any attribute via s.xxx.new_attribute = ...
164-
def __setattr__(self, key: str, value):
167+
def __setattr__(self, key: str, value) -> None:
165168
# _cache is used by a decorator
166169
# We need to check both 1.) cls.__dict__ and 2.) getattr(self, key)
167170
# because
@@ -765,7 +768,7 @@ def hasnans(self) -> bool:
765768
# has no attribute "any"
766769
return bool(isna(self).any()) # type: ignore[union-attr]
767770

768-
def isna(self):
771+
def isna(self) -> npt.NDArray[np.bool_]:
769772
return isna(self._values)
770773

771774
def _reduce(
@@ -890,7 +893,7 @@ def value_counts(
890893
ascending: bool = False,
891894
bins=None,
892895
dropna: bool = True,
893-
):
896+
) -> Series:
894897
"""
895898
Return a Series containing counts of unique values.
896899

pandas/core/common.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def temp_setattr(obj, attr: str, value) -> Iterator[None]:
553553
setattr(obj, attr, old_value)
554554

555555

556-
def require_length_match(data, index: Index):
556+
def require_length_match(data, index: Index) -> None:
557557
"""
558558
Check the length of data matches the length of the index.
559559
"""
@@ -665,7 +665,9 @@ def resolve_numeric_only(numeric_only: bool | None | lib.NoDefault) -> bool:
665665
return result
666666

667667

668-
def deprecate_numeric_only_default(cls: type, name: str, deprecate_none: bool = False):
668+
def deprecate_numeric_only_default(
669+
cls: type, name: str, deprecate_none: bool = False
670+
) -> None:
669671
"""Emit FutureWarning message for deprecation of numeric_only.
670672
671673
See GH#46560 for details on the deprecation.

pandas/core/config_init.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"""
3838

3939

40-
def use_bottleneck_cb(key):
40+
def use_bottleneck_cb(key) -> None:
4141
from pandas.core import nanops
4242

4343
nanops.set_use_bottleneck(cf.get_option(key))
@@ -51,7 +51,7 @@ def use_bottleneck_cb(key):
5151
"""
5252

5353

54-
def use_numexpr_cb(key):
54+
def use_numexpr_cb(key) -> None:
5555
from pandas.core.computation import expressions
5656

5757
expressions.set_use_numexpr(cf.get_option(key))
@@ -65,7 +65,7 @@ def use_numexpr_cb(key):
6565
"""
6666

6767

68-
def use_numba_cb(key):
68+
def use_numba_cb(key) -> None:
6969
from pandas.core.util import numba_
7070

7171
numba_.set_use_numba(cf.get_option(key))
@@ -329,7 +329,7 @@ def use_numba_cb(key):
329329
"""
330330

331331

332-
def table_schema_cb(key):
332+
def table_schema_cb(key) -> None:
333333
from pandas.io.formats.printing import enable_data_resource_formatter
334334

335335
enable_data_resource_formatter(cf.get_option(key))
@@ -500,7 +500,7 @@ def _deprecate_negative_int_max_colwidth(key):
500500
# or we'll hit circular deps.
501501

502502

503-
def use_inf_as_na_cb(key):
503+
def use_inf_as_na_cb(key) -> None:
504504
from pandas.core.dtypes.missing import _use_inf_as_na
505505

506506
_use_inf_as_na(key)
@@ -720,7 +720,7 @@ def use_inf_as_na_cb(key):
720720
"""
721721

722722

723-
def register_plotting_backend_cb(key):
723+
def register_plotting_backend_cb(key) -> None:
724724
if key == "matplotlib":
725725
# We defer matplotlib validation, since it's the default
726726
return
@@ -746,7 +746,7 @@ def register_plotting_backend_cb(key):
746746
"""
747747

748748

749-
def register_converter_cb(key):
749+
def register_converter_cb(key) -> None:
750750
from pandas.plotting import (
751751
deregister_matplotlib_converters,
752752
register_matplotlib_converters,

pandas/core/flags.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def allows_duplicate_labels(self) -> bool:
8181
return self._allows_duplicate_labels
8282

8383
@allows_duplicate_labels.setter
84-
def allows_duplicate_labels(self, value: bool):
84+
def allows_duplicate_labels(self, value: bool) -> None:
8585
value = bool(value)
8686
obj = self._obj()
8787
if obj is None:
@@ -99,12 +99,12 @@ def __getitem__(self, key):
9999

100100
return getattr(self, key)
101101

102-
def __setitem__(self, key, value):
102+
def __setitem__(self, key, value) -> None:
103103
if key not in self._keys:
104104
raise ValueError(f"Unknown flag {key}. Must be one of {self._keys}")
105105
setattr(self, key, value)
106106

107-
def __repr__(self):
107+
def __repr__(self) -> str:
108108
return f"<Flags(allows_duplicate_labels={self.allows_duplicate_labels})>"
109109

110110
def __eq__(self, other):

0 commit comments

Comments
 (0)