Skip to content

Commit 5fa39f3

Browse files
authored
TYP: naming and default values (#1578)
* feat: naming and default values * a few more any's * review
1 parent 4a796fd commit 5fa39f3

File tree

14 files changed

+71
-40
lines changed

14 files changed

+71
-40
lines changed

pandas-stubs/_libs/interval.pyi

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ from pandas import (
1414
Timedelta,
1515
Timestamp,
1616
)
17+
from typing_extensions import Self
1718

1819
from pandas._typing import (
1920
IntervalClosedType,
@@ -26,7 +27,9 @@ VALID_CLOSED: frozenset[str]
2627

2728
_OrderableScalarT = TypeVar("_OrderableScalarT", bound=int | float)
2829
_OrderableTimesT = TypeVar("_OrderableTimesT", bound=Timestamp | Timedelta)
29-
_OrderableT = TypeVar("_OrderableT", bound=int | float | Timestamp | Timedelta)
30+
_OrderableT = TypeVar(
31+
"_OrderableT", bound=int | float | Timestamp | Timedelta, default=Any
32+
)
3033

3134
@type_check_only
3235
class _LengthDescriptor:
@@ -73,12 +76,12 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
7376
def closed(self) -> IntervalClosedType: ...
7477
mid = _MidDescriptor()
7578
length = _LengthDescriptor()
76-
def __init__(
77-
self,
79+
def __new__(
80+
cls,
7881
left: _OrderableT,
7982
right: _OrderableT,
8083
closed: IntervalClosedType = ...,
81-
) -> None: ...
84+
) -> Self: ...
8285
def __hash__(self) -> int: ...
8386
@overload
8487
def __contains__(self: Interval[int], key: float | np.floating) -> bool: ...

pandas-stubs/_typing.pyi

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ from pandas.tseries.offsets import (
7777
P = ParamSpec("P")
7878

7979
HashableT = TypeVar("HashableT", bound=Hashable)
80+
HashableT0 = TypeVar("HashableT0", bound=Hashable, default=Any)
8081
HashableT1 = TypeVar("HashableT1", bound=Hashable)
8182
HashableT2 = TypeVar("HashableT2", bound=Hashable)
8283
HashableT3 = TypeVar("HashableT3", bound=Hashable)
@@ -776,7 +777,7 @@ XMLParsers: TypeAlias = Literal["lxml", "etree"]
776777
HTMLFlavors: TypeAlias = Literal["lxml", "html5lib", "bs4"]
777778

778779
# Interval closed type
779-
IntervalT = TypeVar("IntervalT", bound=Interval)
780+
IntervalT = TypeVar("IntervalT", bound=Interval, default=Interval)
780781
IntervalLeftRight: TypeAlias = Literal["left", "right"]
781782
IntervalClosedType: TypeAlias = IntervalLeftRight | Literal["both", "neither"]
782783

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

875876
# read_csv: usecols
876877
UsecolsArgType: TypeAlias = (
877-
SequenceNotStr[Hashable] | range | AnyArrayLike | Callable[[HashableT], bool] | None
878+
SequenceNotStr[Hashable]
879+
| range
880+
| AnyArrayLike
881+
| Callable[[HashableT0], bool]
882+
| None
878883
)
879884

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

922927
ScalarT = TypeVar("ScalarT", bound=Scalar)
928+
ScalarT0 = TypeVar("ScalarT0", bound=Scalar, default=Scalar)
923929
# Refine the definitions below in 3.9 to use the specialized type.
924930
np_num: TypeAlias = np.bool | np.integer | np.floating | np.complexfloating
925931
np_ndarray_intp: TypeAlias = npt.NDArray[np.intp]
@@ -1015,8 +1021,9 @@ SeriesDType: TypeAlias = (
10151021
| datetime.datetime # includes pd.Timestamp
10161022
| datetime.timedelta # includes pd.Timedelta
10171023
)
1024+
S0 = TypeVar("S0", bound=SeriesDType, default=Any)
10181025
S1 = TypeVar("S1", bound=SeriesDType, default=Any)
1019-
# Like S1, but without `default=Any`.
1026+
# Like S0 and S1, but without `default=Any`.
10201027
S2 = TypeVar("S2", bound=SeriesDType)
10211028
S2_contra = TypeVar("S2_contra", bound=SeriesDType, contravariant=True)
10221029
S2_NDT_contra = TypeVar(
@@ -1050,14 +1057,14 @@ IndexingInt: TypeAlias = (
10501057
)
10511058

10521059
# AxesData is used for data for Index
1053-
AxesData: TypeAlias = Mapping[S3, Any] | Axes | KeysView[S3]
1060+
AxesData: TypeAlias = Mapping[S0, Any] | Axes | KeysView[S0]
10541061

10551062
# Any plain Python or numpy function
10561063
Function: TypeAlias = np.ufunc | Callable[..., Any]
10571064
# Use a distinct HashableT in shared types to avoid conflicts with
10581065
# shared HashableT and HashableT#. This one can be used if the identical
10591066
# type is need in a function that uses GroupByObjectNonScalar
1060-
_HashableTa = TypeVar("_HashableTa", bound=Hashable)
1067+
_HashableTa = TypeVar("_HashableTa", bound=Hashable, default=Any)
10611068
if TYPE_CHECKING: # noqa: PYI002
10621069
ByT = TypeVar(
10631070
"ByT",
@@ -1075,7 +1082,7 @@ if TYPE_CHECKING: # noqa: PYI002
10751082
| Scalar
10761083
| Period
10771084
| Interval[int | float | Timestamp | Timedelta]
1078-
| tuple,
1085+
| tuple[Any, ...],
10791086
)
10801087
# Use a distinct SeriesByT when using groupby with Series of known dtype.
10811088
# Essentially, an intersection between Series S1 TypeVar, and ByT TypeVar
@@ -1130,10 +1137,10 @@ StataDateFormat: TypeAlias = Literal[
11301137
# `DataFrame.replace` also accepts mappings of these.
11311138
ReplaceValue: TypeAlias = (
11321139
Scalar
1133-
| Pattern
1140+
| Pattern[str]
11341141
| NAType
1135-
| Sequence[Scalar | Pattern]
1136-
| Mapping[HashableT, ScalarT]
1142+
| Sequence[Scalar | Pattern[str]]
1143+
| Mapping[HashableT0, ScalarT0]
11371144
| Series
11381145
| None
11391146
)

pandas-stubs/core/frame.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,8 +2761,12 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
27612761
def __rfloordiv__(
27622762
self, other: float | DataFrame | Series[int] | Series[float] | Sequence[float]
27632763
) -> Self: ...
2764-
def __truediv__(self, other: float | DataFrame | Series | Sequence) -> Self: ...
2765-
def __rtruediv__(self, other: float | DataFrame | Series | Sequence) -> Self: ...
2764+
def __truediv__(
2765+
self, other: float | DataFrame | Series | Sequence[Any]
2766+
) -> Self: ...
2767+
def __rtruediv__(
2768+
self, other: float | DataFrame | Series | Sequence[Any]
2769+
) -> Self: ...
27662770
@final
27672771
def __bool__(self) -> NoReturn: ...
27682772

pandas-stubs/core/indexes/base.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
324324
copy: bool = ...,
325325
name: Hashable = ...,
326326
tupleize_cols: bool = ...,
327-
) -> IntervalIndex[Interval[Any]]: ...
327+
) -> IntervalIndex[Interval]: ...
328328
@overload
329329
def __new__(
330330
cls,

pandas-stubs/core/indexes/interval.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class IntervalIndex(ExtensionIndex[IntervalT, np.object_], IntervalMixin):
243243
def __contains__(self, key: IntervalT) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
244244
@overload
245245
def __contains__(self, key: object) -> Literal[False]: ...
246-
def astype(self, dtype: DtypeArg, copy: bool = True) -> IntervalIndex: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] # pyrefly: ignore[bad-override]
246+
def astype(self, dtype: DtypeArg, copy: bool = True) -> IntervalIndex: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] # pyrefly: ignore[bad-override] # ty: ignore[invalid-method-override]
247247
@property
248248
def inferred_type(self) -> str: ...
249249
def memory_usage(self, deep: bool = False) -> int: ...

pandas-stubs/core/reshape/pivot.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ _PivotTableColumnsTypes: TypeAlias = (
6161
_PivotTableValuesTypes: TypeAlias = Label | Sequence[Hashable] | None
6262

6363
_ExtendedAnyArrayLike: TypeAlias = AnyArrayLike | ArrayLike
64-
_Values: TypeAlias = SequenceNotStr[Any] | _ExtendedAnyArrayLike
64+
_CrossTabValues: TypeAlias = SequenceNotStr[Any] | _ExtendedAnyArrayLike
6565

6666
@overload
6767
def pivot_table(
@@ -118,9 +118,9 @@ def pivot(
118118
) -> DataFrame: ...
119119
@overload
120120
def crosstab(
121-
index: _Values | list[_Values],
122-
columns: _Values | list[_Values],
123-
values: _Values,
121+
index: _CrossTabValues | list[_CrossTabValues],
122+
columns: _CrossTabValues | list[_CrossTabValues],
123+
values: _CrossTabValues,
124124
rownames: SequenceNotStr[Hashable] | None = None,
125125
colnames: SequenceNotStr[Hashable] | None = None,
126126
*,
@@ -132,8 +132,8 @@ def crosstab(
132132
) -> DataFrame: ...
133133
@overload
134134
def crosstab(
135-
index: _Values | list[_Values],
136-
columns: _Values | list[_Values],
135+
index: _CrossTabValues | list[_CrossTabValues],
136+
columns: _CrossTabValues | list[_CrossTabValues],
137137
values: None = None,
138138
rownames: SequenceNotStr[Hashable] | None = None,
139139
colnames: SequenceNotStr[Hashable] | None = None,

pandas-stubs/core/series.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
446446
IntervalIndex[Interval[_OrderableT]]
447447
| Interval[_OrderableT]
448448
| Sequence[Interval[_OrderableT]]
449-
| dict[HashableT1, Interval[_OrderableT]]
449+
| dict[Hashable, Interval[_OrderableT]]
450450
),
451451
index: AxesData | None = None,
452452
dtype: Literal["Interval"] = ...,

pandas-stubs/io/json/_json.pyi

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
from collections import abc
2-
from collections.abc import Mapping
1+
from collections.abc import (
2+
Iterator,
3+
Mapping,
4+
)
35
from types import TracebackType
46
from typing import (
7+
Any,
58
Generic,
69
Literal,
710
overload,
@@ -226,7 +229,7 @@ def read_json(
226229
engine: Literal["pyarrow"],
227230
) -> DataFrame: ...
228231

229-
class JsonReader(abc.Iterator, Generic[NDFrameT]):
232+
class JsonReader(Iterator[Any], Generic[NDFrameT]):
230233
def read(self) -> NDFrameT: ...
231234
def close(self) -> None: ...
232235
def __iter__(self) -> JsonReader[NDFrameT]: ...

pandas-stubs/io/parsers/readers.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from collections import (
2-
abc,
32
defaultdict,
43
)
54
from collections.abc import (
65
Callable,
76
Hashable,
7+
Iterator,
88
Mapping,
99
Sequence,
1010
)
@@ -452,7 +452,7 @@ def read_fwf(
452452
**kwds: Any,
453453
) -> DataFrame: ...
454454

455-
class TextFileReader(abc.Iterator):
455+
class TextFileReader(Iterator[Any]):
456456
engine: CSVEngine
457457
orig_options: Mapping[str, Any]
458458
chunksize: int | None

pandas-stubs/io/sql.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ from pandas._typing import (
2424
DtypeBackend,
2525
Scalar,
2626
SequenceNotStr,
27-
npt,
27+
np_ndarray,
2828
)
2929

3030
_SQLConnection: TypeAlias = str | sqlalchemy.engine.Connectable | sqlite3.Connection
@@ -155,7 +155,7 @@ class PandasSQL:
155155
dtype: DtypeArg | None = None,
156156
method: (
157157
Literal["multi"]
158-
| Callable[[SQLTable, Any, list[str], Iterable], int | None]
158+
| Callable[[SQLTable, Any, list[str], Iterable[Any]], int | None]
159159
| None
160160
) = None,
161161
engine: str = "auto",
@@ -189,7 +189,7 @@ class SQLTable:
189189
def exists(self) -> bool: ...
190190
def sql_schema(self) -> str: ...
191191
def create(self) -> None: ...
192-
def insert_data(self) -> tuple[list[str], list[npt.NDArray]]: ...
192+
def insert_data(self) -> tuple[list[str], list[np_ndarray]]: ...
193193
def insert(
194194
self, chunksize: int | None = ..., method: str | None = ...
195195
) -> int | None: ...

0 commit comments

Comments
 (0)