Skip to content
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

#26065 Fix Type Annotations in pandas.core.arrays #26071

Merged
merged 11 commits into from
Apr 30, 2019
24 changes: 0 additions & 24 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,6 @@ ignore_errors=True
[mypy-pandas.core.apply]
ignore_errors=True

[mypy-pandas.core.arrays.array_]
ignore_errors=True

[mypy-pandas.core.arrays.datetimelike]
ignore_errors=True

[mypy-pandas.core.arrays.integer]
ignore_errors=True

[mypy-pandas.core.arrays.interval]
ignore_errors=True

[mypy-pandas.core.arrays.numpy_]
ignore_errors=True

[mypy-pandas.core.arrays.period]
ignore_errors=True

[mypy-pandas.core.arrays.sparse]
ignore_errors=True

[mypy-pandas.core.arrays.timedeltas]
ignore_errors=True

[mypy-pandas.core.base]
ignore_errors=True

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/array_.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Sequence, Union
from typing import Optional, Sequence, Union, cast

import numpy as np

Expand Down Expand Up @@ -229,7 +229,7 @@ def array(data: Sequence[object],
dtype = registry.find(dtype) or dtype

if is_extension_array_dtype(dtype):
cls = dtype.construct_array_type()
cls = cast(ExtensionDtype, dtype).construct_array_type()
gwrome marked this conversation as resolved.
Show resolved Hide resolved
return cls._from_sequence(data, dtype=dtype, copy=copy)

if dtype is None:
Expand Down
13 changes: 8 additions & 5 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
import operator
from typing import Any, Sequence, Tuple, Type, Union
from typing import Any, Sequence, Type, Union, cast
import warnings

import numpy as np
Expand Down Expand Up @@ -40,6 +40,7 @@


class AttributesMixin(object):
_data = None # type: np.ndarray

@property
def _attributes(self):
Expand All @@ -57,7 +58,8 @@ def _get_attributes_dict(self):
return {k: getattr(self, k, None) for k in self._attributes}

@property
def _scalar_type(self) -> Union[Type, Tuple[Type]]:
def _scalar_type(self) -> Union[Type[Period], Type[Timestamp],
gwrome marked this conversation as resolved.
Show resolved Hide resolved
Type[Timedelta]]:
"""The scalar associated with this datelike

* PeriodArray : Period
Expand Down Expand Up @@ -479,12 +481,13 @@ def __setitem__(
raise ValueError("setting an array element with a sequence.")

if (not is_slice
and len(key) != len(value)
and len(cast(Sequence, key)) != len(value)
gwrome marked this conversation as resolved.
Show resolved Hide resolved
and not com.is_bool_indexer(key)):
msg = ("shape mismatch: value array of length '{}' does not "
"match indexing result of length '{}'.")
raise ValueError(msg.format(len(key), len(value)))
if not is_slice and len(key) == 0:
raise ValueError(msg.format(
len(cast(Sequence, key)), len(value)))
if not is_slice and len(cast(Sequence, key)) == 0:
return

value = type(self)._from_sequence(value, dtype=self.dtype)
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import sys
from typing import Type
import warnings

import numpy as np
Expand Down Expand Up @@ -31,9 +32,9 @@ class _IntegerDtype(ExtensionDtype):

The attributes name & type are set when these subclasses are created.
"""
name = None
name = None # type: str
base = None
type = None
type = None # type: Type
na_value = np.nan

def __repr__(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ def mid(self):
else False
"""

@property
@property # type: ignore # Mypy does not support decorated properties
gwrome marked this conversation as resolved.
Show resolved Hide resolved
@Appender(_interval_shared_docs['is_non_overlapping_monotonic']
% _shared_docs_kwargs)
def is_non_overlapping_monotonic(self):
Expand Down
21 changes: 13 additions & 8 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from datetime import timedelta
import operator
from typing import Any, Callable, Optional, Sequence, Union
from typing import Any, Callable, List, Optional, Sequence, Union

import numpy as np

Expand All @@ -24,7 +24,7 @@
from pandas.core.dtypes.missing import isna, notna

import pandas.core.algorithms as algos
from pandas.core.arrays import ExtensionArray, datetimelike as dtl
from pandas.core.arrays import datetimelike as dtl
import pandas.core.common as com

from pandas.tseries import frequencies
Expand Down Expand Up @@ -95,7 +95,7 @@ class PeriodArray(dtl.DatetimeLikeArrayMixin, dtl.DatelikeOps):

Parameters
----------
values : Union[PeriodArray, Series[period], ndarary[int], PeriodIndex]
values : Union[PeriodArray, Series[period], ndarray[int], PeriodIndex]
The data to store. These should be arrays that can be directly
converted to ordinals without inference or copy (PeriodArray,
ndarray[int64]), or a box around such an array (Series[period],
Expand Down Expand Up @@ -136,7 +136,7 @@ class PeriodArray(dtl.DatetimeLikeArrayMixin, dtl.DatelikeOps):
_scalar_type = Period

# Names others delegate to us
_other_ops = []
_other_ops = [] # type: List[str]
_bool_ops = ['is_leap_year']
_object_ops = ['start_time', 'end_time', 'freq']
_field_ops = ['year', 'month', 'day', 'hour', 'minute', 'second',
Expand Down Expand Up @@ -190,7 +190,9 @@ def _from_sequence(
copy: bool = False,
) -> ABCPeriodArray:
if dtype:
freq = dtype.freq
freq = dtype.freq # type: ignore
gwrome marked this conversation as resolved.
Show resolved Hide resolved
# freq is set in PeriodDtype's __new__ method, so mypy doesn't
# recognize it
else:
freq = None

Expand Down Expand Up @@ -277,7 +279,7 @@ def _check_compatible_with(self, other):
def dtype(self):
return self._dtype

@property
@property # type: ignore # read-only property overwriting read/write
gwrome marked this conversation as resolved.
Show resolved Hide resolved
def freq(self):
"""
Return the frequency object for this PeriodArray.
Expand Down Expand Up @@ -539,10 +541,13 @@ def _sub_period(self, other):

return new_data

Other_types = Union[ABCPeriodArray, ABCSeries, ABCPeriodIndex,
gwrome marked this conversation as resolved.
Show resolved Hide resolved
np.ndarray]

@Appender(dtl.DatetimeLikeArrayMixin._addsub_int_array.__doc__)
def _addsub_int_array(
self,
other: Union[ExtensionArray, np.ndarray, ABCIndexClass],
other: Other_types,
op: Callable[[Any], Any]
) -> ABCPeriodArray:
assert op in [operator.add, operator.sub]
Expand Down Expand Up @@ -782,7 +787,7 @@ def period_array(
data = np.asarray(data)

if freq:
dtype = PeriodDtype(freq)
dtype = PeriodDtype(freq) # type: Optional[PeriodDtype]
WillAyd marked this conversation as resolved.
Show resolved Hide resolved
else:
dtype = None

Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from datetime import timedelta
import textwrap
from typing import List
import warnings

import numpy as np
Expand Down Expand Up @@ -131,8 +132,8 @@ class TimedeltaArray(dtl.DatetimeLikeArrayMixin, dtl.TimelikeOps):
_scalar_type = Timedelta
__array_priority__ = 1000
# define my properties & methods for delegation
_other_ops = []
_bool_ops = []
_other_ops = [] # type: List[str]
_bool_ops = [] # type: List[str]
_object_ops = ['freq']
_field_ops = ['days', 'seconds', 'microseconds', 'nanoseconds']
_datetimelike_ops = _field_ops + _object_ops + _bool_ops
Expand Down