Skip to content

Commit

Permalink
TYP: Misc type corrections (pandas-dev#55078)
Browse files Browse the repository at this point in the history
  • Loading branch information
twoertwein authored and mroeschke committed Sep 11, 2023
1 parent 50cb682 commit 0305571
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Period(PeriodMixin):
@classmethod
def _from_ordinal(cls, ordinal: int, freq) -> Period: ...
@classmethod
def now(cls, freq: BaseOffset = ...) -> Period: ...
def now(cls, freq: Frequency = ...) -> Period: ...
def strftime(self, fmt: str) -> str: ...
def to_timestamp(
self,
Expand Down
7 changes: 4 additions & 3 deletions pandas/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ from pandas._libs.tslibs import (
Tick,
)
from pandas._typing import (
Frequency,
Self,
npt,
)
Expand Down Expand Up @@ -117,9 +118,9 @@ class Timedelta(timedelta):
@property
def asm8(self) -> np.timedelta64: ...
# TODO: round/floor/ceil could return NaT?
def round(self, freq: str) -> Self: ...
def floor(self, freq: str) -> Self: ...
def ceil(self, freq: str) -> Self: ...
def round(self, freq: Frequency) -> Self: ...
def floor(self, freq: Frequency) -> Self: ...
def ceil(self, freq: Frequency) -> Self: ...
@property
def resolution_string(self) -> str: ...
def __add__(self, other: timedelta) -> Timedelta: ...
Expand Down
29 changes: 16 additions & 13 deletions pandas/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ from datetime import (
from time import struct_time
from typing import (
ClassVar,
Literal,
TypeAlias,
TypeVar,
overload,
)
Expand All @@ -27,6 +29,7 @@ from pandas._typing import (
)

_DatetimeT = TypeVar("_DatetimeT", bound=datetime)
_TimeZones: TypeAlias = str | _tzinfo | None | int

def integer_op_not_supported(obj: object) -> TypeError: ...

Expand All @@ -51,13 +54,13 @@ class Timestamp(datetime):
tzinfo: _tzinfo | None = ...,
*,
nanosecond: int | None = ...,
tz: str | _tzinfo | None | int = ...,
tz: _TimeZones = ...,
unit: str | int | None = ...,
fold: int | None = ...,
) -> _DatetimeT | NaTType: ...
@classmethod
def _from_value_and_reso(
cls, value: int, reso: int, tz: _tzinfo | None
cls, value: int, reso: int, tz: _TimeZones
) -> Timestamp: ...
@property
def value(self) -> int: ... # np.int64
Expand All @@ -84,19 +87,19 @@ class Timestamp(datetime):
@property
def fold(self) -> int: ...
@classmethod
def fromtimestamp(cls, ts: float, tz: _tzinfo | None = ...) -> Self: ...
def fromtimestamp(cls, ts: float, tz: _TimeZones = ...) -> Self: ...
@classmethod
def utcfromtimestamp(cls, ts: float) -> Self: ...
@classmethod
def today(cls, tz: _tzinfo | str | None = ...) -> Self: ...
def today(cls, tz: _TimeZones = ...) -> Self: ...
@classmethod
def fromordinal(
cls,
ordinal: int,
tz: _tzinfo | str | None = ...,
tz: _TimeZones = ...,
) -> Self: ...
@classmethod
def now(cls, tz: _tzinfo | str | None = ...) -> Self: ...
def now(cls, tz: _TimeZones = ...) -> Self: ...
@classmethod
def utcnow(cls) -> Self: ...
# error: Signature of "combine" incompatible with supertype "datetime"
Expand Down Expand Up @@ -131,7 +134,7 @@ class Timestamp(datetime):
fold: int | None = ...,
) -> Self: ...
# LSP violation: datetime.datetime.astimezone has a default value for tz
def astimezone(self, tz: _tzinfo | None) -> Self: ... # type: ignore[override]
def astimezone(self, tz: _TimeZones) -> Self: ... # type: ignore[override]
def ctime(self) -> str: ...
def isoformat(self, sep: str = ..., timespec: str = ...) -> str: ...
@classmethod
Expand Down Expand Up @@ -184,32 +187,32 @@ class Timestamp(datetime):
def to_julian_date(self) -> np.float64: ...
@property
def asm8(self) -> np.datetime64: ...
def tz_convert(self, tz: _tzinfo | str | None) -> Self: ...
def tz_convert(self, tz: _TimeZones) -> Self: ...
# TODO: could return NaT?
def tz_localize(
self,
tz: _tzinfo | str | None,
ambiguous: str = ...,
tz: _TimeZones,
ambiguous: bool | Literal["raise", "NaT"] = ...,
nonexistent: TimestampNonexistent = ...,
) -> Self: ...
def normalize(self) -> Self: ...
# TODO: round/floor/ceil could return NaT?
def round(
self,
freq: str,
ambiguous: bool | str = ...,
ambiguous: bool | Literal["raise", "NaT"] = ...,
nonexistent: TimestampNonexistent = ...,
) -> Self: ...
def floor(
self,
freq: str,
ambiguous: bool | str = ...,
ambiguous: bool | Literal["raise", "NaT"] = ...,
nonexistent: TimestampNonexistent = ...,
) -> Self: ...
def ceil(
self,
freq: str,
ambiguous: bool | str = ...,
ambiguous: bool | Literal["raise", "NaT"] = ...,
nonexistent: TimestampNonexistent = ...,
) -> Self: ...
def day_name(self, locale: str | None = ...) -> str: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
# Cannot use `Sequence` because a string is a sequence, and we don't want to
# accept that. Could refine if https://github.com/python/typing/issues/256 is
# resolved to differentiate between Sequence[str] and str
ListLike = Union[AnyArrayLike, list, range]
ListLike = Union[AnyArrayLike, list, tuple, range]

# scalars

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import (
TYPE_CHECKING,
Callable,
Literal,
cast,
)

Expand Down Expand Up @@ -569,7 +570,7 @@ def crosstab(
margins: bool = False,
margins_name: Hashable = "All",
dropna: bool = True,
normalize: bool = False,
normalize: bool | Literal[0, 1, "all", "index", "columns"] = False,
) -> DataFrame:
"""
Compute a simple cross tabulation of two (or more) factors.
Expand Down
50 changes: 40 additions & 10 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,10 @@ def read_csv(
header: int | Sequence[int] | None | Literal["infer"] = ...,
names: Sequence[Hashable] | None | lib.NoDefault = ...,
index_col: IndexLabel | Literal[False] | None = ...,
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
usecols: list[HashableT]
| tuple[HashableT]
| Callable[[Hashable], bool]
| None = ...,
dtype: DtypeArg | None = ...,
engine: CSVEngine | None = ...,
converters: Mapping[Hashable, Callable] | None = ...,
Expand Down Expand Up @@ -697,7 +700,10 @@ def read_csv(
header: int | Sequence[int] | None | Literal["infer"] = ...,
names: Sequence[Hashable] | None | lib.NoDefault = ...,
index_col: IndexLabel | Literal[False] | None = ...,
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
usecols: list[HashableT]
| tuple[HashableT]
| Callable[[Hashable], bool]
| None = ...,
dtype: DtypeArg | None = ...,
engine: CSVEngine | None = ...,
converters: Mapping[Hashable, Callable] | None = ...,
Expand Down Expand Up @@ -757,7 +763,10 @@ def read_csv(
header: int | Sequence[int] | None | Literal["infer"] = ...,
names: Sequence[Hashable] | None | lib.NoDefault = ...,
index_col: IndexLabel | Literal[False] | None = ...,
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
usecols: list[HashableT]
| tuple[HashableT]
| Callable[[Hashable], bool]
| None = ...,
dtype: DtypeArg | None = ...,
engine: CSVEngine | None = ...,
converters: Mapping[Hashable, Callable] | None = ...,
Expand Down Expand Up @@ -817,7 +826,10 @@ def read_csv(
header: int | Sequence[int] | None | Literal["infer"] = ...,
names: Sequence[Hashable] | None | lib.NoDefault = ...,
index_col: IndexLabel | Literal[False] | None = ...,
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
usecols: list[HashableT]
| tuple[HashableT]
| Callable[[Hashable], bool]
| None = ...,
dtype: DtypeArg | None = ...,
engine: CSVEngine | None = ...,
converters: Mapping[Hashable, Callable] | None = ...,
Expand Down Expand Up @@ -888,7 +900,10 @@ def read_csv(
header: int | Sequence[int] | None | Literal["infer"] = "infer",
names: Sequence[Hashable] | None | lib.NoDefault = lib.no_default,
index_col: IndexLabel | Literal[False] | None = None,
usecols: list[HashableT] | Callable[[Hashable], bool] | None = None,
usecols: list[HashableT]
| tuple[HashableT]
| Callable[[Hashable], bool]
| None = None,
# General Parsing Configuration
dtype: DtypeArg | None = None,
engine: CSVEngine | None = None,
Expand Down Expand Up @@ -983,7 +998,10 @@ def read_table(
header: int | Sequence[int] | None | Literal["infer"] = ...,
names: Sequence[Hashable] | None | lib.NoDefault = ...,
index_col: IndexLabel | Literal[False] | None = ...,
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
usecols: list[HashableT]
| tuple[HashableT]
| Callable[[Hashable], bool]
| None = ...,
dtype: DtypeArg | None = ...,
engine: CSVEngine | None = ...,
converters: Mapping[Hashable, Callable] | None = ...,
Expand Down Expand Up @@ -1040,7 +1058,10 @@ def read_table(
header: int | Sequence[int] | None | Literal["infer"] = ...,
names: Sequence[Hashable] | None | lib.NoDefault = ...,
index_col: IndexLabel | Literal[False] | None = ...,
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
usecols: list[HashableT]
| tuple[HashableT]
| Callable[[Hashable], bool]
| None = ...,
dtype: DtypeArg | None = ...,
engine: CSVEngine | None = ...,
converters: Mapping[Hashable, Callable] | None = ...,
Expand Down Expand Up @@ -1097,7 +1118,10 @@ def read_table(
header: int | Sequence[int] | None | Literal["infer"] = ...,
names: Sequence[Hashable] | None | lib.NoDefault = ...,
index_col: IndexLabel | Literal[False] | None = ...,
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
usecols: list[HashableT]
| tuple[HashableT]
| Callable[[Hashable], bool]
| None = ...,
dtype: DtypeArg | None = ...,
engine: CSVEngine | None = ...,
converters: Mapping[Hashable, Callable] | None = ...,
Expand Down Expand Up @@ -1154,7 +1178,10 @@ def read_table(
header: int | Sequence[int] | None | Literal["infer"] = ...,
names: Sequence[Hashable] | None | lib.NoDefault = ...,
index_col: IndexLabel | Literal[False] | None = ...,
usecols: list[HashableT] | Callable[[Hashable], bool] | None = ...,
usecols: list[HashableT]
| tuple[HashableT]
| Callable[[Hashable], bool]
| None = ...,
dtype: DtypeArg | None = ...,
engine: CSVEngine | None = ...,
converters: Mapping[Hashable, Callable] | None = ...,
Expand Down Expand Up @@ -1224,7 +1251,10 @@ def read_table(
header: int | Sequence[int] | None | Literal["infer"] = "infer",
names: Sequence[Hashable] | None | lib.NoDefault = lib.no_default,
index_col: IndexLabel | Literal[False] | None = None,
usecols: list[HashableT] | Callable[[Hashable], bool] | None = None,
usecols: list[HashableT]
| tuple[HashableT]
| Callable[[Hashable], bool]
| None = None,
# General Parsing Configuration
dtype: DtypeArg | None = None,
engine: CSVEngine | None = None,
Expand Down

0 comments on commit 0305571

Please sign in to comment.