From b112975a1ae18cb5492f2c47a7d379843e4bf3cc Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Tue, 19 Aug 2025 11:26:41 -0400 Subject: [PATCH 1/3] fix enum typing from _lib --- pandas/_libs/tslibs/dtypes.pyi | 77 ++++++++++++++++++---------------- pandas/core/arrays/period.py | 4 +- pandas/core/dtypes/dtypes.py | 5 ++- pandas/core/tools/datetimes.py | 2 +- 4 files changed, 47 insertions(+), 41 deletions(-) diff --git a/pandas/_libs/tslibs/dtypes.pyi b/pandas/_libs/tslibs/dtypes.pyi index 1a87e753b2061..821c465986203 100644 --- a/pandas/_libs/tslibs/dtypes.pyi +++ b/pandas/_libs/tslibs/dtypes.pyi @@ -28,33 +28,35 @@ class PeriodDtypeBase: def _td64_unit(self) -> str: ... class FreqGroup(Enum): - FR_ANN: int - FR_QTR: int - FR_MTH: int - FR_WK: int - FR_BUS: int - FR_DAY: int - FR_HR: int - FR_MIN: int - FR_SEC: int - FR_MS: int - FR_US: int - FR_NS: int - FR_UND: int + _value_: int + FR_ANN = ... + FR_QTR = ... + FR_MTH = ... + FR_WK = ... + FR_BUS = ... + FR_DAY = ... + FR_HR = ... + FR_MIN = ... + FR_SEC = ... + FR_MS = ... + FR_US = ... + FR_NS = ... + FR_UND = ... @staticmethod def from_period_dtype_code(code: int) -> FreqGroup: ... class Resolution(Enum): - RESO_NS: int - RESO_US: int - RESO_MS: int - RESO_SEC: int - RESO_MIN: int - RESO_HR: int - RESO_DAY: int - RESO_MTH: int - RESO_QTR: int - RESO_YR: int + _value_: int + RESO_NS = ... + RESO_US = ... + RESO_MS = ... + RESO_SEC = ... + RESO_MIN = ... + RESO_HR = ... + RESO_DAY = ... + RESO_MTH = ... + RESO_QTR = ... + RESO_YR = ... def __lt__(self, other: Resolution) -> bool: ... def __ge__(self, other: Resolution) -> bool: ... @property @@ -67,17 +69,18 @@ class Resolution(Enum): def attr_abbrev(self) -> str: ... class NpyDatetimeUnit(Enum): - NPY_FR_Y: int - NPY_FR_M: int - NPY_FR_W: int - NPY_FR_D: int - NPY_FR_h: int - NPY_FR_m: int - NPY_FR_s: int - NPY_FR_ms: int - NPY_FR_us: int - NPY_FR_ns: int - NPY_FR_ps: int - NPY_FR_fs: int - NPY_FR_as: int - NPY_FR_GENERIC: int + _value_: int + NPY_FR_Y = ... + NPY_FR_M = ... + NPY_FR_W = ... + NPY_FR_D = ... + NPY_FR_h = ... + NPY_FR_m = ... + NPY_FR_s = ... + NPY_FR_ms = ... + NPY_FR_us = ... + NPY_FR_ns = ... + NPY_FR_ps = ... + NPY_FR_fs = ... + NPY_FR_as = ... + NPY_FR_GENERIC = ... diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 1670c0f5da605..6e2d4a2e66316 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -1435,11 +1435,11 @@ def _range_from_fields( if quarter is not None: if freq is None: freq = to_offset("Q", is_period=True) - base = FreqGroup.FR_QTR.value + base = cast(int, FreqGroup.FR_QTR.value) else: freq = to_offset(freq, is_period=True) base = libperiod.freq_to_dtype_code(freq) - if base != FreqGroup.FR_QTR.value: + if base != cast(int, FreqGroup.FR_QTR.value): raise AssertionError("base must equal FR_QTR") freqstr = freq.freqstr diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index eb5c7739e5132..ec10d6a83e798 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -1030,7 +1030,10 @@ class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype): # "Dict[int, PandasExtensionDtype]", base class "PandasExtensionDtype" # defined the type as "Dict[str, PandasExtensionDtype]") [assignment] _cache_dtypes: dict[BaseOffset, int] = {} # type: ignore[assignment] - __hash__ = PeriodDtypeBase.__hash__ + # error: Incompatible types in assignment (expression has type + # "Callable[[PeriodDtypeBase], int]", base class "PandasExtensionDtype" + # defined the type as "Callable[[PandasExtensionDtype], int]") + __hash__ = PeriodDtypeBase.__hash__ # type: ignore[assignment] _freq: BaseOffset _supports_2d = True _can_fast_transpose = True diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 1b236deff330d..040fcd02ab211 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -528,7 +528,7 @@ def _to_datetime_with_unit(arg, unit, name, utc: bool, errors: str) -> Index: utc=utc, errors=errors, unit_for_numerics=unit, - creso=NpyDatetimeUnit.NPY_FR_ns.value, + creso=cast(int, NpyDatetimeUnit.NPY_FR_ns.value), ) result = DatetimeIndex(arr, name=name) From a1c195d54c5c05de9422f9c83538ae4a5ff7abd6 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Tue, 19 Aug 2025 13:53:22 -0400 Subject: [PATCH 2/3] remove added ignore --- pandas/core/dtypes/dtypes.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index ec10d6a83e798..eb5c7739e5132 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -1030,10 +1030,7 @@ class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype): # "Dict[int, PandasExtensionDtype]", base class "PandasExtensionDtype" # defined the type as "Dict[str, PandasExtensionDtype]") [assignment] _cache_dtypes: dict[BaseOffset, int] = {} # type: ignore[assignment] - # error: Incompatible types in assignment (expression has type - # "Callable[[PeriodDtypeBase], int]", base class "PandasExtensionDtype" - # defined the type as "Callable[[PandasExtensionDtype], int]") - __hash__ = PeriodDtypeBase.__hash__ # type: ignore[assignment] + __hash__ = PeriodDtypeBase.__hash__ _freq: BaseOffset _supports_2d = True _can_fast_transpose = True From 8df3e11b6799116476324e8564333198326321af Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Mon, 25 Aug 2025 11:19:06 -0400 Subject: [PATCH 3/3] for stubtest, handle enums --- scripts/run_stubtest.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/scripts/run_stubtest.py b/scripts/run_stubtest.py index e87a7d53f4ff3..f136f93dab5a9 100644 --- a/scripts/run_stubtest.py +++ b/scripts/run_stubtest.py @@ -74,6 +74,44 @@ "pandas._libs.tslibs.offsets.BusinessHour.rollforward ", # type alias "pandas._libs.tslibs.timedeltas.UnitChoices", + # enum types in cython vs PYI + "pandas._libs.tslibs.dtypes.FreqGroup.FR_ANN", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_BUS", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_DAY", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_HR", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_MIN", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_MS", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_MTH", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_NS", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_QTR", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_SEC", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_UND", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_US", + "pandas._libs.tslibs.dtypes.FreqGroup.FR_WK", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_D", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_GENERIC", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_M", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_W", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_Y", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_as", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_fs", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_h", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_m", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_ms", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_ns", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_ps", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_s", + "pandas._libs.tslibs.dtypes.NpyDatetimeUnit.NPY_FR_us", + "pandas._libs.tslibs.dtypes.Resolution.RESO_DAY", + "pandas._libs.tslibs.dtypes.Resolution.RESO_HR", + "pandas._libs.tslibs.dtypes.Resolution.RESO_MIN", + "pandas._libs.tslibs.dtypes.Resolution.RESO_MS", + "pandas._libs.tslibs.dtypes.Resolution.RESO_MTH", + "pandas._libs.tslibs.dtypes.Resolution.RESO_NS", + "pandas._libs.tslibs.dtypes.Resolution.RESO_QTR", + "pandas._libs.tslibs.dtypes.Resolution.RESO_SEC", + "pandas._libs.tslibs.dtypes.Resolution.RESO_US", + "pandas._libs.tslibs.dtypes.Resolution.RESO_YR", ] if __name__ == "__main__":