Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion pandas-stubs/_libs/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ VALID_CLOSED: frozenset[str]

_OrderableScalarT = TypeVar("_OrderableScalarT", bound=int | float)
_OrderableTimesT = TypeVar("_OrderableTimesT", bound=Timestamp | Timedelta)
_OrderableT = TypeVar("_OrderableT", bound=int | float | Timestamp | Timedelta)
_OrderableT = TypeVar(
"_OrderableT", bound=int | float | Timestamp | Timedelta, default=Any
)

@type_check_only
class _LengthDescriptor:
Expand Down
25 changes: 16 additions & 9 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ from pandas.tseries.offsets import (
P = ParamSpec("P")

HashableT = TypeVar("HashableT", bound=Hashable)
HashableT0 = TypeVar("HashableT0", bound=Hashable, default=Any)
HashableT1 = TypeVar("HashableT1", bound=Hashable)
HashableT2 = TypeVar("HashableT2", bound=Hashable)
HashableT3 = TypeVar("HashableT3", bound=Hashable)
Expand Down Expand Up @@ -776,7 +777,7 @@ XMLParsers: TypeAlias = Literal["lxml", "etree"]
HTMLFlavors: TypeAlias = Literal["lxml", "html5lib", "bs4"]

# Interval closed type
IntervalT = TypeVar("IntervalT", bound=Interval)
IntervalT = TypeVar("IntervalT", bound=Interval, default=Interval)
IntervalLeftRight: TypeAlias = Literal["left", "right"]
IntervalClosedType: TypeAlias = IntervalLeftRight | Literal["both", "neither"]

Expand Down Expand Up @@ -874,7 +875,11 @@ ExcelWriterMergeCells: TypeAlias = bool | Literal["columns"]

# read_csv: usecols
UsecolsArgType: TypeAlias = (
SequenceNotStr[Hashable] | range | AnyArrayLike | Callable[[HashableT], bool] | None
SequenceNotStr[Hashable]
| range
| AnyArrayLike
| Callable[[HashableT0], bool]
| None
)

# maintain the sub-type of any hashable sequence
Expand Down Expand Up @@ -920,6 +925,7 @@ PyArrowNotStrDtypeArg: TypeAlias = (
StrLike: TypeAlias = str | np.str_

ScalarT = TypeVar("ScalarT", bound=Scalar)
ScalarT0 = TypeVar("ScalarT0", bound=Scalar, default=Scalar)
# Refine the definitions below in 3.9 to use the specialized type.
np_num: TypeAlias = np.bool | np.integer | np.floating | np.complexfloating
np_ndarray_intp: TypeAlias = npt.NDArray[np.intp]
Expand Down Expand Up @@ -1015,8 +1021,9 @@ SeriesDType: TypeAlias = (
| datetime.datetime # includes pd.Timestamp
| datetime.timedelta # includes pd.Timedelta
)
S0 = TypeVar("S0", bound=SeriesDType, default=Any)
S1 = TypeVar("S1", bound=SeriesDType, default=Any)
# Like S1, but without `default=Any`.
# Like S0 and S1, but without `default=Any`.
S2 = TypeVar("S2", bound=SeriesDType)
S2_contra = TypeVar("S2_contra", bound=SeriesDType, contravariant=True)
S2_NDT_contra = TypeVar(
Expand Down Expand Up @@ -1050,14 +1057,14 @@ IndexingInt: TypeAlias = (
)

# AxesData is used for data for Index
AxesData: TypeAlias = Mapping[S3, Any] | Axes | KeysView[S3]
AxesData: TypeAlias = Mapping[S0, Any] | Axes | KeysView[S0]

# Any plain Python or numpy function
Function: TypeAlias = np.ufunc | Callable[..., Any]
# Use a distinct HashableT in shared types to avoid conflicts with
# shared HashableT and HashableT#. This one can be used if the identical
# type is need in a function that uses GroupByObjectNonScalar
_HashableTa = TypeVar("_HashableTa", bound=Hashable)
_HashableTa = TypeVar("_HashableTa", bound=Hashable, default=Any)
if TYPE_CHECKING: # noqa: PYI002
ByT = TypeVar(
"ByT",
Expand All @@ -1075,7 +1082,7 @@ if TYPE_CHECKING: # noqa: PYI002
| Scalar
| Period
| Interval[int | float | Timestamp | Timedelta]
| tuple,
| tuple[Any, ...],
)
# Use a distinct SeriesByT when using groupby with Series of known dtype.
# Essentially, an intersection between Series S1 TypeVar, and ByT TypeVar
Expand Down Expand Up @@ -1130,10 +1137,10 @@ StataDateFormat: TypeAlias = Literal[
# `DataFrame.replace` also accepts mappings of these.
ReplaceValue: TypeAlias = (
Scalar
| Pattern
| Pattern[str]
| NAType
| Sequence[Scalar | Pattern]
| Mapping[HashableT, ScalarT]
| Sequence[Scalar | Pattern[str]]
| Mapping[HashableT0, ScalarT0]
| Series
| None
)
Expand Down
8 changes: 6 additions & 2 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2761,8 +2761,12 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
def __rfloordiv__(
self, other: float | DataFrame | Series[int] | Series[float] | Sequence[float]
) -> Self: ...
def __truediv__(self, other: float | DataFrame | Series | Sequence) -> Self: ...
def __rtruediv__(self, other: float | DataFrame | Series | Sequence) -> Self: ...
def __truediv__(
self, other: float | DataFrame | Series | Sequence[Any]
) -> Self: ...
def __rtruediv__(
self, other: float | DataFrame | Series | Sequence[Any]
) -> Self: ...
@final
def __bool__(self) -> NoReturn: ...

Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
copy: bool = ...,
name: Hashable = ...,
tupleize_cols: bool = ...,
) -> IntervalIndex[Interval[Any]]: ...
) -> IntervalIndex[Interval]: ...
@overload
def __new__(
cls,
Expand Down
4 changes: 3 additions & 1 deletion pandas-stubs/core/indexes/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ class IntervalIndex(ExtensionIndex[IntervalT, np.object_], IntervalMixin):
def __contains__(self, key: IntervalT) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __contains__(self, key: object) -> Literal[False]: ...
def astype(self, dtype: DtypeArg, copy: bool = True) -> IntervalIndex: ...
def astype( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] # ty: ignore[invalid-method-override]
self, dtype: DtypeArg, copy: bool = True
) -> IntervalIndex: ...
@property
def inferred_type(self) -> str: ...
def memory_usage(self, deep: bool = False) -> int: ...
Expand Down
12 changes: 6 additions & 6 deletions pandas-stubs/core/reshape/pivot.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _PivotTableColumnsTypes: TypeAlias = (
_PivotTableValuesTypes: TypeAlias = Label | Sequence[Hashable] | None

_ExtendedAnyArrayLike: TypeAlias = AnyArrayLike | ArrayLike
_Values: TypeAlias = SequenceNotStr[Any] | _ExtendedAnyArrayLike
_CrossTabValues: TypeAlias = SequenceNotStr[Any] | _ExtendedAnyArrayLike

@overload
def pivot_table(
Expand Down Expand Up @@ -118,9 +118,9 @@ def pivot(
) -> DataFrame: ...
@overload
def crosstab(
index: _Values | list[_Values],
columns: _Values | list[_Values],
values: _Values,
index: _CrossTabValues | list[_CrossTabValues],
columns: _CrossTabValues | list[_CrossTabValues],
values: _CrossTabValues,
rownames: SequenceNotStr[Hashable] | None = None,
colnames: SequenceNotStr[Hashable] | None = None,
*,
Expand All @@ -132,8 +132,8 @@ def crosstab(
) -> DataFrame: ...
@overload
def crosstab(
index: _Values | list[_Values],
columns: _Values | list[_Values],
index: _CrossTabValues | list[_CrossTabValues],
columns: _CrossTabValues | list[_CrossTabValues],
values: None = None,
rownames: SequenceNotStr[Hashable] | None = None,
colnames: SequenceNotStr[Hashable] | None = None,
Expand Down
15 changes: 6 additions & 9 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ from typing_extensions import (
)
import xarray as xr

from pandas._libs.interval import (
Interval,
_OrderableT,
)
from pandas._libs.interval import Interval
from pandas._libs.lib import _NoDefaultDoNotUse
from pandas._libs.missing import NAType
from pandas._libs.tslibs import BaseOffset
Expand Down Expand Up @@ -443,16 +440,16 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
def __new__(
cls,
data: (
IntervalIndex[Interval[_OrderableT]]
| Interval[_OrderableT]
| Sequence[Interval[_OrderableT]]
| dict[HashableT1, Interval[_OrderableT]]
IntervalIndex[IntervalT]
| IntervalT
| Sequence[IntervalT]
| dict[Hashable, IntervalT]
),
index: AxesData | None = None,
dtype: Literal["Interval"] = ...,
name: Hashable = None,
copy: bool | None = None,
) -> Series[Interval[_OrderableT]]: ...
) -> Series[IntervalT]: ...
@overload
def __new__( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
cls,
Expand Down
3 changes: 2 additions & 1 deletion pandas-stubs/io/json/_json.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from collections import abc
from collections.abc import Mapping
from types import TracebackType
from typing import (
Any,
Generic,
Literal,
overload,
Expand Down Expand Up @@ -226,7 +227,7 @@ def read_json(
engine: Literal["pyarrow"],
) -> DataFrame: ...

class JsonReader(abc.Iterator, Generic[NDFrameT]):
class JsonReader(abc.Iterator[Any], Generic[NDFrameT]):
Copy link
Member

Choose a reason for hiding this comment

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

Can you collapse abc.Iterator and from collections.abc import Mapping

def read(self) -> NDFrameT: ...
def close(self) -> None: ...
def __iter__(self) -> JsonReader[NDFrameT]: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/io/parsers/readers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def read_fwf(
**kwds: Any,
) -> DataFrame: ...

class TextFileReader(abc.Iterator):
class TextFileReader(abc.Iterator[Any]):
Copy link
Member

Choose a reason for hiding this comment

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

Same here if possible collapse abc.Iterator into from collections.abc since both are imported

engine: CSVEngine
orig_options: Mapping[str, Any]
chunksize: int | None
Expand Down
6 changes: 3 additions & 3 deletions pandas-stubs/io/sql.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ from pandas._typing import (
DtypeBackend,
Scalar,
SequenceNotStr,
npt,
np_ndarray,
)

_SQLConnection: TypeAlias = str | sqlalchemy.engine.Connectable | sqlite3.Connection
Expand Down Expand Up @@ -155,7 +155,7 @@ class PandasSQL:
dtype: DtypeArg | None = None,
method: (
Literal["multi"]
| Callable[[SQLTable, Any, list[str], Iterable], int | None]
| Callable[[SQLTable, Any, list[str], Iterable[Any]], int | None]
| None
) = None,
engine: str = "auto",
Expand Down Expand Up @@ -189,7 +189,7 @@ class SQLTable:
def exists(self) -> bool: ...
def sql_schema(self) -> str: ...
def create(self) -> None: ...
def insert_data(self) -> tuple[list[str], list[npt.NDArray]]: ...
def insert_data(self) -> tuple[list[str], list[np_ndarray]]: ...
def insert(
self, chunksize: int | None = ..., method: str | None = ...
) -> int | None: ...
Expand Down
3 changes: 2 additions & 1 deletion pandas-stubs/io/stata.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import datetime
from io import BytesIO
from types import TracebackType
from typing import (
Any,
Literal,
overload,
)
Expand Down Expand Up @@ -76,7 +77,7 @@ def read_stata(
class StataParser:
def __init__(self) -> None: ...

class StataReader(StataParser, abc.Iterator):
class StataReader(StataParser, abc.Iterator[Any]):
col_sizes: list[int] = ...
path_or_buf: BytesIO = ...
def __init__(
Expand Down
3 changes: 2 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
from typing import (
TYPE_CHECKING,
Any,
Final,
Literal,
get_args,
Expand Down Expand Up @@ -160,7 +161,7 @@ def pytest_warns_bounded(
upper: str | None = None,
version_str: str | None = None,
upper_exception: type[Exception] | None = None,
) -> AbstractContextManager:
) -> AbstractContextManager[Any]:
"""
Version conditional pytest.warns context manager
Expand Down
4 changes: 3 additions & 1 deletion tests/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3051,7 +3051,9 @@ def test_to_dict_simple() -> None:

if TYPE_CHECKING_INVALID_USAGE:

def test(mapping: Mapping) -> None: # pyright: ignore[reportUnusedFunction]
def test( # pyright: ignore[reportUnusedFunction]
mapping: Mapping[Any, Any],
) -> None:
data.to_dict(into=mapping) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue]

def _1() -> None: # pyright: ignore[reportUnusedFunction]
Expand Down