From d7d45a3063626a1eeb44855819dd55a85ee8c964 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Tue, 16 Aug 2022 13:39:40 -0400 Subject: [PATCH 1/4] revert Timestamp and Timedelta constructors typing allowing NaTType return --- pandas/_libs/tslibs/timedeltas.pyi | 8 +++----- pandas/_libs/tslibs/timestamps.pyi | 9 ++++----- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyi b/pandas/_libs/tslibs/timedeltas.pyi index d100108e7dd2b..1fb2bf1b45888 100644 --- a/pandas/_libs/tslibs/timedeltas.pyi +++ b/pandas/_libs/tslibs/timedeltas.pyi @@ -82,15 +82,13 @@ class Timedelta(timedelta): max: ClassVar[Timedelta] resolution: ClassVar[Timedelta] value: int # np.int64 - def __new__( + # error: "__new__" must return a class instance (got "Union[Timestamp, NaTType]") + def __new__( # type: ignore[misc] cls: type[_S], value=..., unit: str | None = ..., **kwargs: float | np.integer | np.floating, - ) -> _S: ... - # GH 46171 - # While Timedelta can return pd.NaT, having the constructor return - # a Union with NaTType makes things awkward for users of pandas + ) -> _S | NaTType: ... @classmethod def _from_value_and_reso(cls, value: np.int64, reso: int) -> Timedelta: ... @property diff --git a/pandas/_libs/tslibs/timestamps.pyi b/pandas/_libs/tslibs/timestamps.pyi index f39d1f44d82c0..e4be7fda43005 100644 --- a/pandas/_libs/tslibs/timestamps.pyi +++ b/pandas/_libs/tslibs/timestamps.pyi @@ -16,6 +16,7 @@ import numpy as np from pandas._libs.tslibs import ( BaseOffset, + NaTType, Period, Tick, Timedelta, @@ -31,7 +32,8 @@ class Timestamp(datetime): resolution: ClassVar[Timedelta] value: int # np.int64 - def __new__( + # error: "__new__" must return a class instance (got "Union[Timestamp, NaTType]") + def __new__( # type: ignore[misc] cls: type[_DatetimeT], ts_input: np.integer | float | str | _date | datetime | np.datetime64 = ..., freq: int | None | str | BaseOffset = ..., @@ -48,10 +50,7 @@ class Timestamp(datetime): tzinfo: _tzinfo | None = ..., *, fold: int | None = ..., - ) -> _DatetimeT: ... - # GH 46171 - # While Timestamp can return pd.NaT, having the constructor return - # a Union with NaTType makes things awkward for users of pandas + ) -> _DatetimeT | NaTType: ... def _set_freq(self, freq: BaseOffset | None) -> None: ... @classmethod def _from_value_and_reso( From 8943224030d8247329b8927a2108a527754a4a92 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Tue, 16 Aug 2022 15:30:33 -0400 Subject: [PATCH 2/4] exclude 2 files from pyright. Fix up tz methods for NaTType --- pandas/_libs/tslibs/nattype.pyi | 13 +++++++++++-- pyright_reportGeneralTypeIssues.json | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/nattype.pyi b/pandas/_libs/tslibs/nattype.pyi index 0aa80330b15bc..d6f7b21cd17d1 100644 --- a/pandas/_libs/tslibs/nattype.pyi +++ b/pandas/_libs/tslibs/nattype.pyi @@ -67,8 +67,17 @@ class NaTType: def round(self) -> NaTType: ... def floor(self) -> NaTType: ... def ceil(self) -> NaTType: ... - def tz_convert(self) -> NaTType: ... - def tz_localize(self) -> NaTType: ... + @property + def tzinfo(self) -> _tzinfo | None: ... + @property + def tz(self) -> _tzinfo | None: ... + def tz_convert(self, tz: _tzinfo | str | None) -> NaTType: ... + def tz_localize( + self, + tz: _tzinfo | str | None, + ambiguous: str = ..., + nonexistent: str = ..., + ) -> NaTType: ... def replace( self, year: int | None = ..., diff --git a/pyright_reportGeneralTypeIssues.json b/pyright_reportGeneralTypeIssues.json index c482aa32600fb..3f20611f2c46d 100644 --- a/pyright_reportGeneralTypeIssues.json +++ b/pyright_reportGeneralTypeIssues.json @@ -16,6 +16,7 @@ "pandas/util/version", # and all files that currently don't pass "pandas/_testing/__init__.py", + "pandas/_testing/_hypothesis.py", "pandas/core/algorithms.py", "pandas/core/apply.py", "pandas/core/array_algos/take.py", @@ -57,6 +58,7 @@ "pandas/core/indexes/multi.py", "pandas/core/indexes/numeric.py", "pandas/core/indexes/period.py", + "pandas/core/indexes/timedeltas.py", "pandas/core/indexing.py", "pandas/core/internals/api.py", "pandas/core/internals/array_manager.py", From 6a7ee5c9000a5c0513d7941b8f52d07084ead73e Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Tue, 16 Aug 2022 16:55:37 -0400 Subject: [PATCH 3/4] add tz and tzinfo methods --- pandas/_libs/tslibs/nattype.pyi | 4 ++-- pandas/_libs/tslibs/nattype.pyx | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/nattype.pyi b/pandas/_libs/tslibs/nattype.pyi index d6f7b21cd17d1..e9ae46cee7aec 100644 --- a/pandas/_libs/tslibs/nattype.pyi +++ b/pandas/_libs/tslibs/nattype.pyi @@ -68,9 +68,9 @@ class NaTType: def floor(self) -> NaTType: ... def ceil(self) -> NaTType: ... @property - def tzinfo(self) -> _tzinfo | None: ... + def tzinfo(self) -> None: ... @property - def tz(self) -> _tzinfo | None: ... + def tz(self) -> None: ... def tz_convert(self, tz: _tzinfo | str | None) -> NaTType: ... def tz_localize( self, diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index b05b0ba636251..b3dd5b7907cad 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -1203,6 +1203,13 @@ default 'raise' NaT """, ) + @property + def tz(self) -> None: + return None + + @property + def tzinfo(self) -> None: + return None c_NaT = NaTType() # C-visible From bb8a78eeb9bdf932a12aa4a6652e72be4fa9980a Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Tue, 16 Aug 2022 18:31:48 -0400 Subject: [PATCH 4/4] remove check that tz doesn't exist for NaT --- pandas/tests/scalar/test_nat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/scalar/test_nat.py b/pandas/tests/scalar/test_nat.py index 1a07c02f4024a..55577af7be9d9 100644 --- a/pandas/tests/scalar/test_nat.py +++ b/pandas/tests/scalar/test_nat.py @@ -190,7 +190,7 @@ def test_nat_iso_format(get_nat): @pytest.mark.parametrize( "klass,expected", [ - (Timestamp, ["freqstr", "normalize", "to_julian_date", "to_period", "tz"]), + (Timestamp, ["freqstr", "normalize", "to_julian_date", "to_period"]), ( Timedelta, [