From b8c3a5f6ec0ca342ee420cc5659a057ee107fdaf Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 7 Jul 2021 11:54:33 +0100 Subject: [PATCH 1/2] TYP: clean for PY3.8 (final) --- pandas/_libs/internals.pyi | 2 ++ pandas/_libs/internals.pyx | 2 -- pandas/_typing.py | 7 +------ pandas/core/base.py | 2 +- pandas/core/generic.py | 2 +- pandas/core/groupby/groupby.py | 2 +- pandas/core/groupby/grouper.py | 6 ++++-- pandas/core/groupby/ops.py | 2 +- pandas/core/indexes/base.py | 2 +- pandas/core/indexes/datetimelike.py | 6 ++---- pandas/core/internals/base.py | 6 ++++-- pandas/core/internals/blocks.py | 6 +++--- pandas/core/resample.py | 2 +- pandas/io/parsers/base_parser.py | 2 +- 14 files changed, 23 insertions(+), 26 deletions(-) diff --git a/pandas/_libs/internals.pyi b/pandas/_libs/internals.pyi index d6fac14d3ee6e..4049a791be194 100644 --- a/pandas/_libs/internals.pyi +++ b/pandas/_libs/internals.pyi @@ -50,10 +50,12 @@ class SharedBlock: class NumpyBlock(SharedBlock): values: np.ndarray + # @final # not useful in cython, but we _would_ annotate with @final def getitem_block_index(self: T, slicer: slice) -> T: ... class NDArrayBackedBlock(SharedBlock): values: NDArrayBackedExtensionArray + # @final # not useful in cython, but we _would_ annotate with @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 cfd4695a5335b..88e41fcba9611 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 c15aa3cf697da..3e11181ed7701 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 5f462de19a398..152e3dc3c0396 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 874d7395b1950..d7f5063ca45be 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, ) from pandas.errors import AbstractMethodError from pandas.util._decorators import cache_readonly diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2f1d85f1340a4..19699292e9c51 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, ) from pandas.compat.numpy import function as nv from pandas.errors import ( diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index e86fbab52fcf8..fa198a7581b57 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -10,6 +10,7 @@ Sequence, TypeVar, cast, + final, ) import warnings @@ -27,10 +28,7 @@ Tick, parsing, ) -from pandas._typing import ( - Callable, - final, -) +from pandas._typing import Callable 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 6275fe39558a3..dc8574c196299 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 @@ -1661,7 +1661,7 @@ def _unstack(self, unstacker, fill_value, new_placement): class NumpyBlock(libinternals.NumpyBlock, Block): values: np.ndarray - getitem_block_index = libinternals.NumpyBlock.getitem_block_index + getitem_block_index = final(libinternals.NumpyBlock.getitem_block_index) class NumericBlock(NumpyBlock): @@ -1675,7 +1675,7 @@ class NDArrayBackedExtensionBlock(libinternals.NDArrayBackedBlock, EABackedBlock """ values: NDArrayBackedExtensionArray - getitem_block_index = libinternals.NDArrayBackedBlock.getitem_block_index + getitem_block_index = final(libinternals.NDArrayBackedBlock.getitem_block_index) @property def is_view(self) -> bool: diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 40a208a24699a..69791777d704e 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, ) from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError diff --git a/pandas/io/parsers/base_parser.py b/pandas/io/parsers/base_parser.py index f914e0601fb89..d882d5f249856 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, From 52c91da2ef7ac73bd1fd9fa44c65b46c6cfcfcb1 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 11 Jul 2021 13:19:45 +0100 Subject: [PATCH 2/2] fixup getitem_block_index --- pandas/_libs/internals.pyi | 5 +++-- pandas/core/internals/blocks.py | 26 ++++++++++++-------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/pandas/_libs/internals.pyi b/pandas/_libs/internals.pyi index 4049a791be194..3feefe7ac8ff4 100644 --- a/pandas/_libs/internals.pyi +++ b/pandas/_libs/internals.pyi @@ -1,6 +1,7 @@ from typing import ( Iterator, Sequence, + final, overload, ) @@ -50,12 +51,12 @@ class SharedBlock: class NumpyBlock(SharedBlock): values: np.ndarray - # @final # not useful in cython, but we _would_ annotate with @final + @final def getitem_block_index(self: T, slicer: slice) -> T: ... class NDArrayBackedBlock(SharedBlock): values: NDArrayBackedExtensionArray - # @final # not useful in cython, but we _would_ annotate with @final + @final def getitem_block_index(self: T, slicer: slice) -> T: ... class Block(SharedBlock): ... diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index dc8574c196299..44ad918c298b6 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -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 = final(libinternals.NumpyBlock.getitem_block_index) - class NumericBlock(NumpyBlock): __slots__ = () @@ -1675,7 +1674,6 @@ class NDArrayBackedExtensionBlock(libinternals.NDArrayBackedBlock, EABackedBlock """ values: NDArrayBackedExtensionArray - getitem_block_index = final(libinternals.NDArrayBackedBlock.getitem_block_index) @property def is_view(self) -> bool: