Skip to content

Commit ba8d823

Browse files
authored
TYP: add Shape alias to pandas._typing (#37128)
1 parent f7cd658 commit ba8d823

File tree

12 files changed

+27
-24
lines changed

12 files changed

+27
-24
lines changed

pandas/_typing.py

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Mapping,
1616
Optional,
1717
Sequence,
18+
Tuple,
1819
Type,
1920
TypeVar,
2021
Union,
@@ -93,6 +94,7 @@
9394
Label = Optional[Hashable]
9495
IndexLabel = Union[Label, Sequence[Label]]
9596
Level = Union[Label, int]
97+
Shape = Tuple[int, ...]
9698
Ordered = Optional[bool]
9799
JSONSerializable = Optional[Union[PythonScalar, List, Dict]]
98100
Axes = Collection

pandas/core/arrays/_mixins.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from typing import Any, Sequence, Tuple, TypeVar
1+
from typing import Any, Sequence, TypeVar
22

33
import numpy as np
44

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

99100
def __len__(self) -> int:

pandas/core/arrays/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import numpy as np
1313

1414
from pandas._libs import lib
15-
from pandas._typing import ArrayLike
15+
from pandas._typing import ArrayLike, Shape
1616
from pandas.compat import set_function_name
1717
from pandas.compat.numpy import function as nv
1818
from pandas.errors import AbstractMethodError
@@ -403,7 +403,7 @@ def dtype(self) -> ExtensionDtype:
403403
raise AbstractMethodError(self)
404404

405405
@property
406-
def shape(self) -> Tuple[int, ...]:
406+
def shape(self) -> Shape:
407407
"""
408408
Return a tuple of the array dimensions.
409409
"""

pandas/core/dtypes/cast.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
ints_to_pytimedelta,
3333
)
3434
from pandas._libs.tslibs.timezones import tz_compare
35-
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar
35+
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar, Shape
3636
from pandas.util._validators import validate_bool_kwarg
3737

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

15921592

15931593
def cast_scalar_to_array(
1594-
shape: Tuple, value: Scalar, dtype: Optional[DtypeObj] = None
1594+
shape: Shape, value: Scalar, dtype: Optional[DtypeObj] = None
15951595
) -> np.ndarray:
15961596
"""
15971597
Create np.ndarray of specified shape and dtype, filled with values.

pandas/core/groupby/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from pandas._libs import NaT, iNaT, lib
2525
import pandas._libs.groupby as libgroupby
2626
import pandas._libs.reduction as libreduction
27-
from pandas._typing import F, FrameOrSeries, Label
27+
from pandas._typing import F, FrameOrSeries, Label, Shape
2828
from pandas.errors import AbstractMethodError
2929
from pandas.util._decorators import cache_readonly
3030

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

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

122122
def __iter__(self):

pandas/core/indexes/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from pandas._libs.lib import is_datetime_array, no_default
2828
from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime, Timestamp
2929
from pandas._libs.tslibs.timezones import tz_compare
30-
from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label, final
30+
from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label, Shape, final
3131
from pandas.compat.numpy import function as nv
3232
from pandas.errors import DuplicateLabelError, InvalidIndexError
3333
from pandas.util._decorators import Appender, cache_readonly, doc
@@ -5644,7 +5644,7 @@ def _maybe_disable_logical_methods(self, opname: str_t):
56445644
make_invalid_op(opname)(self)
56455645

56465646
@property
5647-
def shape(self):
5647+
def shape(self) -> Shape:
56485648
"""
56495649
Return a tuple of the shape of the underlying data.
56505650
"""

pandas/core/indexes/multi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from pandas._libs import algos as libalgos, index as libindex, lib
2222
from pandas._libs.hashtable import duplicated_int64
23-
from pandas._typing import AnyArrayLike, Label, Scalar
23+
from pandas._typing import AnyArrayLike, Label, Scalar, Shape
2424
from pandas.compat.numpy import function as nv
2525
from pandas.errors import InvalidIndexError, PerformanceWarning, UnsortedIndexError
2626
from pandas.util._decorators import Appender, cache_readonly, doc
@@ -702,7 +702,7 @@ def array(self):
702702
)
703703

704704
@property
705-
def shape(self):
705+
def shape(self) -> Shape:
706706
"""
707707
Return a tuple of the shape of the underlying data.
708708
"""

pandas/core/internals/blocks.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pandas._libs.internals import BlockPlacement
1111
from pandas._libs.tslibs import conversion
1212
from pandas._libs.tslibs.timezones import tz_compare
13-
from pandas._typing import ArrayLike, Scalar
13+
from pandas._typing import ArrayLike, Scalar, Shape
1414
from pandas.util._validators import validate_bool_kwarg
1515

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

27642764

2765-
def safe_reshape(arr, new_shape):
2765+
def safe_reshape(arr, new_shape: Shape):
27662766
"""
27672767
If possible, reshape `arr` to have shape `new_shape`,
27682768
with a couple of exceptions (see gh-13012):

pandas/core/internals/concat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66

77
from pandas._libs import NaT, internals as libinternals
8-
from pandas._typing import DtypeObj
8+
from pandas._typing import DtypeObj, Shape
99
from pandas.util._decorators import cache_readonly
1010

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

176176

177177
class JoinUnit:
178-
def __init__(self, block, shape, indexers=None):
178+
def __init__(self, block, shape: Shape, indexers=None):
179179
# Passing shape explicitly is required for cases when block is None.
180180
if indexers is None:
181181
indexers = {}

pandas/core/internals/managers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import numpy as np
1818

1919
from pandas._libs import internals as libinternals, lib
20-
from pandas._typing import ArrayLike, DtypeObj, Label
20+
from pandas._typing import ArrayLike, DtypeObj, Label, Shape
2121
from pandas.util._validators import validate_bool_kwarg
2222

2323
from pandas.core.dtypes.cast import (
@@ -204,7 +204,7 @@ def __nonzero__(self) -> bool:
204204
__bool__ = __nonzero__
205205

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

210210
@property
@@ -1825,7 +1825,7 @@ def _asarray_compat(x):
18251825
else:
18261826
return np.asarray(x)
18271827

1828-
def _shape_compat(x):
1828+
def _shape_compat(x) -> Shape:
18291829
if isinstance(x, ABCSeries):
18301830
return (len(x),)
18311831
else:

pandas/core/ops/array_ops.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
from datetime import timedelta
66
from functools import partial
77
import operator
8-
from typing import Any, Tuple
8+
from typing import Any
99
import warnings
1010

1111
import numpy as np
1212

1313
from pandas._libs import Timedelta, Timestamp, lib, ops as libops
14-
from pandas._typing import ArrayLike
14+
from pandas._typing import ArrayLike, Shape
1515

1616
from pandas.core.dtypes.cast import (
1717
construct_1d_object_array_from_listlike,
@@ -427,7 +427,7 @@ def maybe_upcast_datetimelike_array(obj: ArrayLike) -> ArrayLike:
427427
return obj
428428

429429

430-
def _maybe_upcast_for_op(obj, shape: Tuple[int, ...]):
430+
def _maybe_upcast_for_op(obj, shape: Shape):
431431
"""
432432
Cast non-pandas objects to pandas types to unify behavior of arithmetic
433433
and comparison operations.

pandas/io/pytables.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from pandas._libs import lib, writers as libwriters
3030
from pandas._libs.tslibs import timezones
31-
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label
31+
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label, Shape
3232
from pandas.compat._optional import import_optional_dependency
3333
from pandas.compat.pickle_compat import patch_pickle
3434
from pandas.errors import PerformanceWarning
@@ -3091,7 +3091,7 @@ class BlockManagerFixed(GenericFixed):
30913091
nblocks: int
30923092

30933093
@property
3094-
def shape(self):
3094+
def shape(self) -> Optional[Shape]:
30953095
try:
30963096
ndim = self.ndim
30973097

0 commit comments

Comments
 (0)