Skip to content

Commit 64ad019

Browse files
authored
TYP: ndim is consistently a property (#47378)
* TYP: ndim is consistently a property * unused import * fix test * nicer fix
1 parent 5e3d0ed commit 64ad019

File tree

10 files changed

+39
-12
lines changed

10 files changed

+39
-12
lines changed

pandas/core/arrays/datetimes.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,15 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps):
193193
"""
194194

195195
_typ = "datetimearray"
196-
_scalar_type = Timestamp
197196
_internal_fill_value = np.datetime64("NaT", "ns")
198197
_recognized_scalars = (datetime, np.datetime64)
199198
_is_recognized_dtype = is_datetime64_any_dtype
200199
_infer_matches = ("datetime", "datetime64", "date")
201200

201+
@property
202+
def _scalar_type(self) -> type[Timestamp]:
203+
return Timestamp
204+
202205
# define my properties & methods for delegation
203206
_bool_ops: list[str] = [
204207
"is_month_start",

pandas/core/arrays/interval.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import textwrap
99
from typing import (
1010
TYPE_CHECKING,
11+
Literal,
1112
Sequence,
1213
TypeVar,
1314
Union,
@@ -204,10 +205,13 @@
204205
}
205206
)
206207
class IntervalArray(IntervalMixin, ExtensionArray):
207-
ndim = 1
208208
can_hold_na = True
209209
_na_value = _fill_value = np.nan
210210

211+
@property
212+
def ndim(self) -> Literal[1]:
213+
return 1
214+
211215
# To make mypy recognize the fields
212216
_left: np.ndarray
213217
_right: np.ndarray

pandas/core/arrays/period.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,15 @@ class PeriodArray(dtl.DatelikeOps, libperiod.PeriodMixin):
167167
# array priority higher than numpy scalars
168168
__array_priority__ = 1000
169169
_typ = "periodarray" # ABCPeriodArray
170-
_scalar_type = Period
171170
_internal_fill_value = np.int64(iNaT)
172171
_recognized_scalars = (Period,)
173172
_is_recognized_dtype = is_period_dtype
174173
_infer_matches = ("period",)
175174

175+
@property
176+
def _scalar_type(self) -> type[Period]:
177+
return Period
178+
176179
# Names others delegate to us
177180
_other_ops: list[str] = []
178181
_bool_ops: list[str] = ["is_leap_year"]

pandas/core/arrays/timedeltas.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,15 @@ class TimedeltaArray(dtl.TimelikeOps):
119119
"""
120120

121121
_typ = "timedeltaarray"
122-
_scalar_type = Timedelta
123122
_internal_fill_value = np.timedelta64("NaT", "ns")
124123
_recognized_scalars = (timedelta, np.timedelta64, Tick)
125124
_is_recognized_dtype = is_timedelta64_dtype
126125
_infer_matches = ("timedelta", "timedelta64")
127126

127+
@property
128+
def _scalar_type(self) -> type[Timedelta]:
129+
return Timedelta
130+
128131
__array_priority__ = 1000
129132
# define my properties & methods for delegation
130133
_other_ops: list[str] = []

pandas/core/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def __len__(self) -> int:
318318
raise AbstractMethodError(self)
319319

320320
@property
321-
def ndim(self) -> int:
321+
def ndim(self) -> Literal[1]:
322322
"""
323323
Number of dimensions of the underlying data, by definition 1.
324324
"""

pandas/core/internals/array_manager.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Any,
99
Callable,
1010
Hashable,
11+
Literal,
1112
TypeVar,
1213
)
1314

@@ -704,7 +705,9 @@ def _equal_values(self, other) -> bool:
704705

705706

706707
class ArrayManager(BaseArrayManager):
707-
ndim = 2
708+
@property
709+
def ndim(self) -> Literal[2]:
710+
return 2
708711

709712
def __init__(
710713
self,
@@ -1191,7 +1194,9 @@ class SingleArrayManager(BaseArrayManager, SingleDataManager):
11911194
arrays: list[np.ndarray | ExtensionArray]
11921195
_axes: list[Index]
11931196

1194-
ndim = 1
1197+
@property
1198+
def ndim(self) -> Literal[1]:
1199+
return 1
11951200

11961201
def __init__(
11971202
self,

pandas/core/internals/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
from typing import (
8+
Literal,
89
TypeVar,
910
final,
1011
)
@@ -155,7 +156,9 @@ def _consolidate_inplace(self) -> None:
155156

156157

157158
class SingleDataManager(DataManager):
158-
ndim = 1
159+
@property
160+
def ndim(self) -> Literal[1]:
161+
return 1
159162

160163
@final
161164
@property

pandas/core/internals/managers.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Any,
66
Callable,
77
Hashable,
8+
Literal,
89
Sequence,
910
TypeVar,
1011
cast,
@@ -142,7 +143,10 @@ class BaseBlockManager(DataManager):
142143
blocks: tuple[Block, ...]
143144
axes: list[Index]
144145

145-
ndim: int
146+
@property
147+
def ndim(self) -> int:
148+
raise NotImplementedError
149+
146150
_known_consolidated: bool
147151
_is_consolidated: bool
148152

@@ -1678,7 +1682,10 @@ def _consolidate_inplace(self) -> None:
16781682
class SingleBlockManager(BaseBlockManager, SingleDataManager):
16791683
"""manage a single block with"""
16801684

1681-
ndim = 1
1685+
@property
1686+
def ndim(self) -> Literal[1]:
1687+
return 1
1688+
16821689
_is_consolidated = True
16831690
_known_consolidated = True
16841691
__slots__ = ()

pandas/tests/arrays/test_datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def test_shift_fill_int_deprecated(self):
564564

565565
expected = arr.copy()
566566
if self.array_cls is PeriodArray:
567-
fill_val = PeriodArray._scalar_type._from_ordinal(1, freq=arr.freq)
567+
fill_val = arr._scalar_type._from_ordinal(1, freq=arr.freq)
568568
else:
569569
fill_val = arr._scalar_type(1)
570570
expected[0] = fill_val

pyright_reportGeneralTypeIssues.json

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
"pandas/core/indexing.py",
6262
"pandas/core/internals/api.py",
6363
"pandas/core/internals/array_manager.py",
64-
"pandas/core/internals/base.py",
6564
"pandas/core/internals/blocks.py",
6665
"pandas/core/internals/concat.py",
6766
"pandas/core/internals/construction.py",

0 commit comments

Comments
 (0)