Skip to content

TYP: ndim is consistently a property #47378

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

Merged
merged 4 commits into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,15 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps):
"""

_typ = "datetimearray"
_scalar_type = Timestamp
_internal_fill_value = np.datetime64("NaT", "ns")
_recognized_scalars = (datetime, np.datetime64)
_is_recognized_dtype = is_datetime64_any_dtype
_infer_matches = ("datetime", "datetime64", "date")

@property
def _scalar_type(self) -> type[Timestamp]:
return Timestamp

# define my properties & methods for delegation
_bool_ops: list[str] = [
"is_month_start",
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import textwrap
from typing import (
TYPE_CHECKING,
Literal,
Sequence,
TypeVar,
Union,
Expand Down Expand Up @@ -204,10 +205,13 @@
}
)
class IntervalArray(IntervalMixin, ExtensionArray):
ndim = 1
can_hold_na = True
_na_value = _fill_value = np.nan

@property
def ndim(self) -> Literal[1]:
return 1

# To make mypy recognize the fields
_left: np.ndarray
_right: np.ndarray
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,15 @@ class PeriodArray(dtl.DatelikeOps, libperiod.PeriodMixin):
# array priority higher than numpy scalars
__array_priority__ = 1000
_typ = "periodarray" # ABCPeriodArray
_scalar_type = Period
_internal_fill_value = np.int64(iNaT)
_recognized_scalars = (Period,)
_is_recognized_dtype = is_period_dtype
_infer_matches = ("period",)

@property
def _scalar_type(self) -> type[Period]:
return Period

# Names others delegate to us
_other_ops: list[str] = []
_bool_ops: list[str] = ["is_leap_year"]
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,15 @@ class TimedeltaArray(dtl.TimelikeOps):
"""

_typ = "timedeltaarray"
_scalar_type = Timedelta
_internal_fill_value = np.timedelta64("NaT", "ns")
_recognized_scalars = (timedelta, np.timedelta64, Tick)
_is_recognized_dtype = is_timedelta64_dtype
_infer_matches = ("timedelta", "timedelta64")

@property
def _scalar_type(self) -> type[Timedelta]:
return Timedelta

__array_priority__ = 1000
# define my properties & methods for delegation
_other_ops: list[str] = []
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def __len__(self) -> int:
raise AbstractMethodError(self)

@property
def ndim(self) -> int:
def ndim(self) -> Literal[1]:
"""
Number of dimensions of the underlying data, by definition 1.
"""
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Any,
Callable,
Hashable,
Literal,
TypeVar,
)

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


class ArrayManager(BaseArrayManager):
ndim = 2
@property
def ndim(self) -> Literal[2]:
return 2

def __init__(
self,
Expand Down Expand Up @@ -1191,7 +1194,9 @@ class SingleArrayManager(BaseArrayManager, SingleDataManager):
arrays: list[np.ndarray | ExtensionArray]
_axes: list[Index]

ndim = 1
@property
def ndim(self) -> Literal[1]:
return 1

def __init__(
self,
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/internals/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

from typing import (
Literal,
TypeVar,
final,
)
Expand Down Expand Up @@ -155,7 +156,9 @@ def _consolidate_inplace(self) -> None:


class SingleDataManager(DataManager):
ndim = 1
@property
def ndim(self) -> Literal[1]:
return 1

@final
@property
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Any,
Callable,
Hashable,
Literal,
Sequence,
TypeVar,
cast,
Expand Down Expand Up @@ -142,7 +143,10 @@ class BaseBlockManager(DataManager):
blocks: tuple[Block, ...]
axes: list[Index]

ndim: int
@property
def ndim(self) -> int:
raise NotImplementedError

_known_consolidated: bool
_is_consolidated: bool

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

ndim = 1
@property
def ndim(self) -> Literal[1]:
return 1

_is_consolidated = True
_known_consolidated = True
__slots__ = ()
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def test_shift_fill_int_deprecated(self):

expected = arr.copy()
if self.array_cls is PeriodArray:
fill_val = PeriodArray._scalar_type._from_ordinal(1, freq=arr.freq)
fill_val = arr._scalar_type._from_ordinal(1, freq=arr.freq)
Copy link
Member Author

Choose a reason for hiding this comment

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

This was causing the error: when accessing a property on the class itself, you get the property object, not its content.

else:
fill_val = arr._scalar_type(1)
expected[0] = fill_val
Expand Down
1 change: 0 additions & 1 deletion pyright_reportGeneralTypeIssues.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"pandas/core/indexing.py",
"pandas/core/internals/api.py",
"pandas/core/internals/array_manager.py",
"pandas/core/internals/base.py",
"pandas/core/internals/blocks.py",
"pandas/core/internals/concat.py",
"pandas/core/internals/construction.py",
Expand Down