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

Conversation

simonjayhawkins
Copy link
Member

@simonjayhawkins simonjayhawkins commented Jun 10, 2020

pandas\core\nanops.py:565: error: Item "bool" of "Union[bool, Any]" has no attribute "any"

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Jun 10, 2020
Copy link
Member

@WillAyd WillAyd left a 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)
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")

values: np.ndarray,
axis: Optional[int] = None,
skipna: bool = True,
mask: np.ndarray = None,
Copy link
Member

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?

Copy link
Member Author

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.

@@ -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):
Copy link
Member

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)

Copy link
Member Author

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"

Copy link
Member Author

Choose a reason for hiding this comment

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

reverted.

@simonjayhawkins
Copy link
Member Author

@WillAyd @jorisvandenbossche updated + green

@WillAyd
Copy link
Member

WillAyd commented Jun 13, 2020

Lgtm @jorisvandenbossche merge if nothing outstanding

@jreback jreback added this to the 1.1 milestone Jun 14, 2020
@jreback jreback merged commit 151b6e0 into pandas-dev:master Jun 14, 2020
@jreback
Copy link
Contributor

jreback commented Jun 14, 2020

thanks @simonjayhawkins

@simonjayhawkins simonjayhawkins deleted the pandas.core.nanops branch June 14, 2020 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants