From 1fdafa7e86d214422e76874296434e769f0dae68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 10 Jul 2022 21:32:25 -0400 Subject: [PATCH] TYP: make _engine_type consistently a property --- .pre-commit-config.yaml | 2 +- pandas/core/indexes/base.py | 9 ++++++--- pandas/core/indexes/category.py | 2 +- pandas/core/indexes/datetimes.py | 5 ++++- pandas/core/indexes/numeric.py | 12 +++++++++--- pandas/core/indexes/period.py | 5 ++++- pandas/core/indexes/range.py | 5 ++++- pandas/core/indexes/timedeltas.py | 5 ++++- pyright_reportGeneralTypeIssues.json | 3 --- 9 files changed, 33 insertions(+), 15 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c8123f90ab3a3..06025c730700f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -93,7 +93,7 @@ repos: types: [python] stages: [manual] additional_dependencies: &pyright_dependencies - - pyright@1.1.253 + - pyright@1.1.258 - repo: local hooks: - id: pyright_reportGeneralTypeIssues diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index fc5fcaeab7d2a..58b4d82bcbe5f 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -404,9 +404,12 @@ def _outer_indexer( # associated code in pandas 2.0. _is_backward_compat_public_numeric_index: bool = False - _engine_type: type[libindex.IndexEngine] | type[ - libindex.ExtensionEngine - ] = libindex.ObjectEngine + @property + def _engine_type( + self, + ) -> type[libindex.IndexEngine] | type[libindex.ExtensionEngine]: + return libindex.ObjectEngine + # whether we support partial string indexing. Overridden # in DatetimeIndex and PeriodIndex _supports_partial_string_indexing = False diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 9a70a4a1aa615..c1ae3cb1b16ea 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -192,7 +192,7 @@ def _should_fallback_to_positional(self) -> bool: _values: Categorical @property - def _engine_type(self): + def _engine_type(self) -> type[libindex.IndexEngine]: # self.codes can have dtype int8, int16, int32 or int64, so we need # to return the corresponding engine type (libindex.Int8Engine, etc.). return { diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 6aa2ff91ba933..f776585926024 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -252,9 +252,12 @@ class DatetimeIndex(DatetimeTimedeltaMixin): _typ = "datetimeindex" _data_cls = DatetimeArray - _engine_type = libindex.DatetimeEngine _supports_partial_string_indexing = True + @property + def _engine_type(self) -> type[libindex.DatetimeEngine]: + return libindex.DatetimeEngine + _data: DatetimeArray inferred_freq: str | None tz: tzinfo | None diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index f270a6e8b555f..56fcec751749b 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -106,7 +106,7 @@ class NumericIndex(Index): } @property - def _engine_type(self): + def _engine_type(self) -> type[libindex.IndexEngine]: # error: Invalid index type "Union[dtype[Any], ExtensionDtype]" for # "Dict[dtype[Any], Type[IndexEngine]]"; expected type "dtype[Any]" return self._engine_types[self.dtype] # type: ignore[index] @@ -373,10 +373,13 @@ class Int64Index(IntegerIndex): __doc__ = _num_index_shared_docs["class_descr"] % _index_descr_args _typ = "int64index" - _engine_type = libindex.Int64Engine _default_dtype = np.dtype(np.int64) _dtype_validation_metadata = (is_signed_integer_dtype, "signed integer") + @property + def _engine_type(self) -> type[libindex.Int64Engine]: + return libindex.Int64Engine + class UInt64Index(IntegerIndex): _index_descr_args = { @@ -388,10 +391,13 @@ class UInt64Index(IntegerIndex): __doc__ = _num_index_shared_docs["class_descr"] % _index_descr_args _typ = "uint64index" - _engine_type = libindex.UInt64Engine _default_dtype = np.dtype(np.uint64) _dtype_validation_metadata = (is_unsigned_integer_dtype, "unsigned integer") + @property + def _engine_type(self) -> type[libindex.UInt64Engine]: + return libindex.UInt64Engine + class Float64Index(NumericIndex): _index_descr_args = { diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index e3ab5e8624585..c034d9416eae7 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -159,9 +159,12 @@ class PeriodIndex(DatetimeIndexOpsMixin): dtype: PeriodDtype _data_cls = PeriodArray - _engine_type = libindex.PeriodEngine _supports_partial_string_indexing = True + @property + def _engine_type(self) -> type[libindex.PeriodEngine]: + return libindex.PeriodEngine + @cache_readonly # Signature of "_resolution_obj" incompatible with supertype "DatetimeIndexOpsMixin" def _resolution_obj(self) -> Resolution: # type: ignore[override] diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 12a995c7de99a..376c98b6e176f 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -104,11 +104,14 @@ class RangeIndex(NumericIndex): """ _typ = "rangeindex" - _engine_type = libindex.Int64Engine _dtype_validation_metadata = (is_signed_integer_dtype, "signed integer") _range: range _is_backward_compat_public_numeric_index: bool = False + @property + def _engine_type(self) -> type[libindex.Int64Engine]: + return libindex.Int64Engine + # -------------------------------------------------------------------- # Constructors diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index cdf09bbc3b78c..095c5d1b1ba03 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -101,7 +101,10 @@ class TimedeltaIndex(DatetimeTimedeltaMixin): _typ = "timedeltaindex" _data_cls = TimedeltaArray - _engine_type = libindex.TimedeltaEngine + + @property + def _engine_type(self) -> type[libindex.TimedeltaEngine]: + return libindex.TimedeltaEngine _data: TimedeltaArray diff --git a/pyright_reportGeneralTypeIssues.json b/pyright_reportGeneralTypeIssues.json index 98da481a6d80f..c482aa32600fb 100644 --- a/pyright_reportGeneralTypeIssues.json +++ b/pyright_reportGeneralTypeIssues.json @@ -15,7 +15,6 @@ "pandas/io/clipboard", "pandas/util/version", # and all files that currently don't pass - "pandas/_config/config.py", "pandas/_testing/__init__.py", "pandas/core/algorithms.py", "pandas/core/apply.py", @@ -58,7 +57,6 @@ "pandas/core/indexes/multi.py", "pandas/core/indexes/numeric.py", "pandas/core/indexes/period.py", - "pandas/core/indexes/range.py", "pandas/core/indexing.py", "pandas/core/internals/api.py", "pandas/core/internals/array_manager.py", @@ -80,7 +78,6 @@ "pandas/core/tools/datetimes.py", "pandas/core/tools/timedeltas.py", "pandas/core/util/hashing.py", - "pandas/core/util/numba_.py", "pandas/core/window/ewm.py", "pandas/core/window/rolling.py", "pandas/io/common.py",