Skip to content

TYP: overload maybe_downcast_numeric and maybe_downcast_to_dtype #46929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ def _disallow_mismatched_datetimelike(value, dtype: DtypeObj):
raise TypeError(f"Cannot cast {repr(value)} to {dtype}")


@overload
def maybe_downcast_to_dtype(result: np.ndarray, dtype: str | np.dtype) -> np.ndarray:
...


@overload
def maybe_downcast_to_dtype(result: ExtensionArray, dtype: str | np.dtype) -> ArrayLike:
...


def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLike:
"""
try to cast to the specified dtype (e.g. convert back to bool/int
Expand Down Expand Up @@ -301,6 +311,20 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
return result


@overload
def maybe_downcast_numeric(
result: np.ndarray, dtype: np.dtype, do_round: bool = False
) -> np.ndarray:
...


@overload
def maybe_downcast_numeric(
result: ExtensionArray, dtype: DtypeObj, do_round: bool = False
) -> ArrayLike:
...


def maybe_downcast_numeric(
result: ArrayLike, dtype: DtypeObj, do_round: bool = False
) -> ArrayLike:
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,7 @@ def _call_cython_op(
else:
op_result = result

# error: Incompatible return value type (got "Union[ExtensionArray, ndarray]",
# expected "ndarray")
return op_result # type: ignore[return-value]
return op_result

@final
def cython_operation(
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,11 +1141,7 @@ def interval_range(
if all(is_integer(x) for x in com.not_none(start, end, freq)):
# np.linspace always produces float output

# error: Incompatible types in assignment (expression has type
# "Union[ExtensionArray, ndarray]", variable has type "ndarray")
breaks = maybe_downcast_numeric( # type: ignore[assignment]
breaks, np.dtype("int64")
)
breaks = maybe_downcast_numeric(breaks, np.dtype("int64"))
else:
# delegate to the appropriate range function
if isinstance(endpoint, Timestamp):
Expand Down