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 @@ -14,24 +14,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]
DatetimeLike = Type[Union[Period, Timestamp, Timedelta]]
gwrome marked this conversation as resolved.
Show resolved Hide resolved
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,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, Union, cast
import warnings

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

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


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

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

* PeriodArray : Period
Expand Down Expand Up @@ -478,14 +480,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)))
if len(key) == 0:
gwrome marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 2 additions & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,8 @@ def mid(self):
else False
"""

@property
@property # type: ignore # https://github.com/python/mypy/issues/1362
gwrome marked this conversation as resolved.
Show resolved Hide resolved
# Mypy does not support decorated properties
@Appender(_interval_shared_docs['is_non_overlapping_monotonic']
% _shared_docs_kwargs)
def is_non_overlapping_monotonic(self):
Expand Down
15 changes: 8 additions & 7 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 @@ -277,7 +277,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,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 @@ -779,7 +780,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