Skip to content

Commit

Permalink
TYP: add Shape alias to pandas._typing (#37128)
Browse files Browse the repository at this point in the history
  • Loading branch information
arw2019 authored Nov 4, 2020
1 parent f7cd658 commit ba8d823
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 24 deletions.
2 changes: 2 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Mapping,
Optional,
Sequence,
Tuple,
Type,
TypeVar,
Union,
Expand Down Expand Up @@ -93,6 +94,7 @@
Label = Optional[Hashable]
IndexLabel = Union[Label, Sequence[Label]]
Level = Union[Label, int]
Shape = Tuple[int, ...]
Ordered = Optional[bool]
JSONSerializable = Optional[Union[PythonScalar, List, Dict]]
Axes = Collection
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Any, Sequence, Tuple, TypeVar
from typing import Any, Sequence, TypeVar

import numpy as np

from pandas._libs import lib
from pandas._typing import Shape
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import cache_readonly, doc
Expand Down Expand Up @@ -93,7 +94,7 @@ def _validate_fill_value(self, fill_value):
# TODO: make this a cache_readonly; for that to work we need to remove
# the _index_data kludge in libreduction
@property
def shape(self) -> Tuple[int, ...]:
def shape(self) -> Shape:
return self._ndarray.shape

def __len__(self) -> int:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy as np

from pandas._libs import lib
from pandas._typing import ArrayLike
from pandas._typing import ArrayLike, Shape
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
Expand Down Expand Up @@ -403,7 +403,7 @@ def dtype(self) -> ExtensionDtype:
raise AbstractMethodError(self)

@property
def shape(self) -> Tuple[int, ...]:
def shape(self) -> Shape:
"""
Return a tuple of the array dimensions.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
ints_to_pytimedelta,
)
from pandas._libs.tslibs.timezones import tz_compare
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar, Shape
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -1591,7 +1591,7 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:


def cast_scalar_to_array(
shape: Tuple, value: Scalar, dtype: Optional[DtypeObj] = None
shape: Shape, value: Scalar, dtype: Optional[DtypeObj] = None
) -> np.ndarray:
"""
Create np.ndarray of specified shape and dtype, filled with values.
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from pandas._libs import NaT, iNaT, lib
import pandas._libs.groupby as libgroupby
import pandas._libs.reduction as libreduction
from pandas._typing import F, FrameOrSeries, Label
from pandas._typing import F, FrameOrSeries, Label, Shape
from pandas.errors import AbstractMethodError
from pandas.util._decorators import cache_readonly

Expand Down Expand Up @@ -116,7 +116,7 @@ def groupings(self) -> List["grouper.Grouping"]:
return self._groupings

@property
def shape(self) -> Tuple[int, ...]:
def shape(self) -> Shape:
return tuple(ping.ngroups for ping in self.groupings)

def __iter__(self):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pandas._libs.lib import is_datetime_array, no_default
from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime, Timestamp
from pandas._libs.tslibs.timezones import tz_compare
from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label, final
from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label, Shape, final
from pandas.compat.numpy import function as nv
from pandas.errors import DuplicateLabelError, InvalidIndexError
from pandas.util._decorators import Appender, cache_readonly, doc
Expand Down Expand Up @@ -5644,7 +5644,7 @@ def _maybe_disable_logical_methods(self, opname: str_t):
make_invalid_op(opname)(self)

@property
def shape(self):
def shape(self) -> Shape:
"""
Return a tuple of the shape of the underlying data.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from pandas._libs import algos as libalgos, index as libindex, lib
from pandas._libs.hashtable import duplicated_int64
from pandas._typing import AnyArrayLike, Label, Scalar
from pandas._typing import AnyArrayLike, Label, Scalar, Shape
from pandas.compat.numpy import function as nv
from pandas.errors import InvalidIndexError, PerformanceWarning, UnsortedIndexError
from pandas.util._decorators import Appender, cache_readonly, doc
Expand Down Expand Up @@ -702,7 +702,7 @@ def array(self):
)

@property
def shape(self):
def shape(self) -> Shape:
"""
Return a tuple of the shape of the underlying data.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pandas._libs.internals import BlockPlacement
from pandas._libs.tslibs import conversion
from pandas._libs.tslibs.timezones import tz_compare
from pandas._typing import ArrayLike, Scalar
from pandas._typing import ArrayLike, Scalar, Shape
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -2762,7 +2762,7 @@ def _block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike:
return values


def safe_reshape(arr, new_shape):
def safe_reshape(arr, new_shape: Shape):
"""
If possible, reshape `arr` to have shape `new_shape`,
with a couple of exceptions (see gh-13012):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np

from pandas._libs import NaT, internals as libinternals
from pandas._typing import DtypeObj
from pandas._typing import DtypeObj, Shape
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.cast import maybe_promote
Expand Down Expand Up @@ -175,7 +175,7 @@ def _get_mgr_concatenation_plan(mgr, indexers):


class JoinUnit:
def __init__(self, block, shape, indexers=None):
def __init__(self, block, shape: Shape, indexers=None):
# Passing shape explicitly is required for cases when block is None.
if indexers is None:
indexers = {}
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np

from pandas._libs import internals as libinternals, lib
from pandas._typing import ArrayLike, DtypeObj, Label
from pandas._typing import ArrayLike, DtypeObj, Label, Shape
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -204,7 +204,7 @@ def __nonzero__(self) -> bool:
__bool__ = __nonzero__

@property
def shape(self) -> Tuple[int, ...]:
def shape(self) -> Shape:
return tuple(len(ax) for ax in self.axes)

@property
Expand Down Expand Up @@ -1825,7 +1825,7 @@ def _asarray_compat(x):
else:
return np.asarray(x)

def _shape_compat(x):
def _shape_compat(x) -> Shape:
if isinstance(x, ABCSeries):
return (len(x),)
else:
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from datetime import timedelta
from functools import partial
import operator
from typing import Any, Tuple
from typing import Any
import warnings

import numpy as np

from pandas._libs import Timedelta, Timestamp, lib, ops as libops
from pandas._typing import ArrayLike
from pandas._typing import ArrayLike, Shape

from pandas.core.dtypes.cast import (
construct_1d_object_array_from_listlike,
Expand Down Expand Up @@ -427,7 +427,7 @@ def maybe_upcast_datetimelike_array(obj: ArrayLike) -> ArrayLike:
return obj


def _maybe_upcast_for_op(obj, shape: Tuple[int, ...]):
def _maybe_upcast_for_op(obj, shape: Shape):
"""
Cast non-pandas objects to pandas types to unify behavior of arithmetic
and comparison operations.
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from pandas._libs import lib, writers as libwriters
from pandas._libs.tslibs import timezones
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label, Shape
from pandas.compat._optional import import_optional_dependency
from pandas.compat.pickle_compat import patch_pickle
from pandas.errors import PerformanceWarning
Expand Down Expand Up @@ -3091,7 +3091,7 @@ class BlockManagerFixed(GenericFixed):
nblocks: int

@property
def shape(self):
def shape(self) -> Optional[Shape]:
try:
ndim = self.ndim

Expand Down

0 comments on commit ba8d823

Please sign in to comment.