Skip to content

Commit

Permalink
revert Timestamp and Timedelta constructors typing allowing NaTType r…
Browse files Browse the repository at this point in the history
…eturn (pandas-dev#48112)

* revert Timestamp and Timedelta constructors typing allowing NaTType return

* exclude 2 files from pyright.  Fix up tz methods for NaTType

* add tz and tzinfo methods

* remove check that tz doesn't exist for NaT
  • Loading branch information
Dr-Irv authored and noatamir committed Nov 9, 2022
1 parent 91f1516 commit eeb2bdd
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
13 changes: 11 additions & 2 deletions pandas/_libs/tslibs/nattype.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> None: ...
@property
def tz(self) -> 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 = ...,
Expand Down
7 changes: 7 additions & 0 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions pandas/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions pandas/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import numpy as np

from pandas._libs.tslibs import (
BaseOffset,
NaTType,
Period,
Tick,
Timedelta,
Expand All @@ -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 = ...,
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/scalar/test_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
[
Expand Down
2 changes: 2 additions & 0 deletions pyright_reportGeneralTypeIssues.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit eeb2bdd

Please sign in to comment.