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
18 changes: 0 additions & 18 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@ ignore_errors=True
[mypy-pandas.core.api]
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.period]
ignore_errors=True

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

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

Expand Down
7 changes: 6 additions & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from pathlib import Path
from typing import IO, AnyStr, Union
from typing import IO, AnyStr, Type, Union

import numpy as np

from pandas._libs import Timestamp
from pandas._libs.tslibs.period import Period
from pandas._libs.tslibs.timedeltas import Timedelta

from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import ABCExtensionArray

ArrayLike = Union[ABCExtensionArray, np.ndarray]
DatetimeLikeScalar = Type[Union[Period, Timestamp, Timedelta]]
Dtype = Union[str, np.dtype, ExtensionDtype]
FilePathOrBuffer = Union[str, Path, IO[AnyStr]]
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
24 changes: 14 additions & 10 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta
import operator
from typing import Any, Sequence, Tuple, Type, Union
from typing import Any, Sequence, Union, cast
import warnings

import numpy as np
Expand All @@ -27,6 +27,7 @@
from pandas.core.dtypes.inference import is_array_like
from pandas.core.dtypes.missing import isna

from pandas._typing import DatetimeLikeScalar
from pandas.core import missing, nanops
from pandas.core.algorithms import (
checked_add_with_arr, take, unique1d, value_counts)
Expand All @@ -39,6 +40,7 @@


class AttributesMixin:
_data = None # type: np.ndarray

@property
def _attributes(self):
Expand All @@ -56,7 +58,7 @@ 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) -> DatetimeLikeScalar:
"""The scalar associated with this datelike

* PeriodArray : Period
Expand Down Expand Up @@ -477,14 +479,16 @@ def __setitem__(
if lib.is_scalar(key):
raise ValueError("setting an array element with a sequence.")

if (not is_slice
and len(key) != len(value)
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:
return
if not is_slice:
key = cast(Sequence, key)
gwrome marked this conversation as resolved.
Show resolved Hide resolved
if (len(key) != len(value)
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)))
elif not len(key):
return

value = type(self)._from_sequence(value, dtype=self.dtype)
self._check_compatible_with(value)
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
5 changes: 3 additions & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,8 +939,9 @@ def mid(self):
points) and is either monotonic increasing or monotonic decreasing,
else False
"""

@property
# https://github.com/python/mypy/issues/1362
# Mypy does not support decorated properties
@property # type: ignore
@Appender(_interval_shared_docs['is_non_overlapping_monotonic']
% _shared_docs_kwargs)
def is_non_overlapping_monotonic(self):
Expand Down
17 changes: 10 additions & 7 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 @@ -23,7 +23,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 @@ -94,7 +94,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 @@ -135,7 +135,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 @@ -276,7 +276,8 @@ def _check_compatible_with(self, other):
def dtype(self):
return self._dtype

@property
# read-only property overwriting read/write
@property # type: ignore
def freq(self):
"""
Return the frequency object for this PeriodArray.
Expand Down Expand Up @@ -538,7 +539,8 @@ def _sub_period(self, other):
@Appender(dtl.DatetimeLikeArrayMixin._addsub_int_array.__doc__)
def _addsub_int_array(
self,
other: Union[ExtensionArray, np.ndarray, ABCIndexClass],
other: Union[ABCPeriodArray, ABCSeries,
ABCPeriodIndex, np.ndarray],
op: Callable[[Any], Any]
) -> ABCPeriodArray:
assert op in [operator.add, operator.sub]
Expand Down Expand Up @@ -778,7 +780,8 @@ def period_array(
data = np.asarray(data)

if freq:
dtype = PeriodDtype(freq)
# typed Optional here because the else block below assigns None
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,5 +1,6 @@
from datetime import timedelta
import textwrap
from typing import List
import warnings

import numpy as np
Expand Down Expand Up @@ -130,8 +131,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