-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
TYP: check_untyped_defs pandas.core.nanops #34689
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor question otherwise looks good
@@ -130,7 +130,7 @@ def f( | |||
|
|||
return result | |||
|
|||
return f | |||
return cast(F, f) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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")
pandas/core/nanops.py
Outdated
values: np.ndarray, | ||
axis: Optional[int] = None, | ||
skipna: bool = True, | ||
mask: np.ndarray = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be typed as Optional[np.ndarray]
, since the default is None?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep. good catch. np.ndarray evaluates to Any, so None is compatible. will update.
pandas/core/nanops.py
Outdated
@@ -557,7 +562,8 @@ def nanmean(values, axis=None, skipna=True, mask=None): | |||
count = _get_counts(values.shape, mask, axis, dtype=dtype_count) | |||
the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_sum)) | |||
|
|||
if axis is not None and getattr(the_sum, "ndim", False): | |||
if not is_scalar(count): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those two if statements don't seem equivalent on first sight?
(they might be in practice though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, if count is a scalar, ct_mask = count == 0 would evaluate to a bool and then ct_mask.any() would raise an AttributeError.
pandas\core\nanops.py:571: error: Item "bool" of "Union[bool, Any]" has no attribute "any"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverted.
@WillAyd @jorisvandenbossche updated + green |
Lgtm @jorisvandenbossche merge if nothing outstanding |
thanks @simonjayhawkins |
pandas\core\nanops.py:565: error: Item "bool" of "Union[bool, Any]" has no attribute "any"