Skip to content

TYP: untyped decorators #36996

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def ndim(self) -> int:
def size(self) -> int:
return np.prod(self.shape)

@cache_readonly
@property
def nbytes(self) -> int:
return self._ndarray.nbytes

Expand Down
8 changes: 4 additions & 4 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import Appender, Substitution
from pandas.util._decorators import Appender, Substitution, cache_readonly
from pandas.util._validators import validate_fillna_kwargs

from pandas.core.dtypes.cast import maybe_cast_to_extension_array
Expand Down Expand Up @@ -395,7 +395,7 @@ def to_numpy(
# Required attributes
# ------------------------------------------------------------------------

@property
@cache_readonly
def dtype(self) -> ExtensionDtype:
"""
An instance of 'ExtensionDtype'.
Expand All @@ -409,14 +409,14 @@ def shape(self) -> Tuple[int, ...]:
"""
return (len(self),)

@property
@cache_readonly
def size(self) -> int:
"""
The number of elements in the array.
"""
return np.prod(self.shape)

@property
@cache_readonly
def ndim(self) -> int:
"""
Extension Arrays are only allowed to be 1-dimensional.
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pandas._typing import ArrayLike
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.common import (
is_bool_dtype,
Expand Down Expand Up @@ -68,7 +69,7 @@ def type(self) -> Type: # type: ignore[override]
def kind(self) -> str:
return "b"

@property
@cache_readonly
def numpy_dtype(self) -> np.dtype:
return np.dtype("bool")

Expand Down Expand Up @@ -269,7 +270,7 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
self._dtype = BooleanDtype()
super().__init__(values, mask, copy=copy)

@property
@cache_readonly
def dtype(self) -> BooleanDtype:
return self._dtype

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def __init__(
self._dtype = self._dtype.update_dtype(dtype)
self._codes = coerce_indexer_dtype(codes, dtype.categories)

@property
@cache_readonly
def dtype(self) -> CategoricalDtype:
"""
The :class:`~pandas.api.types.CategoricalDtype` for this instance.
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
tzconversion,
)
from pandas.errors import PerformanceWarning
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.common import (
DT64NS_DTYPE,
Expand Down Expand Up @@ -484,7 +485,7 @@ def _maybe_clear_freq(self):
def _box_func(self, x) -> Union[Timestamp, NaTType]:
return Timestamp(x, freq=self.freq, tz=self.tz)

@property
@cache_readonly
def dtype(self) -> Union[np.dtype, DatetimeTZDtype]:
"""
The dtype for the DatetimeArray.
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
intervals_to_interval_bounds,
)
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender
from pandas.util._decorators import Appender, cache_readonly

from pandas.core.dtypes.cast import maybe_convert_platform
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -144,7 +144,6 @@
)
)
class IntervalArray(IntervalMixin, ExtensionArray):
ndim = 1
can_hold_na = True
_na_value = _fill_value = np.nan

Expand Down Expand Up @@ -522,15 +521,15 @@ def _shallow_copy(self, left, right):
# ---------------------------------------------------------------------
# Descriptive

@property
@cache_readonly
def dtype(self):
return IntervalDtype(self.left.dtype)

@property
def nbytes(self) -> int:
return self.left.nbytes + self.right.nbytes

@property
@cache_readonly
def size(self) -> int:
# Avoid materializing self.values
return self.left.size
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def numpy_dtype(self) -> np.dtype:
""" Return an instance of our numpy dtype """
return np.dtype(self.type)

@cache_readonly
@property
def kind(self) -> str:
return self.numpy_dtype.kind

Expand Down Expand Up @@ -95,7 +95,7 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
self._data = values
self._mask = mask

@property
@cache_readonly
def dtype(self) -> BaseMaskedDtype:
raise AbstractMethodError(self)

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pandas._libs import lib
from pandas._typing import Scalar
from pandas.compat.numpy import function as nv
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.missing import isna
Expand Down Expand Up @@ -194,7 +195,7 @@ def _from_backing_data(self, arr: np.ndarray) -> "PandasArray":
# ------------------------------------------------------------------------
# Data

@property
@cache_readonly
def dtype(self) -> PandasDtype:
return self._dtype

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import pandas.compat as compat
from pandas.compat.numpy import function as nv
from pandas.errors import PerformanceWarning
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.cast import (
astype_nansafe,
Expand Down Expand Up @@ -515,7 +516,7 @@ def sp_values(self) -> np.ndarray:
"""
return self._sparse_values

@property
@cache_readonly
def dtype(self):
return self._dtype

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from pandas._libs.tslibs.fields import get_timedelta_field
from pandas._libs.tslibs.timedeltas import array_to_timedelta64, parse_timedelta_unit
from pandas.compat.numpy import function as nv
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.common import (
DT64NS_DTYPE,
Expand Down Expand Up @@ -122,7 +123,7 @@ class TimedeltaArray(dtl.DatetimeLikeArrayMixin, dtl.TimelikeOps):
def _box_func(self, x) -> Union[Timedelta, NaTType]:
return Timedelta(x, unit="ns")

@property
@cache_readonly
def dtype(self) -> np.dtype:
"""
The dtype for the TimedeltaArray.
Expand Down
49 changes: 1 addition & 48 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def _selected_obj(self):
else:
return self.obj[self._selection]

@cache_readonly
@property
def ndim(self) -> int:
return self._selected_obj.ndim

Expand Down Expand Up @@ -1315,53 +1315,6 @@ def nunique(self, dropna: bool = True) -> int:
n -= 1
return n

@property
def is_unique(self) -> bool:
"""
Return boolean if values in the object are unique.

Returns
-------
bool
"""
return self.nunique(dropna=False) == len(self)

@property
def is_monotonic(self) -> bool:
"""
Return boolean if values in the object are
monotonic_increasing.

Returns
-------
bool
"""
from pandas import Index

return Index(self).is_monotonic

@property
def is_monotonic_increasing(self) -> bool:
"""
Alias for is_monotonic.
"""
# mypy complains if we alias directly
return self.is_monotonic

@property
def is_monotonic_decreasing(self) -> bool:
"""
Return boolean if values in the object are
monotonic_decreasing.

Returns
-------
bool
"""
from pandas import Index

return Index(self).is_monotonic_decreasing

def memory_usage(self, deep=False):
"""
Memory usage of the values.
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 @@ -1643,7 +1643,7 @@ def is_monotonic(self) -> bool:
"""
return self.is_monotonic_increasing

@property
@cache_readonly
def is_monotonic_increasing(self) -> bool:
"""
Return if the index is monotonic increasing (only equal or
Expand All @@ -1660,7 +1660,7 @@ def is_monotonic_increasing(self) -> bool:
"""
return self._engine.is_monotonic_increasing

@property
@cache_readonly
def is_monotonic_decreasing(self) -> bool:
"""
Return if the index is monotonic decreasing (only equal or
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def _format_with_header(self, header: List[str], na_rep: str = "NaN") -> List[st

# --------------------------------------------------------------------

@property
@cache_readonly
def inferred_type(self) -> str:
return "categorical"

Expand Down
10 changes: 5 additions & 5 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ class DatetimeIndexOpsMixin(ExtensionIndex):
_bool_ops: List[str] = []
_field_ops: List[str] = []

# error: "Callable[[Any], Any]" has no attribute "fget"
hasnans = cache_readonly(
DatetimeLikeArrayMixin._hasnans.fget # type: ignore[attr-defined]
)
@cache_readonly
def hasnans(self) -> bool:
return self._data._hasnans

_hasnans = hasnans # for index / array -agnostic code

@property
@cache_readonly
def _is_all_dates(self) -> bool:
return True

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
def is_type_compatible(self, typ) -> bool:
return typ == self.inferred_type or typ == "datetime"

@property
@cache_readonly
def inferred_type(self) -> str:
# b/c datetime is represented as microseconds since the epoch, make
# sure we can't have ambiguous indexing
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def __contains__(self, key: Any) -> bool:
def _multiindex(self) -> MultiIndex:
return MultiIndex.from_arrays([self.left, self.right], names=["left", "right"])

@cache_readonly
@property
def values(self) -> IntervalArray:
"""
Return the IntervalIndex's data as an IntervalArray.
Expand Down Expand Up @@ -414,7 +414,7 @@ def astype(self, dtype, copy: bool = True):
return self._shallow_copy(new_values)
return Index.astype(self, dtype, copy=copy)

@property
@cache_readonly
def inferred_type(self) -> str:
"""Return a string of the type inferred from the values"""
return "interval"
Expand Down Expand Up @@ -1096,7 +1096,7 @@ def func(self, other, sort=sort):

# --------------------------------------------------------------------

@property
@cache_readonly
def _is_all_dates(self) -> bool:
"""
This is False even when left/right contain datetime-like objects,
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 @@ -1228,7 +1228,7 @@ def memory_usage(self, deep: bool = False) -> int:
# a tuple representation unnecessarily
return self._nbytes(deep)

@cache_readonly
@property
def nbytes(self) -> int:
""" return the number of bytes in the underlying data """
return self._nbytes(False)
Expand Down Expand Up @@ -1748,7 +1748,7 @@ def to_flat_index(self):
"""
return Index(self._values, tupleize_cols=False)

@property
@cache_readonly
def _is_all_dates(self) -> bool:
return False

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def _assert_safe_casting(cls, data, subarr):
"""
pass

@property
@cache_readonly
def _is_all_dates(self) -> bool:
"""
Checks that all the labels are datetime objects.
Expand Down Expand Up @@ -240,7 +240,7 @@ def __contains__(self, key) -> bool:
except (OverflowError, TypeError, ValueError):
return False

@property
@cache_readonly
def inferred_type(self) -> str:
"""
Always 'integer' for ``Int64Index`` and ``UInt64Index``
Expand Down Expand Up @@ -335,7 +335,7 @@ class Float64Index(NumericIndex):
_engine_type = libindex.Float64Engine
_default_dtype = np.float64

@property
@cache_readonly
def inferred_type(self) -> str:
"""
Always 'floating' for ``Float64Index``
Expand Down
Loading