Skip to content

TYP: check_untyped_defs pandas.core.nanops #34689

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 4 commits into from
Jun 14, 2020
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
20 changes: 13 additions & 7 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(self, name=None, **kwargs):
self.name = name
self.kwargs = kwargs

def __call__(self, alt):
def __call__(self, alt: F) -> F:
bn_name = self.name or alt.__name__

try:
Expand Down Expand Up @@ -130,7 +130,7 @@ def f(

return result

return f
return cast(F, f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the cast required here for? Wouldn't the return annotation already provide this info?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how we have been typing all our decorators to preserve the signature. Its recommended best practice. https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators

if we don't cast, we get a mypy error pandas\core\nanops.py:133: error: Incompatible return value type (got "Callable[..., Any]", expected "F")



def _bn_ok_dtype(dtype: DtypeObj, name: str) -> bool:
Expand Down Expand Up @@ -514,7 +514,12 @@ def nansum(

@disallow(PeriodDtype)
@bottleneck_switch()
def nanmean(values, axis=None, skipna=True, mask=None):
def nanmean(
values: np.ndarray,
axis: Optional[int] = None,
skipna: bool = True,
mask: Optional[np.ndarray] = None,
) -> float:
"""
Compute the mean of the element along an axis ignoring NaNs

Expand All @@ -528,7 +533,7 @@ def nanmean(values, axis=None, skipna=True, mask=None):

Returns
-------
result : float
float
Unless input is a float array, in which case use the same
precision as the input array.

Expand Down Expand Up @@ -558,6 +563,7 @@ def nanmean(values, axis=None, skipna=True, mask=None):
the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_sum))

if axis is not None and getattr(the_sum, "ndim", False):
count = cast(np.ndarray, count)
with np.errstate(all="ignore"):
# suppress division by zero warnings
the_mean = the_sum / count
Expand Down Expand Up @@ -1205,17 +1211,17 @@ def _maybe_arg_null_out(


def _get_counts(
values_shape: Tuple[int],
values_shape: Tuple[int, ...],
mask: Optional[np.ndarray],
axis: Optional[int],
dtype: Dtype = float,
) -> Union[int, np.ndarray]:
) -> Union[int, float, np.ndarray]:
"""
Get the count of non-null values along an axis

Parameters
----------
values_shape : Tuple[int]
values_shape : tuple of int
shape tuple from values ndarray, used if mask is None
mask : Optional[ndarray[bool]]
locations in values that should be considered missing
Expand Down
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,6 @@ check_untyped_defs=False
[mypy-pandas.core.missing]
check_untyped_defs=False

[mypy-pandas.core.nanops]
check_untyped_defs=False

[mypy-pandas.core.ops.docstrings]
check_untyped_defs=False

Expand Down