diff --git a/pandas-stubs/_libs/interval.pyi b/pandas-stubs/_libs/interval.pyi index b749329ef..4b5613fe5 100644 --- a/pandas-stubs/_libs/interval.pyi +++ b/pandas-stubs/_libs/interval.pyi @@ -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: diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 343e8831e..5796aeb5b 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -76,6 +76,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) @@ -774,7 +775,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"] @@ -872,7 +873,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 @@ -918,6 +923,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] @@ -959,7 +965,10 @@ np_1darray_dt: TypeAlias = np_1darray[np.datetime64] np_1darray_td: TypeAlias = np_1darray[np.timedelta64] np_2darray: TypeAlias = np.ndarray[tuple[int, int], np.dtype[GenericT]] -NDArrayT = TypeVar("NDArrayT", bound=np.ndarray) +if sys.version_info >= (3, 11): + NDArrayT = TypeVar("NDArrayT", bound=np.ndarray) +else: + NDArrayT = TypeVar("NDArrayT", bound=np.ndarray[Any, Any]) DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic]) KeysArgType: TypeAlias = Any @@ -969,7 +978,7 @@ ListLikeExceptSeriesAndStr: TypeAlias = ( ) ListLikeU: TypeAlias = Sequence[Any] | np_1darray | Series | Index ListLikeHashable: TypeAlias = ( - MutableSequence[HashableT] | np_1darray | tuple[HashableT, ...] | range + MutableSequence[HashableT0] | np_1darray | tuple[HashableT0, ...] | range ) class SupportsDType(Protocol[GenericT_co]): @@ -1010,8 +1019,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( @@ -1045,14 +1055,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", @@ -1070,7 +1080,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 @@ -1088,21 +1098,23 @@ if TYPE_CHECKING: # noqa: PYI002 | Period | Interval[int | float | Timestamp | Timedelta], ) -GroupByObjectNonScalar: TypeAlias = ( - tuple[_HashableTa, ...] - | list[_HashableTa] - | Function - | list[Function] - | list[Series] - | np_ndarray - | list[np_ndarray] - | Mapping[Label, Any] - | list[Mapping[Label, Any]] - | list[Index] - | Grouper - | list[Grouper] -) -GroupByObject: TypeAlias = Scalar | Index | GroupByObjectNonScalar | Series + GroupByObjectNonScalar: TypeAlias = ( + tuple[_HashableTa, ...] + | list[_HashableTa] + | Function + | list[Function] + | list[Series] + | np_ndarray + | list[np_ndarray] + | Mapping[Label, Any] + | list[Mapping[Label, Any]] + | list[Index] + | Grouper + | list[Grouper] + ) + GroupByObject: TypeAlias = ( + Scalar | Index | GroupByObjectNonScalar[_HashableTa] | Series + ) StataDateFormat: TypeAlias = Literal[ "tc", @@ -1125,10 +1137,10 @@ StataDateFormat: TypeAlias = Literal[ # `DataFrame.replace` also accepts mappings of these. ReplaceValue: TypeAlias = ( Scalar - | Pattern + | Pattern[Any] | NAType - | Sequence[Scalar | Pattern] - | Mapping[HashableT, ScalarT] + | Sequence[Scalar | Pattern[Any]] + | Mapping[HashableT0, ScalarT0] | Series | None ) diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 742138fee..96ef999f7 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -58,7 +58,7 @@ from pandas.core.indexing import ( _LocIndexer, ) from pandas.core.reshape.pivot import ( - _PivotAggFunc, + _PivotAggFuncTypes, _PivotTableColumnsTypes, _PivotTableIndexTypes, _PivotTableValuesTypes, @@ -178,7 +178,7 @@ from pandas.plotting import PlotAccessor from pandas.plotting._core import _BoxPlotT _T_MUTABLE_MAPPING_co = TypeVar( - "_T_MUTABLE_MAPPING_co", bound=MutableMapping, covariant=True + "_T_MUTABLE_MAPPING_co", bound=MutableMapping[Any, Any], covariant=True ) class _iLocIndexerFrame(_iLocIndexer, Generic[_T]): @@ -340,28 +340,23 @@ class _AtIndexerFrame(_AtIndexer): value: Scalar | NAType | NaTType | None, ) -> None: ... -# With mypy 1.14.1 and python 3.12, the second overload needs a type-ignore statement -if sys.version_info >= (3, 12): - class _GetItemHack: - @overload - def __getitem__(self, key: Scalar | tuple[Hashable, ...]) -> Series: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload] +class _GetItemHack: + @overload + def __getitem__(self, key: Scalar | tuple[Hashable, ...]) -> Series: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload] + # With mypy 1.14.1 and python 3.12, the second overload needs a type-ignore statement + if sys.version_info >= (3, 12): @overload def __getitem__( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload] self, key: Iterable[Hashable] | slice ) -> Self: ... - @overload - def __getitem__(self, key: Hashable) -> Series: ... - -else: - class _GetItemHack: - @overload - def __getitem__(self, key: Scalar | tuple[Hashable, ...]) -> Series: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload] + else: @overload def __getitem__( # pyright: ignore[reportOverlappingOverload] self, key: Iterable[Hashable] | slice ) -> Self: ... - @overload - def __getitem__(self, key: Hashable) -> Series: ... + + @overload + def __getitem__(self, key: Hashable) -> Series: ... _AstypeArgExt: TypeAlias = ( AstypeArg @@ -562,16 +557,29 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): coerce_float: bool = False, nrows: int | None = None, ) -> Self: ... - def to_records( - self, - index: _bool = True, - column_dtypes: ( - _str | npt.DTypeLike | Mapping[HashableT1, npt.DTypeLike] | None - ) = None, - index_dtypes: ( - _str | npt.DTypeLike | Mapping[HashableT2, npt.DTypeLike] | None - ) = None, - ) -> np.recarray: ... + if sys.version_info >= (3, 11): + def to_records( + self, + index: _bool = True, + column_dtypes: ( + _str | npt.DTypeLike | Mapping[HashableT1, npt.DTypeLike] | None + ) = None, + index_dtypes: ( + _str | npt.DTypeLike | Mapping[HashableT2, npt.DTypeLike] | None + ) = None, + ) -> np.recarray: ... + else: + def to_records( + self, + index: _bool = True, + column_dtypes: ( + _str | npt.DTypeLike | Mapping[HashableT1, npt.DTypeLike] | None + ) = None, + index_dtypes: ( + _str | npt.DTypeLike | Mapping[HashableT2, npt.DTypeLike] | None + ) = None, + ) -> np.recarray[Any, Any]: ... + @overload def to_stata( self, @@ -1360,7 +1368,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): dropna: _bool = ..., ) -> DataFrameGroupBy[Period, Literal[False]]: ... @overload - def groupby( # pyright: ignore reportOverlappingOverload + def groupby( self, by: IntervalIndex[IntervalT], level: IndexLabel | None = ..., @@ -1373,7 +1381,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): @overload def groupby( self, - by: IntervalIndex[IntervalT], + by: IntervalIndex, level: IndexLabel | None = ..., as_index: Literal[False] = False, sort: _bool = ..., @@ -1459,9 +1467,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): values: _PivotTableValuesTypes = None, index: _PivotTableIndexTypes = None, columns: _PivotTableColumnsTypes = None, - aggfunc: ( - _PivotAggFunc | Sequence[_PivotAggFunc] | Mapping[Hashable, _PivotAggFunc] - ) = "mean", + aggfunc: _PivotAggFuncTypes[Scalar] = "mean", fill_value: Scalar | None = None, margins: _bool = False, dropna: _bool = True, @@ -2842,8 +2848,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: ... diff --git a/pandas-stubs/core/indexes/accessors.pyi b/pandas-stubs/core/indexes/accessors.pyi index 61d24d45c..7b46576d8 100644 --- a/pandas-stubs/core/indexes/accessors.pyi +++ b/pandas-stubs/core/indexes/accessors.pyi @@ -5,6 +5,7 @@ from datetime import ( tzinfo as _tzinfo, ) from typing import ( + Any, Generic, Literal, TypeVar, @@ -377,7 +378,7 @@ class PeriodProperties( Series[Timestamp], Series[int], Series[str], DatetimeArray, PeriodArray ], _DatetimeFieldOps[Series[int]], - _IsLeapYearProperty, + _IsLeapYearProperty[_DTBoolOpsReturnType], _FreqProperty[BaseOffset], ): ... class CombinedDatetimelikeProperties( @@ -393,7 +394,9 @@ class CombinedDatetimelikeProperties( Series[Period], ], _TimedeltaPropertiesNoRounding[Series[int], Series[float]], - _PeriodProperties, + _PeriodProperties[ + Series[Timestamp], Series[int], Series[str], DatetimeArray, PeriodArray + ], ): ... @type_check_only @@ -458,7 +461,7 @@ class DtDescriptor: @overload def __get__( self, instance: Series[Period], owner: type[Series] - ) -> PeriodProperties: ... + ) -> PeriodProperties[Any]: ... @type_check_only class ArrayDescriptor: diff --git a/pandas-stubs/core/indexes/interval.pyi b/pandas-stubs/core/indexes/interval.pyi index c968845c8..f99811697 100644 --- a/pandas-stubs/core/indexes/interval.pyi +++ b/pandas-stubs/core/indexes/interval.pyi @@ -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] + self, dtype: DtypeArg, copy: bool = True + ) -> IntervalIndex: ... @property def inferred_type(self) -> str: ... def memory_usage(self, deep: bool = False) -> int: ... diff --git a/pandas-stubs/core/indexes/period.pyi b/pandas-stubs/core/indexes/period.pyi index 5cb27ebb1..0247dda31 100644 --- a/pandas-stubs/core/indexes/period.pyi +++ b/pandas-stubs/core/indexes/period.pyi @@ -1,7 +1,6 @@ from collections.abc import Hashable import datetime from typing import ( - Any, overload, ) @@ -30,7 +29,7 @@ from pandas._typing import ( class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexFieldOps): def __new__( cls, - data: AxesData[Any] | None = None, + data: AxesData | None = None, freq: Frequency | None = None, dtype: Dtype | None = None, copy: bool = False, diff --git a/pandas-stubs/core/reshape/pivot.pyi b/pandas-stubs/core/reshape/pivot.pyi index 945e912e6..75ed1d038 100644 --- a/pandas-stubs/core/reshape/pivot.pyi +++ b/pandas-stubs/core/reshape/pivot.pyi @@ -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( @@ -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, *, @@ -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, diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 7778931f6..42b254fe4 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -446,7 +446,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame): IntervalIndex[Interval[_OrderableT]] | Interval[_OrderableT] | Sequence[Interval[_OrderableT]] - | dict[HashableT1, Interval[_OrderableT]] + | dict[Hashable, Interval[_OrderableT]] ), index: AxesData | None = None, dtype: Literal["Interval"] = ..., @@ -534,7 +534,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame): Scalar | _DataLike | Mapping[HashableT1, Any] - | BaseGroupBy + | BaseGroupBy[Any] | NaTType | NAType | None @@ -970,7 +970,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame): self: Series[BooleanDtype], periods: int = ... ) -> Series[BooleanDtype]: ... @overload - def diff(self: Series[Interval], periods: int = ...) -> Never: ... + def diff(self: Series[Interval[Any]], periods: int = ...) -> Never: ... @overload def diff( self: SupportsGetItem[Scalar, SupportsSelfSub[S2]], periods: int = ... @@ -1105,7 +1105,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame): def swaplevel( self, i: Level = -2, j: Level = -1, copy: _bool = True ) -> Series[S1]: ... - def reorder_levels(self, order: list[Any]) -> Series[S1]: ... + def reorder_levels(self, order: Sequence[int | np.integer]) -> Series[S1]: ... def explode(self, ignore_index: _bool = ...) -> Series[S1]: ... def unstack( self, diff --git a/pandas-stubs/io/html.pyi b/pandas-stubs/io/html.pyi index 091475b26..4a43aa45d 100644 --- a/pandas-stubs/io/html.pyi +++ b/pandas-stubs/io/html.pyi @@ -29,7 +29,7 @@ from pandas._typing import ( def read_html( io: FilePath | ReadBuffer[str], *, - match: str | Pattern = ..., + match: str | Pattern[str] = ..., flavor: HTMLFlavors | Sequence[HTMLFlavors] | None = ..., header: int | Sequence[int] | None = ..., index_col: int | Sequence[int] | list[HashableT1] | None = ..., diff --git a/pandas-stubs/io/json/_json.pyi b/pandas-stubs/io/json/_json.pyi index aabebb978..dc1f2ac0c 100644 --- a/pandas-stubs/io/json/_json.pyi +++ b/pandas-stubs/io/json/_json.pyi @@ -2,6 +2,7 @@ from collections import abc from collections.abc import Mapping from types import TracebackType from typing import ( + Any, Generic, Literal, overload, @@ -226,7 +227,7 @@ def read_json( engine: Literal["pyarrow"], ) -> DataFrame: ... -class JsonReader(abc.Iterator, Generic[NDFrameT]): +class JsonReader(abc.Iterator[Any], Generic[NDFrameT]): def read(self) -> NDFrameT: ... def close(self) -> None: ... def __iter__(self) -> JsonReader[NDFrameT]: ... diff --git a/pandas-stubs/io/parsers/readers.pyi b/pandas-stubs/io/parsers/readers.pyi index e414b4ca3..e655bc2e5 100644 --- a/pandas-stubs/io/parsers/readers.pyi +++ b/pandas-stubs/io/parsers/readers.pyi @@ -232,7 +232,7 @@ def read_table( header: int | Sequence[int] | Literal["infer"] | None = "infer", names: ListLikeHashable | None = ..., index_col: int | str | Sequence[str | int] | Literal[False] | None = None, - usecols: UsecolsArgType[HashableT] = None, + usecols: UsecolsArgType = None, dtype: DtypeArg | defaultdict[Any, Any] | None = None, engine: CSVEngine | None = None, converters: ( @@ -295,7 +295,7 @@ def read_table( header: int | Sequence[int] | Literal["infer"] | None = "infer", names: ListLikeHashable | None = ..., index_col: int | str | Sequence[str | int] | Literal[False] | None = None, - usecols: UsecolsArgType[HashableT] = None, + usecols: UsecolsArgType = None, dtype: DtypeArg | defaultdict[Any, Any] | None = None, engine: CSVEngine | None = None, converters: ( @@ -358,7 +358,7 @@ def read_table( header: int | Sequence[int] | Literal["infer"] | None = "infer", names: ListLikeHashable | None = ..., index_col: int | str | Sequence[str | int] | Literal[False] | None = None, - usecols: UsecolsArgType[HashableT] = None, + usecols: UsecolsArgType = None, dtype: DtypeArg | defaultdict[Any, Any] | None = None, engine: CSVEngine | None = None, converters: ( @@ -452,7 +452,7 @@ def read_fwf( **kwds: Any, ) -> DataFrame: ... -class TextFileReader(abc.Iterator): +class TextFileReader(abc.Iterator[Any]): engine: CSVEngine orig_options: Mapping[str, Any] chunksize: int | None diff --git a/pandas-stubs/io/sql.pyi b/pandas-stubs/io/sql.pyi index 4d92623de..c3f7a4333 100644 --- a/pandas-stubs/io/sql.pyi +++ b/pandas-stubs/io/sql.pyi @@ -33,8 +33,8 @@ _SQLStatement: TypeAlias = ( str | sqlalchemy.sql.expression.Selectable | sqlalchemy.sql.expression.TextClause - | sqlalchemy.sql.Select - | FromStatement + | sqlalchemy.sql.Select[Any] + | FromStatement[Any] | sqlalchemy.sql.expression.UpdateBase ) @@ -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", @@ -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[npt.NDArray[Any]]]: ... def insert( self, chunksize: int | None = ..., method: str | None = ... ) -> int | None: ... diff --git a/pandas-stubs/io/stata.pyi b/pandas-stubs/io/stata.pyi index ce50b8083..91f47b858 100644 --- a/pandas-stubs/io/stata.pyi +++ b/pandas-stubs/io/stata.pyi @@ -4,6 +4,7 @@ import datetime from io import BytesIO from types import TracebackType from typing import ( + Any, Literal, overload, ) @@ -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__( diff --git a/pandas-stubs/io/xml.pyi b/pandas-stubs/io/xml.pyi index d76408e75..c11f20cfc 100644 --- a/pandas-stubs/io/xml.pyi +++ b/pandas-stubs/io/xml.pyi @@ -1,4 +1,5 @@ from collections.abc import Sequence +from typing import Any from pandas.core.frame import DataFrame @@ -25,7 +26,7 @@ def read_xml( names: Sequence[str] | None = ..., dtype: DtypeArg | None = ..., converters: ConvertersArg | None = ..., - parse_dates: ParseDatesArg | None = ..., + parse_dates: ParseDatesArg[Any, Any] | None = ..., # encoding can not be None for lxml and StringIO input encoding: str | None = ..., parser: XMLParsers = ..., diff --git a/pandas-stubs/plotting/_core.pyi b/pandas-stubs/plotting/_core.pyi index 527224442..0af3c89fb 100644 --- a/pandas-stubs/plotting/_core.pyi +++ b/pandas-stubs/plotting/_core.pyi @@ -39,7 +39,7 @@ class _BoxPlotT(NamedTuple): _SingleColor: TypeAlias = ( str | list[float] | tuple[float, float, float] | tuple[float, float, float, float] ) -_PlotAccessorColor: TypeAlias = str | list[_SingleColor] | dict[HashableT, _SingleColor] +_PlotAccessorColor: TypeAlias = str | list[_SingleColor] | dict[Any, _SingleColor] # Keep in sync with `DataFrame.boxplot` @overload diff --git a/pyproject.toml b/pyproject.toml index 0663fc85d..4d013324c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -310,7 +310,6 @@ include = ["tests", "pandas-stubs"] enableTypeIgnoreComments = false # use pyright-specific ignores # disable subset of strict reportMissingParameterType = false -reportMissingTypeArgument = false reportUnnecessaryTypeIgnoreComment = true reportUnknownArgumentType = false reportUnknownLambdaType = false diff --git a/tests/__init__.py b/tests/__init__.py index f11a46a34..1ac265c2b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -8,6 +8,7 @@ import sys from typing import ( TYPE_CHECKING, + Any, Final, Literal, get_args, @@ -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 diff --git a/tests/frame/test_frame.py b/tests/frame/test_frame.py index d895b9981..c72148337 100644 --- a/tests/frame/test_frame.py +++ b/tests/frame/test_frame.py @@ -2998,19 +2998,42 @@ def test_to_xarray() -> None: def test_to_records() -> None: df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]}) - check(assert_type(df.to_records(False, "int8"), np.recarray), np.recarray) - check( - assert_type(df.to_records(False, index_dtypes=np.int8), np.recarray), - np.recarray, - ) - check( - assert_type( - df.to_records(False, {"col1": np.int8, "col2": np.int16}), np.recarray - ), - np.recarray, - ) dtypes = {"col1": np.int8, "col2": np.int16} - check(assert_type(df.to_records(False, dtypes), np.recarray), np.recarray) + if sys.version_info >= (3, 11): + check(assert_type(df.to_records(False, "int8"), np.recarray), np.recarray) + check( + assert_type(df.to_records(False, index_dtypes=np.int8), np.recarray), + np.recarray, + ) + check( + assert_type( + df.to_records(False, {"col1": np.int8, "col2": np.int16}), np.recarray + ), + np.recarray, + ) + check(assert_type(df.to_records(False, dtypes), np.recarray), np.recarray) + else: + check( + assert_type(df.to_records(False, "int8"), np.recarray[Any, Any]), + np.recarray, + ) + check( + assert_type( + df.to_records(False, index_dtypes=np.int8), np.recarray[Any, Any] + ), + np.recarray, + ) + check( + assert_type( + df.to_records(False, {"col1": np.int8, "col2": np.int16}), + np.recarray[Any, Any], + ), + np.recarray, + ) + check( + assert_type(df.to_records(False, dtypes), np.recarray[Any, Any]), + np.recarray, + ) def test_to_dict_simple() -> None: @@ -3028,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] diff --git a/tests/frame/test_groupby.py b/tests/frame/test_groupby.py index 20c2f013f..2f39e870f 100644 --- a/tests/frame/test_groupby.py +++ b/tests/frame/test_groupby.py @@ -531,7 +531,7 @@ def sum_mean(x: pd.DataFrame) -> float: ): check(assert_type(df.groupby("col1").apply(lfunc), pd.Series), pd.Series) - def sum_to_list(x: pd.DataFrame) -> list: + def sum_to_list(x: pd.DataFrame) -> list[Any]: return x.sum().tolist() with pytest_warns_bounded( diff --git a/tests/indexes/test_indexes.py b/tests/indexes/test_indexes.py index 2db0c37c7..601e803d2 100644 --- a/tests/indexes/test_indexes.py +++ b/tests/indexes/test_indexes.py @@ -1719,9 +1719,15 @@ def test_index_view() -> None: # - pyright: ndarray[tuple[Any, ...], dtype[Any]] check(assert_type(ind.view(np.ndarray), np.ndarray), np.ndarray) # type: ignore[assert-type] else: - check(assert_type(ind.view(np.ndarray), np.ndarray), np.ndarray) + check(assert_type(ind.view(np.ndarray), np.ndarray[Any, Any]), np.ndarray) - class MyArray(np.ndarray): ... + if sys.version_info >= (3, 11): + + class MyArray(np.ndarray): ... + + else: + + class MyArray(np.ndarray[Any, Any]): ... check(assert_type(ind.view(MyArray), MyArray), MyArray) diff --git a/tests/series/test_properties.py b/tests/series/test_properties.py index 844336ab9..efa7fe7d5 100644 --- a/tests/series/test_properties.py +++ b/tests/series/test_properties.py @@ -1,3 +1,4 @@ +# pyright: reportMissingTypeArgument=false from typing import TYPE_CHECKING import numpy as np diff --git a/tests/test_interval.py b/tests/test_interval.py index 398bda276..0dc0c3f90 100644 --- a/tests/test_interval.py +++ b/tests/test_interval.py @@ -2,6 +2,7 @@ import numpy as np import pandas as pd +from pandas.core.arrays.interval import IntervalArray from typing_extensions import assert_type from tests import ( @@ -125,6 +126,6 @@ def test_interval_array_contains() -> None: df = pd.DataFrame({"A": range(1, 10)}) obj = pd.Interval(1, 4) ser = pd.Series(obj, index=df.index) - arr = ser.array + arr = check(assert_type(ser.array, IntervalArray), IntervalArray) check(assert_type(arr.contains(df["A"]), "pd.Series[bool]"), pd.Series, np.bool_) check(assert_type(arr.contains(3), np_1darray_bool), np_1darray_bool) diff --git a/tests/test_resampler.py b/tests/test_resampler.py index f9da71ebe..ee142ef74 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -3,20 +3,18 @@ Hashable, Iterator, ) +from typing import Any # noqa: F401 from typing import TypeAlias import numpy as np -import pandas as pd -from pandas import ( - DataFrame, - Series, - date_range, -) +from pandas.core.frame import DataFrame from pandas.core.groupby.generic import ( DataFrameGroupBy, SeriesGroupBy, ) +from pandas.core.indexes.datetimes import date_range from pandas.core.resample import DatetimeIndexResampler +from pandas.core.series import Series from typing_extensions import assert_type from tests import ( @@ -142,7 +140,7 @@ def test_asfreq() -> None: def test_getattr() -> None: - check(assert_type(DF.resample("ME").col1, SeriesGroupBy), SeriesGroupBy) + check(assert_type(DF.resample("ME").col1, "SeriesGroupBy[Any, Any]"), SeriesGroupBy) def test_interpolate() -> None: @@ -404,7 +402,7 @@ def f(val: Series) -> Series: def test_aggregate_series_combinations() -> None: def s2series(val: Series) -> Series: - return pd.Series(val) + return Series(val) def s2scalar(val: Series) -> float: return float(val.mean()) @@ -426,7 +424,7 @@ def s2scalar(val: Series) -> float: def test_aggregate_frame_combinations() -> None: def df2frame(val: DataFrame) -> DataFrame: - return pd.DataFrame(val) + return DataFrame(val) def df2series(val: DataFrame) -> Series: return val.mean() @@ -467,8 +465,10 @@ def df2scalar(val: DataFrame) -> float: def test_getitem() -> None: - check(assert_type(DF.resample("ME")["col1"], SeriesGroupBy), SeriesGroupBy) check( - assert_type(DF.resample("ME")[["col1", "col2"]], DataFrameGroupBy), + assert_type(DF.resample("ME")["col1"], "SeriesGroupBy[Any, Any]"), SeriesGroupBy + ) + check( + assert_type(DF.resample("ME")[["col1", "col2"]], "DataFrameGroupBy[Any, Any]"), DataFrameGroupBy, )