@@ -14,18 +14,17 @@ from os import PathLike
1414from re import Pattern
1515import sys
1616from typing import (
17+ TYPE_CHECKING ,
1718 Any ,
1819 Generic ,
1920 Literal ,
2021 Protocol ,
2122 SupportsIndex ,
2223 TypeAlias ,
2324 TypedDict ,
24- Union ,
2525 overload ,
2626)
2727
28- from _typeshed import _T_contra
2928import numpy as np
3029from numpy import typing as npt
3130import pandas as pd
@@ -87,13 +86,15 @@ HashableT5 = TypeVar("HashableT5", bound=Hashable)
8786
8887ArrayLike : TypeAlias = ExtensionArray | npt .NDArray [Any ]
8988AnyArrayLike : TypeAlias = ArrayLike | Index | Series
90- AnyArrayLikeInt : TypeAlias = (
91- IntegerArray | Index [int ] | Series [int ] | npt .NDArray [np .integer ]
92- )
89+ if TYPE_CHECKING : # noqa: PYI002
90+ AnyArrayLikeInt : TypeAlias = (
91+ IntegerArray | Index [int ] | Series [int ] | npt .NDArray [np .integer ]
92+ )
9393
9494# list-like
9595
9696_T_co = TypeVar ("_T_co" , covariant = True )
97+ _T_contra = TypeVar ("_T_contra" , contravariant = True )
9798
9899class SequenceNotStr (Protocol [_T_co ]):
99100 @overload
@@ -594,18 +595,25 @@ IndexKeyFunc: TypeAlias = Callable[[Index], Index | AnyArrayLike] | None
594595
595596# types of `func` kwarg for DataFrame.aggregate and Series.aggregate
596597# More specific than what is in pandas
597- # following Union is here to make it ty compliant https://github.com/astral-sh/ty/issues/591
598- AggFuncTypeBase : TypeAlias = Union [Callable , str , np .ufunc ] # noqa: UP007
599- AggFuncTypeDictSeries : TypeAlias = Mapping [HashableT , AggFuncTypeBase ]
598+ AggFuncTypeBase : TypeAlias = Callable [P , Any ] | str | np .ufunc
599+ AggFuncTypeDictSeries : TypeAlias = Mapping [HashableT , AggFuncTypeBase [P ]]
600600AggFuncTypeDictFrame : TypeAlias = Mapping [
601- HashableT , AggFuncTypeBase | list [AggFuncTypeBase ]
601+ HashableT , AggFuncTypeBase [ P ] | Sequence [AggFuncTypeBase [ P ] ]
602602]
603- AggFuncTypeSeriesToFrame : TypeAlias = list [AggFuncTypeBase ] | AggFuncTypeDictSeries
603+ AggFuncTypeSeriesToFrame : TypeAlias = (
604+ Sequence [AggFuncTypeBase [P ]] | AggFuncTypeDictSeries [HashableT , P ]
605+ )
604606AggFuncTypeFrame : TypeAlias = (
605- AggFuncTypeBase | list [AggFuncTypeBase ] | AggFuncTypeDictFrame
607+ AggFuncTypeBase [P ]
608+ | Sequence [AggFuncTypeBase [P ]]
609+ | AggFuncTypeDictFrame [HashableT , P ]
610+ )
611+ AggFuncTypeDict : TypeAlias = (
612+ AggFuncTypeDictSeries [HashableT , P ] | AggFuncTypeDictFrame [HashableT , P ]
613+ )
614+ AggFuncType : TypeAlias = (
615+ AggFuncTypeBase [P ] | Sequence [AggFuncTypeBase [P ]] | AggFuncTypeDict [HashableT , P ]
606616)
607- AggFuncTypeDict : TypeAlias = AggFuncTypeDictSeries | AggFuncTypeDictFrame
608- AggFuncType : TypeAlias = AggFuncTypeBase | list [AggFuncTypeBase ] | AggFuncTypeDict
609617
610618# Not used in stubs
611619# AggObjType = Union[
@@ -692,7 +700,9 @@ CompressionOptions: TypeAlias = (
692700
693701# types in DataFrameFormatter
694702FormattersType : TypeAlias = (
695- list [Callable ] | tuple [Callable , ...] | Mapping [str | int , Callable ]
703+ list [Callable [..., Any ]]
704+ | tuple [Callable [..., Any ], ...]
705+ | Mapping [str | int , Callable [..., Any ]]
696706)
697707# ColspaceType = Mapping[Hashable, Union[str, int]] not used in stubs
698708FloatFormatType : TypeAlias = str | Callable [[float ], str ] | EngFormatter
@@ -825,7 +835,7 @@ TimeNonexistent: TypeAlias = (
825835DropKeep : TypeAlias = Literal ["first" , "last" , False ]
826836CorrelationMethod : TypeAlias = (
827837 Literal ["pearson" , "kendall" , "spearman" ]
828- | Callable [[np . typing . NDArray [Any ], np . typing .NDArray [Any ]], float ]
838+ | Callable [[npt . NDArray [Any ], npt .NDArray [Any ]], float ]
829839)
830840AlignJoin : TypeAlias = Literal ["outer" , "inner" , "left" , "right" ]
831841DtypeBackend : TypeAlias = Literal ["pyarrow" , "numpy_nullable" ]
@@ -948,6 +958,8 @@ np_1darray_dt: TypeAlias = np_1darray[np.datetime64]
948958np_1darray_td : TypeAlias = np_1darray [np .timedelta64 ]
949959np_2darray : TypeAlias = np .ndarray [tuple [int , int ], np .dtype [GenericT ]]
950960
961+ NDArrayT = TypeVar ("NDArrayT" , bound = np .ndarray )
962+
951963DtypeNp = TypeVar ("DtypeNp" , bound = np .dtype [np .generic ])
952964KeysArgType : TypeAlias = Any
953965ListLikeT = TypeVar ("ListLikeT" , bound = ListLike )
@@ -966,8 +978,9 @@ class SupportsDType(Protocol[GenericT_co]):
966978# Similar to npt.DTypeLike but leaves out np.dtype and None for use in overloads
967979DTypeLike : TypeAlias = type [Any ] | tuple [Any , Any ] | list [Any ] | str
968980
969- IndexType : TypeAlias = slice | np_ndarray_anyint | Index | list [int ] | Series [int ]
970- MaskType : TypeAlias = Series [bool ] | np_ndarray_bool | list [bool ]
981+ if TYPE_CHECKING : # noqa: PYI002
982+ IndexType : TypeAlias = slice | np_ndarray_anyint | Index | list [int ] | Series [int ]
983+ MaskType : TypeAlias = Series [bool ] | np_ndarray_bool | list [bool ]
971984
972985# Scratch types for generics
973986
@@ -1039,48 +1052,49 @@ Function: TypeAlias = np.ufunc | Callable[..., Any]
10391052# shared HashableT and HashableT#. This one can be used if the identical
10401053# type is need in a function that uses GroupByObjectNonScalar
10411054_HashableTa = TypeVar ("_HashableTa" , bound = Hashable )
1042- ByT = TypeVar (
1043- "ByT" ,
1044- bound = str
1045- | bytes
1046- | datetime .date
1047- | datetime .datetime
1048- | datetime .timedelta
1049- | np .datetime64
1050- | np .timedelta64
1051- | bool
1052- | int
1053- | float
1054- | complex
1055- | Scalar
1056- | Period
1057- | Interval [int | float | Timestamp | Timedelta ]
1058- | tuple ,
1059- )
1060- # Use a distinct SeriesByT when using groupby with Series of known dtype.
1061- # Essentially, an intersection between Series S1 TypeVar, and ByT TypeVar
1062- SeriesByT = TypeVar (
1063- "SeriesByT" ,
1064- bound = str
1065- | bytes
1066- | datetime .date
1067- | bool
1068- | int
1069- | float
1070- | complex
1071- | datetime .datetime
1072- | datetime .timedelta
1073- | Period
1074- | Interval [int | float | Timestamp | Timedelta ],
1075- )
1055+ if TYPE_CHECKING : # noqa: PYI002
1056+ ByT = TypeVar (
1057+ "ByT" ,
1058+ bound = str
1059+ | bytes
1060+ | datetime .date
1061+ | datetime .datetime
1062+ | datetime .timedelta
1063+ | np .datetime64
1064+ | np .timedelta64
1065+ | bool
1066+ | int
1067+ | float
1068+ | complex
1069+ | Scalar
1070+ | Period
1071+ | Interval [int | float | Timestamp | Timedelta ]
1072+ | tuple ,
1073+ )
1074+ # Use a distinct SeriesByT when using groupby with Series of known dtype.
1075+ # Essentially, an intersection between Series S1 TypeVar, and ByT TypeVar
1076+ SeriesByT = TypeVar (
1077+ "SeriesByT" ,
1078+ bound = str
1079+ | bytes
1080+ | datetime .date
1081+ | bool
1082+ | int
1083+ | float
1084+ | complex
1085+ | datetime .datetime
1086+ | datetime .timedelta
1087+ | Period
1088+ | Interval [int | float | Timestamp | Timedelta ],
1089+ )
10761090GroupByObjectNonScalar : TypeAlias = (
1077- tuple
1091+ tuple [ _HashableTa , ...]
10781092 | list [_HashableTa ]
10791093 | Function
10801094 | list [Function ]
10811095 | list [Series ]
1082- | np . ndarray
1083- | list [np . ndarray ]
1096+ | np_ndarray
1097+ | list [np_ndarray ]
10841098 | Mapping [Label , Any ]
10851099 | list [Mapping [Label , Any ]]
10861100 | list [Index ]
0 commit comments