Skip to content

TYP: use annotations in strprime.pyi and timestamps.pyi #41841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions pandas/_libs/tslibs/strptime.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import Optional

import numpy as np

def array_strptime(
values: np.ndarray, # np.ndarray[object]
fmt: Optional[str],
fmt: str | None,
exact: bool = True,
errors: str = "raise"
errors: str = "raise",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while cleaning these stubs can also remove the default values and just use ...

from https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions

Stub files should only contain information necessary for the type checker, and leave out unnecessary detail:

for arguments with a default, use ... instead of the actual default;

) -> tuple[np.ndarray, np.ndarray]: ...
# first ndarray is M8[ns], second is object ndarray of Optional[tzinfo]

# first ndarray is M8[ns], second is object ndarray of tzinfo | None
76 changes: 33 additions & 43 deletions pandas/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import sys
from time import struct_time
from typing import (
ClassVar,
Optional,
Type,
TypeVar,
overload,
Expand All @@ -27,10 +26,8 @@ from pandas._libs.tslibs import (

_S = TypeVar("_S")


def integer_op_not_supported(obj) -> None: ...


class Timestamp(datetime):
min: ClassVar[Timestamp]
max: ClassVar[Timestamp]
Expand All @@ -41,9 +38,15 @@ class Timestamp(datetime):
# error: "__new__" must return a class instance (got "Union[Timestamp, NaTType]")
def __new__( # type: ignore[misc]
cls: Type[_S],
ts_input: int | np.integer | float | str | _date | datetime | np.datetime64 = ...,
ts_input: int
| np.integer
| float
| str
| _date
| datetime
| np.datetime64 = ...,
freq=...,
tz: str | _tzinfo | None | int= ...,
tz: str | _tzinfo | None | int = ...,
unit=...,
year: int | None = ...,
month: int | None = ...,
Expand All @@ -55,7 +58,7 @@ class Timestamp(datetime):
nanosecond: int | None = ...,
tzinfo: _tzinfo | None = ...,
*,
fold: int | None= ...,
fold: int | None = ...,
) -> _S | NaTType: ...

def _set_freq(self, freq: BaseOffset | None) -> None: ...
Expand All @@ -75,22 +78,19 @@ class Timestamp(datetime):
@property
def microsecond(self) -> int: ...
@property
def tzinfo(self) -> Optional[_tzinfo]: ...
def tzinfo(self) -> _tzinfo | None: ...
@property
def tz(self) -> Optional[_tzinfo]: ...

def tz(self) -> _tzinfo | None: ...
@property
def fold(self) -> int: ...

@classmethod
def fromtimestamp(cls: Type[_S], t: float, tz: Optional[_tzinfo] = ...) -> _S: ...
def fromtimestamp(cls: Type[_S], t: float, tz: _tzinfo | None = ...) -> _S: ...
@classmethod
def utcfromtimestamp(cls: Type[_S], t: float) -> _S: ...
@classmethod
def today(cls: Type[_S]) -> _S: ...
@classmethod
def fromordinal(cls: Type[_S], n: int) -> _S: ...

if sys.version_info >= (3, 8):
@classmethod
def now(cls: Type[_S], tz: _tzinfo | str | None = ...) -> _S: ...
Expand All @@ -101,28 +101,23 @@ class Timestamp(datetime):
@overload
@classmethod
def now(cls, tz: _tzinfo) -> datetime: ...

@classmethod
def utcnow(cls: Type[_S]) -> _S: ...
@classmethod
def combine(cls, date: _date, time: _time, tzinfo: Optional[_tzinfo] = ...) -> datetime: ...

def combine(
cls, date: _date, time: _time, tzinfo: _tzinfo | None = ...
) -> datetime: ...
@classmethod
def fromisoformat(cls: Type[_S], date_string: str) -> _S: ...

def strftime(self, fmt: str) -> str: ...
def __format__(self, fmt: str) -> str: ...

def toordinal(self) -> int: ...
def timetuple(self) -> struct_time: ...

def timestamp(self) -> float: ...

def utctimetuple(self) -> struct_time: ...
def date(self) -> _date: ...
def time(self) -> _time: ...
def timetz(self) -> _time: ...

def replace(
self,
year: int = ...,
Expand All @@ -132,26 +127,21 @@ class Timestamp(datetime):
minute: int = ...,
second: int = ...,
microsecond: int = ...,
tzinfo: Optional[_tzinfo] = ...,
tzinfo: _tzinfo | None = ...,
*,
fold: int = ...,
) -> datetime: ...

if sys.version_info >= (3, 8):
def astimezone(self: _S, tz: Optional[_tzinfo] = ...) -> _S: ...
def astimezone(self: _S, tz: _tzinfo | None = ...) -> _S: ...
else:
def astimezone(self, tz: Optional[_tzinfo] = ...) -> datetime: ...

def astimezone(self, tz: _tzinfo | None = ...) -> datetime: ...
def ctime(self) -> str: ...
def isoformat(self, sep: str = ..., timespec: str = ...) -> str: ...

@classmethod
def strptime(cls, date_string: str, format: str) -> datetime: ...

def utcoffset(self) -> Optional[timedelta]: ...
def tzname(self) -> Optional[str]: ...
def dst(self) -> Optional[timedelta]: ...

def utcoffset(self) -> timedelta | None: ...
def tzname(self) -> str | None: ...
def dst(self) -> timedelta | None: ...
def __le__(self, other: datetime) -> bool: ... # type: ignore
def __lt__(self, other: datetime) -> bool: ... # type: ignore
def __ge__(self, other: datetime) -> bool: ... # type: ignore
Expand All @@ -166,12 +156,10 @@ class Timestamp(datetime):
def __sub__(self, other: datetime) -> timedelta: ...
@overload
def __sub__(self, other: timedelta) -> datetime: ...

def __hash__(self) -> int: ...
def weekday(self) -> int: ...
def isoweekday(self) -> int: ...
def isocalendar(self) -> tuple[int, int, int]: ...

@property
def is_leap_year(self) -> bool: ...
@property
Expand All @@ -186,23 +174,25 @@ class Timestamp(datetime):
def is_quarter_end(self) -> bool: ...
@property
def is_year_end(self) -> bool: ...

def to_pydatetime(self, warn: bool = ...) -> datetime: ...
def to_datetime64(self) -> np.datetime64: ...
def to_period(self, freq) -> Period: ...
def to_julian_date(self) -> np.float64: ...

@property
def asm8(self) -> np.datetime64: ...

def tz_convert(self: _S, tz) -> _S: ...

# TODO: could return NaT?
def tz_localize(self: _S, tz, ambiguous: str = ..., nonexistent: str = ...) -> _S: ...

def tz_localize(
self: _S, tz, ambiguous: str = ..., nonexistent: str = ...
) -> _S: ...
def normalize(self: _S) -> _S: ...

# TODO: round/floor/ceil could return NaT?
def round(self: _S, freq, ambiguous: bool | str = ..., nonexistent: str = ...) -> _S: ...
def floor(self: _S, freq, ambiguous: bool | str = ..., nonexistent: str = ...) -> _S: ...
def ceil(self: _S, freq, ambiguous: bool | str = ..., nonexistent: str = ...) -> _S: ...
def round(
self: _S, freq, ambiguous: bool | str = ..., nonexistent: str = ...
) -> _S: ...
def floor(
self: _S, freq, ambiguous: bool | str = ..., nonexistent: str = ...
) -> _S: ...
def ceil(
self: _S, freq, ambiguous: bool | str = ..., nonexistent: str = ...
) -> _S: ...