Skip to content

Improved the type stubs in the _libs directory to help with type checking #43744

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 47 additions & 0 deletions pandas/_libs/interval.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import FrozenSet, Tuple

import numpy as np

class IntervalMixin:
@property
def _left(self) -> object: ...
@property
def _right(self) -> object: ...
@property
def closed_left(self) -> bool: ...
@property
def closed_right(self) -> bool: ...
@property
def open_left(self) -> bool: ...
@property
def open_right(self) -> bool: ...
@property
def mid(self) -> float: ...
@property
def length(self) -> float: ...
@property
def is_empty(self) -> bool: ...
def _check_closed_matches(self, other: IntervalMixin, name: str=...) -> None: ...

class Interval(IntervalMixin):
def __init__(self, left: object, right: object, closed: str=...) -> None: ...
@property
def closed(self) -> str: ...
@property
def left(self) -> object: ...
@property
def right(self) -> object: ...
def __str__(self) -> str: ...
def __add__(self, y: Interval) -> Interval: ...
def __sub__(self, y: Interval) -> Interval: ...
def __mul__(self, y: Interval) -> Interval: ...
def __trudeiv__(self, y: Interval) -> Interval: ...
def __floordiv__(self, y: Interval) -> Interval: ...
def __overlaps__(self, other: Interval) -> bool: ...

VALID_CLOSED: FrozenSet[str]

def intervals_to_interval_bounds(
intervals: np.ndarray,
valdiate_closed: bool = ...
) -> Tuple[object, object, str]: ...
12 changes: 12 additions & 0 deletions pandas/_libs/missing.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class C_NAType:
...


class NAType(C_NAType):
...

NA: NAType

def is_matching_na(left: object, right: object, nan_matches_none: bool = ...) -> bool:
...

3 changes: 3 additions & 0 deletions pandas/_libs/properties.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

cache_readonly = property
Copy link
Member

@twoertwein twoertwein Sep 25, 2021

Choose a reason for hiding this comment

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

I think this will need many ignore statements, since there are quite a few places where a typed cache_readonly will reveal existing type issues (until now, mypy couldn't find cache_readonly).

I think you will also need to add annotations for AxisProperty as that is important elsewhere. It would be great to have partial type-stubs.


2 changes: 2 additions & 0 deletions pandas/_libs/tslibs/ccalendar.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ def get_firstbday(year: int, month: int) -> int: ...
def get_lastbday(year: int, month: int) -> int: ...
def get_day_of_year(year: int, month: int, day: int) -> int: ...
def get_iso_calendar(year: int, month: int, day: int) -> tuple[int, int, int]: ...
def is_leapyear(year: int) -> bool: ...
def get_week_of_year(year: int, month: int, day: int) -> int: ...
def get_days_in_month(year: int, month: int) -> int: ...
def dayofweek(y: int, m: int, d: int) -> int: ...
46 changes: 23 additions & 23 deletions pandas/_libs/tslibs/dtypes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@ class PeriodDtypeBase:
def from_date_offset(cls, offset: BaseOffset) -> PeriodDtypeBase: ...

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 = ...
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
@staticmethod
def get_freq_group(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 = ...
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
def __lt__(self, other: Resolution) -> bool: ...
def __ge__(self, other: Resolution) -> bool: ...
@property
Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/nattype.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ iNaT: int
nat_strings: set[str]

def is_null_datetimelike(val: object, inat_is_null: bool = ...) -> bool: ...
def checknull_with_nat(val: object) -> bool: ...
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need to expose this to python code.


class NaTType(datetime):
value: np.int64
Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/np_datetime.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class OutOfBoundsDatetime(ValueError): ...
102 changes: 102 additions & 0 deletions pandas/_libs/tslibs/offsets.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from __future__ import annotations
from datetime import datetime
from typing import Any, Tuple, Union
from datetime import timedelta

class BaseOffset:
def __init__(self, n: int = ..., normalize: bool = ...) -> None: ...
def __eq__(self, other) -> bool: ...
def __ne__(self, other) -> bool: ...
def __hash__(self) -> int: ...
@property
def kwds(self) -> dict: ...
@property
def base(self) -> BaseOffset: ...
def __add__(self, other) -> BaseOffset: ...
def __sub__(self, other) -> BaseOffset: ...
def __call__(self, other): ...
def __mul__(self, other): ...
def __neg__(self) -> BaseOffset: ...
def copy(self) -> BaseOffset: ...
def __repr__(self) -> str: ...
@property
def name(self) -> str: ...
@property
def rule_code(self) -> str: ...
def freqstr(self) -> str: ...
# Next one is problematic due to circular imports
Copy link
Member

Choose a reason for hiding this comment

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

do it inside a if TYPE_CHECKING block

#def apply_index(self, dtindex: DatetimeIndex) -> DatetimeIndex: ...
def apply_index(self, dtindex): ...
def _apply_array(self, dtarr) -> None: ...
def rollback(self, dt: datetime) -> datetime: ...
def rollforward(self, dt: datetime) -> datetime: ...
def is_on_offset(self, dt: datetime) -> bool: ...
def __setstate__(self, state) -> None: ...
def __getstate__(self): ...
@property
def nanos(self) -> int: ...
def onOffset(self, dt: datetime) -> bool: ...
def isAnchored(self) -> bool: ...
def is_anchored(self) -> bool: ...

class SingleConstructorOffset(BaseOffset):
@classmethod
def _from_name(cls, suffix=None): ...
def __reduce__(self): ...

def to_offset(freq: Union[str, Tuple, timedelta, BaseOffset, None]) -> Union[BaseOffset, None]: ...

class Tick(SingleConstructorOffset):
def __init__(self, n: int = ..., normalize: bool = ...) -> None: ...

class Day(Tick): ...
class Hour(Tick): ...
class Minute(Tick): ...
class Second(Tick): ...
class Milli(Tick): ...
class Micro(Tick): ...
class Nano(Tick): ...
class RelativeDeltaOffset(BaseOffset):
def __init__(self, n: int = ..., normalize: bool = ..., **kwds: Any) -> None: ...
class BusinessMixin(SingleConstructorOffset): ...
class BusinessDay(BusinessMixin): ...
class BusinessHour(BusinessMixin): ...
class WeekOfMonthMixin(SingleConstructorOffset): ...
class YearOffset(SingleConstructorOffset): ...
class BYearEnd(YearOffset): ...
class BYearBegin(YearOffset): ...
class YearEnd(YearOffset): ...
class YearBegin(YearOffset): ...
class QuarterOffset(SingleConstructorOffset): ...
class BQuarterEnd(QuarterOffset): ...
class BQuarterBegin(QuarterOffset): ...
class QuarterEnd(QuarterOffset): ...
class QuarterBegin(QuarterOffset): ...
class MonthOffset(SingleConstructorOffset): ...
class MonthEnd(MonthOffset): ...
class MonthBegin(MonthOffset): ...
class BusinessMonthEnd(MonthOffset): ...
class BusinessMonthBegin(MonthOffset): ...
class SemiMonthOffset(SingleConstructorOffset): ...
class SemiMonthEnd(SemiMonthOffset): ...
class SemiMonthBegin(SemiMonthOffset): ...
class Week(SingleConstructorOffset): ...
class WeekOfMonth(WeekOfMonthMixin): ...
class LastWeekOfMonth(WeekOfMonthMixin): ...
class FY5253Mixin(SingleConstructorOffset): ...
class FY5253(FY5253Mixin): ...
class FY5253Quarter(FY5253Mixin): ...
class Easter(SingleConstructorOffset): ...
class _CustomBusinessMonth(BusinessMixin): ...
class CustomBusinessDay(BusinessDay): ...
class CustomBusinessHour(BusinessHour): ...
class CustomBusinessMonthEnd(_CustomBusinessMonth): ...
class CustomBusinessMonthBegin(_CustomBusinessMonth): ...
class DateOffset(RelativeDeltaOffset): ...

BDay = BusinessDay
BMonthEnd = BusinessMonthEnd
BMonthBegin = BusinessMonthBegin
CBMonthEnd = CustomBusinessMonthEnd
CBMonthBegin = CustomBusinessMonthBegin
CDay = CustomBusinessDay
4 changes: 4 additions & 0 deletions pandas/_libs/tslibs/period.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from typing import Literal

import numpy as np
Expand All @@ -14,6 +15,9 @@ from pandas._typing import (
INVALID_FREQ_ERR_MSG: str
DIFFERENT_FREQ: str

def is_period_object(obj: object) -> bool: ...
def get_period_ordinal(dts: datetime, freq: int) -> int: ...

class IncompatibleFrequency(ValueError): ...

def periodarr_to_dt64arr(
Expand Down
1 change: 0 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import numpy as np

from pandas._libs.tslibs import (
BaseOffset,
NaT,
NaTType,
Period,
Timedelta,
Expand Down