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

Update type hints to python3 style (non-array files) #25802

Merged
merged 16 commits into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
6 changes: 2 additions & 4 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,7 @@ def base(self):
return self.values.base

@property
def array(self):
# type: () -> ExtensionArray
def array(self) -> ExtensionArray:
"""
The ExtensionArray of the data backing this Series or Index.

Expand Down Expand Up @@ -962,8 +961,7 @@ def to_numpy(self, dtype=None, copy=False):
return result

@property
def _ndarray_values(self):
# type: () -> np.ndarray
def _ndarray_values(self) -> np.ndarray:
"""
The data as an ndarray, possibly losing information.

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ def maybe_box_datetimelike(value):
values_from_object = lib.values_from_object


def is_bool_indexer(key):
# type: (Any) -> bool
def is_bool_indexer(key: Any) -> bool:
"""
Check whether `key` is a valid boolean indexer.

Expand Down
17 changes: 6 additions & 11 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Extend pandas with custom array types"""
from typing import List, Optional, Type
from typing import List, Optional

import numpy as np

Expand Down Expand Up @@ -65,8 +65,7 @@ def __ne__(self, other):
return not self.__eq__(other)

@property
def names(self):
# type: () -> Optional[List[str]]
def names(self) -> Optional[List[str]]:
"""Ordered list of field names, or None if there are no fields.

This is for compatibility with NumPy arrays, and may be removed in the
Expand Down Expand Up @@ -116,8 +115,7 @@ def is_dtype(cls, dtype):
return False

@property
def _is_numeric(self):
# type: () -> bool
def _is_numeric(self) -> bool:
"""
Whether columns with this dtype should be considered numeric.

Expand All @@ -128,8 +126,7 @@ def _is_numeric(self):
return False

@property
def _is_boolean(self):
# type: () -> bool
def _is_boolean(self) -> bool:
"""
Whether this dtype should be considered boolean.

Expand Down Expand Up @@ -212,8 +209,7 @@ def __str__(self):
return self.name

@property
def type(self):
# type: () -> Type
def type(self) -> bool:
gwrome marked this conversation as resolved.
Show resolved Hide resolved
"""
The scalar type for the array, e.g. ``int``

Expand Down Expand Up @@ -242,8 +238,7 @@ def kind(self):
return 'O'

@property
def name(self):
# type: () -> str
def name(self) -> str:
"""
A string identifying the data type.

Expand Down
10 changes: 5 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@
Index(['value'], dtype='object')
"""


# -----------------------------------------------------------------------
# DataFrame class

Expand Down Expand Up @@ -6243,11 +6244,10 @@ def diff(self, periods=1, axis=0):
# Function application

def _gotitem(self,
key, # type: Union[str, List[str]]
ndim, # type: int
subset=None # type: Union[Series, DataFrame, None]
):
# type: (...) -> Union[Series, DataFrame]
key: Union[str, List[str]],
ndim: int,
subset: Union[Series, 'DataFrame', None] = None,
gwrome marked this conversation as resolved.
Show resolved Hide resolved
) -> Union[Series, 'DataFrame']:
"""
Sub-classes to define. Return a sliced object.

Expand Down
16 changes: 7 additions & 9 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class providing the base-class of operations.
import datetime
from functools import partial, wraps
import types
from typing import Optional, Type
from typing import Optional, Tuple, Type
import warnings

import numpy as np
Expand Down Expand Up @@ -1040,17 +1040,15 @@ def _bool_agg(self, val_test, skipna):
Shared func to call any / all Cython GroupBy implementations.
"""

def objs_to_bool(vals):
# type: (np.ndarray) -> (np.ndarray, Type)
def objs_to_bool(vals: np.ndarray) -> Tuple[np.ndarray, Type]:
if is_object_dtype(vals):
vals = np.array([bool(x) for x in vals])
else:
vals = vals.astype(np.bool)

return vals.view(np.uint8), np.bool

def result_to_bool(result, inference):
# type: (np.ndarray, Type) -> np.ndarray
def result_to_bool(result: np.ndarray, inference: Type) -> np.ndarray:
return result.astype(inference, copy=False)

return self._get_cythonized_result('group_any_all', self.grouper,
Expand Down Expand Up @@ -1738,8 +1736,8 @@ def quantile(self, q=0.5, interpolation='linear'):
b 3.0
"""

def pre_processor(vals):
# type: (np.ndarray) -> (np.ndarray, Optional[Type])
def pre_processor(vals: np.ndarray) -> \
gwrome marked this conversation as resolved.
Show resolved Hide resolved
Tuple[np.ndarray, Optional[Type]]:
if is_object_dtype(vals):
raise TypeError("'quantile' cannot be performed against "
"'object' dtypes!")
Expand All @@ -1753,8 +1751,8 @@ def pre_processor(vals):

return vals, inference

def post_processor(vals, inference):
# type: (np.ndarray, Optional[Type]) -> np.ndarray
def post_processor(vals: np.ndarray, inference: Optional[Type]) -> \
gwrome marked this conversation as resolved.
Show resolved Hide resolved
np.ndarray:
if inference:
# Check for edge case
if not (is_integer_dtype(inference) and
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3632,8 +3632,7 @@ def values(self):
return self._data.view(np.ndarray)

@property
def _values(self):
# type: () -> Union[ExtensionArray, Index, np.ndarray]
def _values(self) -> Union[ExtensionArray, 'Index', np.ndarray]:
Copy link
Member

Choose a reason for hiding this comment

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

Is Index not a valid type here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it's used inside the Index class itself, Index isn't defined yet when types are checked, so we need to use a forward reference literal. See PEP 484. In that context, Index alone is an unresolved reference.

# TODO(EA): remove index types as they become extension arrays
"""
The best array representation.
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DatetimeIndexOpsMixin(ExtensionOpsMixin):
"""
common ops mixin to support a unified interface datetimelike Index
"""
_data = None # type: DatetimeLikeArrayMixin
_data: DatetimeLikeArrayMixin = None

# DatetimeLikeArrayMixin assumes subclasses are mutable, so these are
# properties there. They can be made into cache_readonly for Index
Expand Down Expand Up @@ -129,8 +129,7 @@ def _ndarray_values(self):
# Abstract data attributes

@property
def values(self):
# type: () -> np.ndarray
def values(self) -> np.ndarray:
# Note: PeriodArray overrides this to return an ndarray of objects.
return self._data._data

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index, PeriodDelegateMixin):
_is_numeric_dtype = False
_infer_as_myclass = True

_data = None # type: PeriodArray
_data: PeriodArray = None

_engine_type = libindex.PeriodEngine

Expand Down
9 changes: 4 additions & 5 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools
import inspect
import re
from typing import Any, List
from typing import Any, List, Optional
import warnings

import numpy as np
Expand Down Expand Up @@ -1828,10 +1828,9 @@ def interpolate(self, method='pad', axis=0, inplace=False, limit=None,
placement=self.mgr_locs)

def shift(self,
periods, # type: int
axis=0, # type: libinternals.BlockPlacement
fill_value=None): # type: Any
# type: (...) -> List[ExtensionBlock]
periods: int,
axis: libinternals.BlockPlacement = 0,
fill_value: Optional[Any] = None) -> List['ExtensionBlock']:
gwrome marked this conversation as resolved.
Show resolved Hide resolved
"""
Shift the block by `periods`.

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,8 +1863,8 @@ def _shape_compat(x):
return stacked, placement


def _interleaved_dtype(blocks):
# type: (List[Block]) -> Optional[Union[np.dtype, ExtensionDtype]]
def _interleaved_dtype(blocks: List[Block]) \
gwrome marked this conversation as resolved.
Show resolved Hide resolved
-> Optional[Union[np.dtype, ExtensionDtype]]:
"""Find the common dtype for `blocks`.

Parameters
Expand Down