Skip to content

TYP: some types for pandas/core/arrays/sparse/array.py #29898

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
Changes from 1 commit
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
21 changes: 15 additions & 6 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections import abc
import numbers
import operator
from typing import Any, Callable
from typing import Any, Callable, List, Type, TypeVar
import warnings

import numpy as np
Expand Down Expand Up @@ -50,6 +50,8 @@

from .dtype import SparseDtype

_SparseArrayT = TypeVar("_SparseArrayT", bound="SparseArray")

# ----------------------------------------------------------------------------
# Array

Expand Down Expand Up @@ -260,6 +262,7 @@ class SparseArray(PandasObject, ExtensionArray, ExtensionOpsMixin):
_pandas_ftype = "sparse"
_subtyp = "sparse_array" # register ABCSparseArray
_deprecations = PandasObject._deprecations | frozenset(["get_values"])
_sparse_index: SparseIndex

def __init__(
self,
Expand Down Expand Up @@ -372,8 +375,11 @@ def __init__(

@classmethod
def _simple_new(
cls, sparse_array: np.ndarray, sparse_index: SparseIndex, dtype: SparseDtype
) -> ABCSparseArray:
cls: Type[_SparseArrayT],
Copy link
Member

Choose a reason for hiding this comment

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

needing to annotate cls seems weird. is that just me?

Copy link
Member Author

Choose a reason for hiding this comment

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

annotated for generic self. see https://mypy.readthedocs.io/en/latest/generics.html#generic-methods-and-generic-self

again could see how far we get without this.

sparse_array: np.ndarray,
sparse_index: SparseIndex,
dtype: SparseDtype,
) -> _SparseArrayT:
new = cls([])
new._sparse_index = sparse_index
new._sparse_values = sparse_array
Expand Down Expand Up @@ -805,13 +811,14 @@ def _get_val_at(self, loc):
else:
return libindex.get_value_at(self.sp_values, sp_loc)

def take(self, indices, allow_fill=False, fill_value=None):
def take(self, indices, allow_fill: bool = False, fill_value=None):
if is_scalar(indices):
raise ValueError(
"'indices' must be an array, not a scalar '{}'.".format(indices)
)
indices = np.asarray(indices, dtype=np.int32)

result: List
Copy link
Member

Choose a reason for hiding this comment

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

not saying anything about contents?

Copy link
Member Author

Choose a reason for hiding this comment

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

not until _take_with_fill and _take_without_fill are annotated. removed till then.

if indices.size == 0:
result = []
kwargs = {"dtype": self.dtype}
Expand Down Expand Up @@ -1392,8 +1399,10 @@ def __abs__(self):
# ------------------------------------------------------------------------

@classmethod
def _create_unary_method(cls, op):
def sparse_unary_method(self):
def _create_unary_method(
cls: Type[_SparseArrayT], op
) -> Callable[[_SparseArrayT], _SparseArrayT]:
Copy link
Member

Choose a reason for hiding this comment

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

does mypy specifically need this? my kneejerk reaction is that it adds verbosity but not clarity

Copy link
Member Author

Choose a reason for hiding this comment

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

we could see how far we get without it. but remember your frustration with subclass of ChainMap not returning the same type?

Copy link
Member

Choose a reason for hiding this comment

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

In case I haven't mentioned it recently, I'm totally happy to defer to you when it comes to annotations

def sparse_unary_method(self: _SparseArrayT) -> _SparseArrayT:
fill_value = op(np.array(self.fill_value)).item()
values = op(self.sp_values)
dtype = SparseDtype(values.dtype, fill_value)
Expand Down