Skip to content

TYP: partial typing of masked array #31728

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 3 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
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
Prev Previous commit
address comments
  • Loading branch information
simonjayhawkins committed Feb 6, 2020
commit 554ddc0f2de492d41ec2cdec09d413a1420245c8
5 changes: 3 additions & 2 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np

from pandas._libs import lib, missing as libmissing
from pandas._typing import ArrayLike
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv

Expand Down Expand Up @@ -382,7 +383,7 @@ def __setitem__(self, key, value) -> None:
self._data[key] = value
self._mask[key] = mask

def astype(self, dtype, copy: bool = True) -> Union[np.ndarray, BaseMaskedArray]:
def astype(self, dtype, copy: bool = True) -> ArrayLike:
"""
Cast to a NumPy array or ExtensionArray with 'dtype'.

Expand All @@ -397,7 +398,7 @@ def astype(self, dtype, copy: bool = True) -> Union[np.ndarray, BaseMaskedArray]

Returns
-------
array : ndarray or ExtensionArray
ndarray or ExtensionArray
NumPy ndarray, BooleanArray or IntegerArray with 'dtype' for its dtype.

Raises
Expand Down
9 changes: 5 additions & 4 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np

from pandas._libs import lib, missing as libmissing
from pandas._typing import ArrayLike
from pandas.compat import set_function_name
from pandas.util._decorators import cache_readonly

Expand Down Expand Up @@ -425,9 +426,9 @@ def __setitem__(self, key, value) -> None:
self._data[key] = value
self._mask[key] = mask

def astype(self, dtype, copy: bool = True) -> Union[np.ndarray, BaseMaskedArray]:
def astype(self, dtype, copy: bool = True) -> ArrayLike:
"""
Cast to a NumPy array or IntegerArray with 'dtype'.
Cast to a NumPy array or ExtensionArray with 'dtype'.

Parameters
----------
Expand All @@ -440,8 +441,8 @@ def astype(self, dtype, copy: bool = True) -> Union[np.ndarray, BaseMaskedArray]

Returns
-------
array : ndarray or IntegerArray or BooleanArray
NumPy ndarray, IntergerArray or BooleanArray with 'dtype' for its dtype.
ndarray or ExtensionArray
NumPy ndarray, BooleanArray or IntegerArray with 'dtype' for its dtype.

Raises
------
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np

from pandas._libs import lib, missing as libmissing
from pandas._typing import Scalar

from pandas.core.dtypes.common import is_integer, is_object_dtype, is_string_dtype
from pandas.core.dtypes.missing import isna, notna
Expand All @@ -12,7 +13,6 @@
from pandas.core.indexers import check_array_indexer

if TYPE_CHECKING:
from pandas._typing import Scalar
from pandas import Series


Expand All @@ -27,7 +27,7 @@ class BaseMaskedArray(ExtensionArray, ExtensionOpsMixin):
"""

# The value used to fill '_data' to avoid upcasting
_internal_fill_value: "Scalar"
_internal_fill_value: Scalar

def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
Copy link
Member Author

Choose a reason for hiding this comment

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

__init__ needs to be declared in the base class...

pandas\core\arrays\masked.py:56: error: Too many arguments for "BaseMaskedArray"
pandas\core\arrays\masked.py:181: error: Too many arguments for "BaseMaskedArray"
pandas\core\arrays\masked.py:207: error: Too many arguments for "BaseMaskedArray"
pandas\core\arrays\masked.py:207: error: Unexpected keyword argument "copy" for "BaseMaskedArray"
pandas\core\arrays\masked.py:213: error: Too many arguments for "BaseMaskedArray"
pandas\core\arrays\masked.py:213: error: Unexpected keyword argument "copy" for "BaseMaskedArray"

also creating this ensures that the subclasses have the correct signature for the constructor to work with __invert__, _concat_same_type, take and copy from the base class.

we could just use a AbstractMethodError but I think it makes sense to put the shared functionality here.

BooleanArray has checking for values.ndim and mask.ndim. IntegerArray does not. It may make sense to have that check here also if applicable to IntegerArray.

if copy:
Expand Down Expand Up @@ -61,7 +61,7 @@ def __invert__(self: BaseMaskedArrayT) -> BaseMaskedArrayT:
return type(self)(~self._data, self._mask)

def to_numpy(
self, dtype=None, copy: bool = False, na_value: "Scalar" = lib.no_default,
self, dtype=None, copy: bool = False, na_value: Scalar = lib.no_default,
) -> np.ndarray:
"""
Convert to a NumPy Array.
Expand Down Expand Up @@ -189,7 +189,7 @@ def take(
self: BaseMaskedArrayT,
Copy link
Member

Choose a reason for hiding this comment

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

We otherwise don't type self, or do we?
(self is always the type of the class, no?)

indexer,
allow_fill: bool = False,
fill_value: Optional["Scalar"] = None,
fill_value: Optional[Scalar] = None,
) -> BaseMaskedArrayT:
# we always fill with 1 internally
# to avoid upcasting
Expand Down