diff --git a/pandas/_libs/internals.pyi b/pandas/_libs/internals.pyi index d6fac14d3ee6e..3feefe7ac8ff4 100644 --- a/pandas/_libs/internals.pyi +++ b/pandas/_libs/internals.pyi @@ -1,6 +1,7 @@ from typing import ( Iterator, Sequence, + final, overload, ) @@ -50,10 +51,12 @@ class SharedBlock: class NumpyBlock(SharedBlock): values: np.ndarray + @final def getitem_block_index(self: T, slicer: slice) -> T: ... class NDArrayBackedBlock(SharedBlock): values: NDArrayBackedExtensionArray + @final def getitem_block_index(self: T, slicer: slice) -> T: ... class Block(SharedBlock): ... diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index fe76075c00280..ba59c50142550 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -517,7 +517,6 @@ cdef class NumpyBlock(SharedBlock): # set placement and ndim self.values = values - # @final # not useful in cython, but we _would_ annotate with @final cpdef NumpyBlock getitem_block_index(self, slice slicer): """ Perform __getitem__-like specialized to slicing along index. @@ -540,7 +539,6 @@ cdef class NDArrayBackedBlock(SharedBlock): # set placement and ndim self.values = values - # @final # not useful in cython, but we _would_ annotate with @final cpdef NDArrayBackedBlock getitem_block_index(self, slice slicer): """ Perform __getitem__-like specialized to slicing along index. diff --git a/pandas/_typing.py b/pandas/_typing.py index 588aa00116231..6583a9f60ee15 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -37,10 +37,7 @@ # and use a string literal forward reference to it in subsequent types # https://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles if TYPE_CHECKING: - from typing import ( - TypedDict, - final, - ) + from typing import TypedDict import numpy.typing as npt @@ -76,8 +73,6 @@ from pandas.tseries.offsets import DateOffset else: npt: Any = None - # typing.final does not exist until py38 - final = lambda x: x # typing.TypedDict does not exist until py38 TypedDict = dict diff --git a/pandas/core/base.py b/pandas/core/base.py index 430f1b57f0b87..285b7cd4f3a94 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -13,6 +13,7 @@ Literal, TypeVar, cast, + final, ) import numpy as np @@ -24,7 +25,6 @@ FrameOrSeries, IndexLabel, Shape, - final, npt, ) from pandas.compat import PYPY diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 877ccc56fe384..c63aeb736d16a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -18,6 +18,7 @@ Mapping, Sequence, cast, + final, overload, ) import warnings @@ -54,7 +55,6 @@ TimedeltaConvertibleTypes, TimestampConvertibleTypes, ValueKeyFunc, - final, npt, ) from pandas.compat._optional import import_optional_dependency diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index c5609672a9e5a..d3a86fa5950ed 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -29,6 +29,7 @@ class providing the base-class of operations. TypeVar, Union, cast, + final, ) import warnings @@ -49,7 +50,6 @@ class providing the base-class of operations. RandomState, Scalar, T, - final, ) from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index d12366caa44e3..3307558deec33 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -4,7 +4,10 @@ """ from __future__ import annotations -from typing import Hashable +from typing import ( + Hashable, + final, +) import warnings import numpy as np @@ -12,7 +15,6 @@ from pandas._typing import ( ArrayLike, FrameOrSeries, - final, ) from pandas.errors import InvalidIndexError from pandas.util._decorators import cache_readonly diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index ed142ae7a890c..36fbda5974ea0 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -14,6 +14,7 @@ Hashable, Iterator, Sequence, + final, overload, ) @@ -31,7 +32,6 @@ F, FrameOrSeries, Shape, - final, npt, ) from pandas.errors import AbstractMethodError diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d369624d30cdf..044cdff2e0ba7 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -13,6 +13,7 @@ Sequence, TypeVar, cast, + final, overload, ) import warnings @@ -44,7 +45,6 @@ F, Shape, T, - final, npt, ) from pandas.compat.numpy import function as nv diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index fef1d965eb534..07c6a84f75302 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -11,6 +11,7 @@ Sequence, TypeVar, cast, + final, ) import warnings @@ -28,7 +29,6 @@ Tick, parsing, ) -from pandas._typing import final from pandas.compat.numpy import function as nv from pandas.util._decorators import ( Appender, diff --git a/pandas/core/internals/base.py b/pandas/core/internals/base.py index 2bb14efad1ce7..0ee22200ed495 100644 --- a/pandas/core/internals/base.py +++ b/pandas/core/internals/base.py @@ -4,12 +4,14 @@ """ from __future__ import annotations -from typing import TypeVar +from typing import ( + TypeVar, + final, +) from pandas._typing import ( DtypeObj, Shape, - final, ) from pandas.errors import AbstractMethodError diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 139eef5b05d11..294f7c2d13593 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -9,6 +9,7 @@ Iterable, Sequence, cast, + final, ) import warnings @@ -28,7 +29,6 @@ DtypeObj, F, Shape, - final, ) from pandas.util._decorators import cache_readonly from pandas.util._validators import validate_bool_kwarg @@ -312,17 +312,6 @@ def getitem_block(self, slicer) -> Block: return type(self)(new_values, new_mgr_locs, self.ndim) - def getitem_block_index(self, slicer: slice) -> Block: - """ - Perform __getitem__-like specialized to slicing along index. - - Assumes self.ndim == 2 - """ - # error: Invalid index type "Tuple[ellipsis, slice]" for - # "Union[ndarray, ExtensionArray]"; expected type "Union[int, slice, ndarray]" - new_values = self.values[..., slicer] # type: ignore[index] - return type(self)(new_values, self._mgr_locs, ndim=self.ndim) - @final def getitem_block_columns(self, slicer, new_mgr_locs: BlockPlacement) -> Block: """ @@ -1557,6 +1546,18 @@ def _slice(self, slicer): return self.values[slicer] + @final + def getitem_block_index(self, slicer: slice) -> ExtensionBlock: + """ + Perform __getitem__-like specialized to slicing along index. + + Assumes self.ndim == 2 + """ + # error: Invalid index type "Tuple[ellipsis, slice]" for + # "Union[ndarray, ExtensionArray]"; expected type "Union[int, slice, ndarray]" + new_values = self.values[..., slicer] # type: ignore[index] + return type(self)(new_values, self._mgr_locs, ndim=self.ndim) + def fillna( self, value, limit=None, inplace: bool = False, downcast=None ) -> list[Block]: @@ -1661,8 +1662,6 @@ def _unstack(self, unstacker, fill_value, new_placement): class NumpyBlock(libinternals.NumpyBlock, Block): values: np.ndarray - getitem_block_index = libinternals.NumpyBlock.getitem_block_index - class NumericBlock(NumpyBlock): __slots__ = () @@ -1675,7 +1674,6 @@ class NDArrayBackedExtensionBlock(libinternals.NDArrayBackedBlock, EABackedBlock """ values: NDArrayBackedExtensionArray - getitem_block_index = libinternals.NDArrayBackedBlock.getitem_block_index @property def is_view(self) -> bool: diff --git a/pandas/core/resample.py b/pandas/core/resample.py index cf71bcfffc264..b9a75a6917140 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -7,6 +7,7 @@ Callable, Hashable, Literal, + final, no_type_check, ) @@ -27,7 +28,6 @@ T, TimedeltaConvertibleTypes, TimestampConvertibleTypes, - final, npt, ) from pandas.compat.numpy import function as nv diff --git a/pandas/io/parsers/base_parser.py b/pandas/io/parsers/base_parser.py index 980c75b370bd5..5714bbab016c8 100644 --- a/pandas/io/parsers/base_parser.py +++ b/pandas/io/parsers/base_parser.py @@ -12,6 +12,7 @@ Iterable, Sequence, cast, + final, ) import warnings @@ -26,7 +27,6 @@ ArrayLike, DtypeArg, FilePathOrBuffer, - final, ) from pandas.errors import ( ParserError,