Skip to content

Commit fcd2a5d

Browse files
API: make construct_array_type non-classmethod (#62060)
Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 05d5765 commit fcd2a5d

File tree

17 files changed

+25
-50
lines changed

17 files changed

+25
-50
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ Other API changes
418418
an empty ``RangeIndex`` or empty ``Index`` with object dtype when determining
419419
the dtype of the resulting Index (:issue:`60797`)
420420
- :class:`IncompatibleFrequency` now subclasses ``TypeError`` instead of ``ValueError``. As a result, joins with mismatched frequencies now cast to object like other non-comparable joins, and arithmetic with indexes with mismatched frequencies align (:issue:`55782`)
421+
- :meth:`ExtensionDtype.construct_array_type` is now a regular method instead of a ``classmethod`` (:issue:`58663`)
421422
- Comparison operations between :class:`Index` and :class:`Series` now consistently return :class:`Series` regardless of which object is on the left or right (:issue:`36759`)
422423
- Numpy functions like ``np.isinf`` that return a bool dtype when called on a :class:`Index` object now return a bool-dtype :class:`Index` instead of ``np.ndarray`` (:issue:`52676`)
423424

pandas/core/arrays/boolean.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ def kind(self) -> str:
8585
def numpy_dtype(self) -> np.dtype:
8686
return np.dtype("bool")
8787

88-
@classmethod
89-
def construct_array_type(cls) -> type_t[BooleanArray]:
88+
def construct_array_type(self) -> type_t[BooleanArray]:
9089
"""
9190
Return the array type associated with this dtype.
9291

pandas/core/arrays/floating.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class FloatingDtype(NumericDtype):
2828
_default_np_dtype = np.dtype(np.float64)
2929
_checker = is_float_dtype
3030

31-
@classmethod
32-
def construct_array_type(cls) -> type[FloatingArray]:
31+
def construct_array_type(self) -> type[FloatingArray]:
3332
"""
3433
Return the array type associated with this dtype.
3534

pandas/core/arrays/integer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class IntegerDtype(NumericDtype):
2828
_default_np_dtype = np.dtype(np.int64)
2929
_checker = is_integer_dtype
3030

31-
@classmethod
32-
def construct_array_type(cls) -> type[IntegerArray]:
31+
def construct_array_type(self) -> type[IntegerArray]:
3332
"""
3433
Return the array type associated with this dtype.
3534

pandas/core/arrays/numeric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _coerce_to_data_and_mask(
150150
if dtype is not None:
151151
dtype = dtype_cls._standardize_dtype(dtype)
152152

153-
cls = dtype_cls.construct_array_type()
153+
cls = dtype_cls().construct_array_type()
154154
if isinstance(values, cls):
155155
values, mask = values._data, values._mask
156156
if dtype is not None:

pandas/core/arrays/string_.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,7 @@ def construct_from_string(cls, string) -> Self:
283283
else:
284284
raise TypeError(f"Cannot construct a '{cls.__name__}' from '{string}'")
285285

286-
# https://github.com/pandas-dev/pandas/issues/36126
287-
# error: Signature of "construct_array_type" incompatible with supertype
288-
# "ExtensionDtype"
289-
def construct_array_type( # type: ignore[override]
290-
self,
291-
) -> type_t[BaseStringArray]:
286+
def construct_array_type(self) -> type_t[BaseStringArray]:
292287
"""
293288
Return the array type associated with this dtype.
294289

pandas/core/dtypes/base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,15 @@ def names(self) -> list[str] | None:
211211
"""
212212
return None
213213

214-
@classmethod
215-
def construct_array_type(cls) -> type_t[ExtensionArray]:
214+
def construct_array_type(self) -> type_t[ExtensionArray]:
216215
"""
217216
Return the array type associated with this dtype.
218217
219218
Returns
220219
-------
221220
type
222221
"""
223-
raise AbstractMethodError(cls)
222+
raise AbstractMethodError(self)
224223

225224
def empty(self, shape: Shape) -> ExtensionArray:
226225
"""

pandas/core/dtypes/dtypes.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ def _hash_categories(self) -> int:
527527
combined_hashed = combine_hash_arrays(iter(cat_array), num_items=len(cat_array))
528528
return np.bitwise_xor.reduce(combined_hashed)
529529

530-
@classmethod
531-
def construct_array_type(cls) -> type_t[Categorical]:
530+
def construct_array_type(self) -> type_t[Categorical]:
532531
"""
533532
Return the array type associated with this dtype.
534533
@@ -856,8 +855,7 @@ def tz(self) -> tzinfo:
856855
"""
857856
return self._tz
858857

859-
@classmethod
860-
def construct_array_type(cls) -> type_t[DatetimeArray]:
858+
def construct_array_type(self) -> type_t[DatetimeArray]:
861859
"""
862860
Return the array type associated with this dtype.
863861
@@ -1174,8 +1172,7 @@ def is_dtype(cls, dtype: object) -> bool:
11741172
return False
11751173
return super().is_dtype(dtype)
11761174

1177-
@classmethod
1178-
def construct_array_type(cls) -> type_t[PeriodArray]:
1175+
def construct_array_type(self) -> type_t[PeriodArray]:
11791176
"""
11801177
Return the array type associated with this dtype.
11811178
@@ -1363,8 +1360,7 @@ def subtype(self):
13631360
"""
13641361
return self._subtype
13651362

1366-
@classmethod
1367-
def construct_array_type(cls) -> type[IntervalArray]:
1363+
def construct_array_type(self) -> type[IntervalArray]:
13681364
"""
13691365
Return the array type associated with this dtype.
13701366
@@ -1576,8 +1572,7 @@ def construct_from_string(cls, string: str) -> NumpyEADtype:
15761572
raise TypeError(msg) from err
15771573
return cls(dtype)
15781574

1579-
@classmethod
1580-
def construct_array_type(cls) -> type_t[NumpyExtensionArray]:
1575+
def construct_array_type(self) -> type_t[NumpyExtensionArray]:
15811576
"""
15821577
Return the array type associated with this dtype.
15831578
@@ -1649,8 +1644,7 @@ def itemsize(self) -> int:
16491644
"""Return the number of bytes in this dtype"""
16501645
return self.numpy_dtype.itemsize
16511646

1652-
@classmethod
1653-
def construct_array_type(cls) -> type_t[BaseMaskedArray]:
1647+
def construct_array_type(self) -> type_t[BaseMaskedArray]:
16541648
"""
16551649
Return the array type associated with this dtype.
16561650
@@ -1914,8 +1908,7 @@ def name(self) -> str:
19141908
def __repr__(self) -> str:
19151909
return self.name
19161910

1917-
@classmethod
1918-
def construct_array_type(cls) -> type_t[SparseArray]:
1911+
def construct_array_type(self) -> type_t[SparseArray]:
19191912
"""
19201913
Return the array type associated with this dtype.
19211914
@@ -2316,8 +2309,7 @@ def itemsize(self) -> int:
23162309
"""Return the number of bytes in this dtype"""
23172310
return self.numpy_dtype.itemsize
23182311

2319-
@classmethod
2320-
def construct_array_type(cls) -> type_t[ArrowExtensionArray]:
2312+
def construct_array_type(self) -> type_t[ArrowExtensionArray]:
23212313
"""
23222314
Return the array type associated with this dtype.
23232315

pandas/tests/arrays/test_array.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,7 @@ def test_bounds_check():
487487
class DecimalDtype2(DecimalDtype):
488488
name = "decimal2"
489489

490-
@classmethod
491-
def construct_array_type(cls):
490+
def construct_array_type(self):
492491
"""
493492
Return the array type associated with this dtype.
494493

pandas/tests/dtypes/test_common.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,7 @@ def type(self):
576576
def name(self):
577577
raise NotImplementedError
578578

579-
@classmethod
580-
def construct_array_type(cls):
579+
def construct_array_type(self):
581580
raise NotImplementedError
582581

583582
def _is_numeric(self) -> bool:

0 commit comments

Comments
 (0)