Skip to content

Commit 1a6dbd7

Browse files
committed
use default in TypeVar
1 parent ba4ebd8 commit 1a6dbd7

File tree

14 files changed

+72
-74
lines changed

14 files changed

+72
-74
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mypy round.py
4545
we get the following error message:
4646

4747
```text
48-
round.py:6: error: Argument "decimals" to "round" of "DataFrame" has incompatible type "DataFrame"; expected "Union[int, Dict[Any, Any], Series[Any]]" [arg-type]
48+
round.py:6: error: Argument "decimals" to "round" of "DataFrame" has incompatible type "DataFrame"; expected "Union[int, Dict[Any, Any], Series]" [arg-type]
4949
Found 1 error in 1 file (checked 1 source file)
5050
```
5151

docs/philosophy.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ lt = s < 3
2929

3030
In the pandas source, `lt` is a `Series` with a `dtype` of `bool`. In the pandas-stubs,
3131
the type of `lt` is `Series[bool]`. This allows further type checking to occur in other
32-
pandas methods. Note that in the above example, `s` is typed as `Series[Any]` because
33-
its type cannot be statically inferred.
32+
pandas methods. Note that in the above example, `s` is typed as `Series` (which defaults
33+
to `Series[Any]` because its type cannot be statically inferred.
3434

3535
This also allows type checking for operations on series that contain date/time data. Consider
3636
the following example that creates two series of datetimes with corresponding arithmetic.

pandas-stubs/_typing.pyi

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ from typing import (
1818
Protocol,
1919
SupportsIndex,
2020
TypedDict,
21-
TypeVar,
2221
overload,
2322
)
2423

@@ -35,6 +34,7 @@ from pandas.core.tools.datetimes import FulldatetimeDict
3534
from typing_extensions import (
3635
ParamSpec,
3736
TypeAlias,
37+
TypeVar,
3838
)
3939

4040
from pandas._libs.interval import Interval
@@ -65,7 +65,7 @@ HashableT5 = TypeVar("HashableT5", bound=Hashable)
6565
# array-like
6666

6767
ArrayLike: TypeAlias = ExtensionArray | np.ndarray
68-
AnyArrayLike: TypeAlias = ArrayLike | Index[Any] | Series[Any]
68+
AnyArrayLike: TypeAlias = ArrayLike | Index | Series
6969

7070
# list-like
7171

@@ -801,7 +801,7 @@ DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
801801
KeysArgType: TypeAlias = Any
802802
ListLikeT = TypeVar("ListLikeT", bound=ListLike)
803803
ListLikeExceptSeriesAndStr: TypeAlias = (
804-
MutableSequence[Any] | np.ndarray | tuple[Any, ...] | Index[Any]
804+
MutableSequence[Any] | np.ndarray | tuple[Any, ...] | Index
805805
)
806806
ListLikeU: TypeAlias = Sequence | np.ndarray | Series | Index
807807
ListLikeHashable: TypeAlias = (
@@ -842,6 +842,7 @@ S1 = TypeVar(
842842
| CategoricalDtype
843843
| BaseOffset
844844
| list[str],
845+
default=Any,
845846
)
846847

847848
S2 = TypeVar(
@@ -891,6 +892,7 @@ ByT = TypeVar(
891892
| Period
892893
| Interval[int | float | Timestamp | Timedelta]
893894
| tuple,
895+
default=Any,
894896
)
895897
# Use a distinct SeriesByT when using groupby with Series of known dtype.
896898
# Essentially, an intersection between Series S1 TypeVar, and ByT TypeVar
@@ -949,7 +951,7 @@ ReplaceValue: TypeAlias = (
949951
| NAType
950952
| Sequence[Scalar | Pattern]
951953
| Mapping[HashableT, ScalarT]
952-
| Series[Any]
954+
| Series
953955
| None
954956
)
955957

pandas-stubs/core/dtypes/missing.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ isneginf_scalar = ...
2626
@overload
2727
def isna(obj: DataFrame) -> DataFrame: ...
2828
@overload
29-
def isna(obj: Series[Any]) -> Series[bool]: ...
29+
def isna(obj: Series) -> Series[bool]: ...
3030
@overload
31-
def isna(obj: Index[Any] | list[Any] | ArrayLike) -> npt.NDArray[np.bool_]: ...
31+
def isna(obj: Index | list[Any] | ArrayLike) -> npt.NDArray[np.bool_]: ...
3232
@overload
3333
def isna(
3434
obj: Scalar | NaTType | NAType | None,
@@ -39,9 +39,9 @@ isnull = isna
3939
@overload
4040
def notna(obj: DataFrame) -> DataFrame: ...
4141
@overload
42-
def notna(obj: Series[Any]) -> Series[bool]: ...
42+
def notna(obj: Series) -> Series[bool]: ...
4343
@overload
44-
def notna(obj: Index[Any] | list[Any] | ArrayLike) -> npt.NDArray[np.bool_]: ...
44+
def notna(obj: Index | list[Any] | ArrayLike) -> npt.NDArray[np.bool_]: ...
4545
@overload
4646
def notna(obj: ScalarT | NaTType | NAType | None) -> TypeIs[ScalarT]: ...
4747

pandas-stubs/core/frame.pyi

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,11 +1315,11 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
13151315
@overload
13161316
def stack(
13171317
self, level: Level | list[Level] = ..., dropna: _bool = ..., sort: _bool = ...
1318-
) -> Self | Series[Any]: ...
1318+
) -> Self | Series: ...
13191319
@overload
13201320
def stack(
13211321
self, level: Level | list[Level] = ..., future_stack: _bool = ...
1322-
) -> Self | Series[Any]: ...
1322+
) -> Self | Series: ...
13231323
def explode(
13241324
self, column: Sequence[Hashable], ignore_index: _bool = ...
13251325
) -> Self: ...
@@ -1379,7 +1379,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
13791379
@overload
13801380
def apply(
13811381
self,
1382-
f: Callable[..., ListLikeExceptSeriesAndStr | Series[Any]],
1382+
f: Callable[..., ListLikeExceptSeriesAndStr | Series],
13831383
axis: AxisIndex = ...,
13841384
raw: _bool = ...,
13851385
result_type: None = ...,
@@ -1407,7 +1407,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
14071407
result_type: None = ...,
14081408
args: Any = ...,
14091409
**kwargs: Any,
1410-
) -> Series[Any]: ...
1410+
) -> Series: ...
14111411

14121412
# apply() overloads with keyword result_type, and axis does not matter
14131413
@overload
@@ -1424,7 +1424,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
14241424
@overload
14251425
def apply(
14261426
self,
1427-
f: Callable[..., ListLikeExceptSeriesAndStr | Series[Any] | Mapping[Any, Any]],
1427+
f: Callable[..., ListLikeExceptSeriesAndStr | Series | Mapping[Any, Any]],
14281428
axis: Axis = ...,
14291429
raw: _bool = ...,
14301430
args: Any = ...,
@@ -1442,12 +1442,12 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
14421442
*,
14431443
result_type: Literal["reduce"],
14441444
**kwargs: Any,
1445-
) -> Series[Any]: ...
1445+
) -> Series: ...
14461446
@overload
14471447
def apply(
14481448
self,
14491449
f: Callable[
1450-
..., ListLikeExceptSeriesAndStr | Series[Any] | Scalar | Mapping[Any, Any]
1450+
..., ListLikeExceptSeriesAndStr | Series | Scalar | Mapping[Any, Any]
14511451
],
14521452
axis: Axis = ...,
14531453
raw: _bool = ...,
@@ -1461,14 +1461,14 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
14611461
@overload
14621462
def apply(
14631463
self,
1464-
f: Callable[..., Series[Any]],
1464+
f: Callable[..., Series],
14651465
axis: AxisIndex = ...,
14661466
raw: _bool = ...,
14671467
args: Any = ...,
14681468
*,
14691469
result_type: Literal["reduce"],
14701470
**kwargs: Any,
1471-
) -> Series[Any]: ...
1471+
) -> Series: ...
14721472

14731473
# apply() overloads with default result_type of None, and keyword axis=1 matters
14741474
@overload
@@ -1492,11 +1492,11 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
14921492
*,
14931493
axis: AxisColumn,
14941494
**kwargs: Any,
1495-
) -> Series[Any]: ...
1495+
) -> Series: ...
14961496
@overload
14971497
def apply(
14981498
self,
1499-
f: Callable[..., Series[Any]],
1499+
f: Callable[..., Series],
15001500
raw: _bool = ...,
15011501
result_type: None = ...,
15021502
args: Any = ...,
@@ -1509,7 +1509,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
15091509
@overload
15101510
def apply(
15111511
self,
1512-
f: Callable[..., Series[Any]],
1512+
f: Callable[..., Series],
15131513
raw: _bool = ...,
15141514
args: Any = ...,
15151515
*,
@@ -1534,7 +1534,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
15341534
) -> Self: ...
15351535
def merge(
15361536
self,
1537-
right: DataFrame | Series[Any],
1537+
right: DataFrame | Series,
15381538
how: MergeHow = ...,
15391539
on: IndexLabel | AnyArrayLike | None = ...,
15401540
left_on: IndexLabel | AnyArrayLike | None = ...,

pandas-stubs/core/generic.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ class NDFrame(indexing.IndexingMixin):
305305
labels: None = ...,
306306
*,
307307
axis: Axis = ...,
308-
index: Hashable | Sequence[Hashable] | Index[Any] = ...,
308+
index: Hashable | Sequence[Hashable] | Index = ...,
309309
columns: Hashable | Iterable[Hashable],
310310
level: Level | None = ...,
311311
inplace: Literal[True],
@@ -317,7 +317,7 @@ class NDFrame(indexing.IndexingMixin):
317317
labels: None = ...,
318318
*,
319319
axis: Axis = ...,
320-
index: Hashable | Sequence[Hashable] | Index[Any],
320+
index: Hashable | Sequence[Hashable] | Index,
321321
columns: Hashable | Iterable[Hashable] = ...,
322322
level: Level | None = ...,
323323
inplace: Literal[True],
@@ -326,7 +326,7 @@ class NDFrame(indexing.IndexingMixin):
326326
@overload
327327
def drop(
328328
self,
329-
labels: Hashable | Sequence[Hashable] | Index[Any],
329+
labels: Hashable | Sequence[Hashable] | Index,
330330
*,
331331
axis: Axis = ...,
332332
index: None = ...,
@@ -341,7 +341,7 @@ class NDFrame(indexing.IndexingMixin):
341341
labels: None = ...,
342342
*,
343343
axis: Axis = ...,
344-
index: Hashable | Sequence[Hashable] | Index[Any] = ...,
344+
index: Hashable | Sequence[Hashable] | Index = ...,
345345
columns: Hashable | Iterable[Hashable],
346346
level: Level | None = ...,
347347
inplace: Literal[False] = ...,
@@ -353,7 +353,7 @@ class NDFrame(indexing.IndexingMixin):
353353
labels: None = ...,
354354
*,
355355
axis: Axis = ...,
356-
index: Hashable | Sequence[Hashable] | Index[Any],
356+
index: Hashable | Sequence[Hashable] | Index,
357357
columns: Hashable | Iterable[Hashable] = ...,
358358
level: Level | None = ...,
359359
inplace: Literal[False] = ...,
@@ -362,7 +362,7 @@ class NDFrame(indexing.IndexingMixin):
362362
@overload
363363
def drop(
364364
self,
365-
labels: Hashable | Sequence[Hashable] | Index[Any],
365+
labels: Hashable | Sequence[Hashable] | Index,
366366
*,
367367
axis: Axis = ...,
368368
index: None = ...,

pandas-stubs/core/groupby/generic.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ from typing import (
1010
Generic,
1111
Literal,
1212
NamedTuple,
13-
TypeVar,
1413
final,
1514
overload,
1615
)
@@ -26,6 +25,7 @@ from pandas.core.series import Series
2625
from typing_extensions import (
2726
Self,
2827
TypeAlias,
28+
TypeVar,
2929
)
3030

3131
from pandas._libs.tslibs.timestamps import Timestamp
@@ -168,7 +168,7 @@ class SeriesGroupBy(GroupBy[Series[S1]], Generic[S1, ByT]):
168168
self,
169169
) -> Iterator[tuple[ByT, Series[S1]]]: ...
170170

171-
_TT = TypeVar("_TT", bound=Literal[True, False])
171+
_TT = TypeVar("_TT", bound=Literal[True, False], default=Literal[True])
172172

173173
class DataFrameGroupBy(GroupBy[DataFrame], Generic[ByT, _TT]):
174174
# error: Overload 3 for "apply" will never be used because its parameters overlap overload 1

pandas-stubs/core/indexes/base.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ class Index(IndexOpsMixin[S1]):
475475
),
476476
) -> Self: ...
477477

478-
UnknownIndex: TypeAlias = Index[Any]
478+
UnknownIndex: TypeAlias = Index
479479

480480
def ensure_index_from_sequences(
481481
sequences: Sequence[Sequence[Dtype]], names: list[str] = ...

pandas-stubs/core/indexes/multi.pyi

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ from collections.abc import (
44
Iterable,
55
Sequence,
66
)
7-
from typing import (
8-
Any,
9-
overload,
10-
)
7+
from typing import overload
118

129
import numpy as np
1310
import pandas as pd
@@ -27,7 +24,7 @@ from pandas._typing import (
2724
np_ndarray_bool,
2825
)
2926

30-
class MultiIndex(Index[Any]):
27+
class MultiIndex(Index):
3128
def __new__(
3229
cls,
3330
levels: Sequence[SequenceNotStr[Hashable]] = ...,

pandas-stubs/core/reshape/concat.pyi

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ from collections.abc import (
44
Sequence,
55
)
66
from typing import (
7-
Any,
87
Literal,
98
overload,
109
)
@@ -40,7 +39,7 @@ def concat( # type: ignore[overload-overlap]
4039
) -> DataFrame: ...
4140
@overload
4241
def concat( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
43-
objs: Iterable[Series[Any]] | Mapping[HashableT1, Series[Any]],
42+
objs: Iterable[Series] | Mapping[HashableT1, Series],
4443
*,
4544
axis: AxisIndex = ...,
4645
join: Literal["inner", "outer"] = ...,
@@ -51,11 +50,11 @@ def concat( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappin
5150
verify_integrity: bool = ...,
5251
sort: bool = ...,
5352
copy: bool = ...,
54-
) -> Series[Any]: ...
53+
) -> Series: ...
5554
@overload
5655
def concat( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
5756
objs: (
58-
Iterable[Series[Any] | DataFrame] | Mapping[HashableT1, Series[Any] | DataFrame]
57+
Iterable[Series | DataFrame] | Mapping[HashableT1, Series | DataFrame]
5958
),
6059
*,
6160
axis: Axis = ...,
@@ -98,7 +97,7 @@ def concat( # type: ignore[overload-overlap]
9897
) -> DataFrame: ...
9998
@overload
10099
def concat( # type: ignore[overload-overlap]
101-
objs: Iterable[Series[Any] | None] | Mapping[HashableT1, Series[Any] | None],
100+
objs: Iterable[Series | None] | Mapping[HashableT1, Series | None],
102101
*,
103102
axis: AxisIndex = ...,
104103
join: Literal["inner", "outer"] = ...,
@@ -109,12 +108,12 @@ def concat( # type: ignore[overload-overlap]
109108
verify_integrity: bool = ...,
110109
sort: bool = ...,
111110
copy: bool = ...,
112-
) -> Series[Any]: ...
111+
) -> Series: ...
113112
@overload
114113
def concat(
115114
objs: (
116-
Iterable[Series[Any] | DataFrame | None]
117-
| Mapping[HashableT1, Series[Any] | DataFrame | None]
115+
Iterable[Series | DataFrame | None]
116+
| Mapping[HashableT1, Series | DataFrame | None]
118117
),
119118
*,
120119
axis: Axis = ...,

pandas-stubs/core/series.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
13091309
dtype: ObjectDtypeArg | VoidDtypeArg | ExtensionDtype | DtypeObj,
13101310
copy: _bool = ...,
13111311
errors: IgnoreRaise = ...,
1312-
) -> Series[Any]: ...
1312+
) -> Series: ...
13131313
def copy(self, deep: _bool = ...) -> Series[S1]: ...
13141314
def infer_objects(self) -> Series[S1]: ...
13151315
@overload
@@ -2354,4 +2354,4 @@ class IntervalSeries(Series[Interval[_OrderableT]], Generic[_OrderableT]):
23542354
def array(self) -> IntervalArray: ...
23552355
def diff(self, periods: int = ...) -> Never: ...
23562356

2357-
UnknownSeries: TypeAlias = Series[Any]
2357+
UnknownSeries: TypeAlias = Series

pandas-stubs/io/formats/style_render.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ class StylerRenderer:
8282
level: Level | list[Level] | None = ...,
8383
) -> Self: ...
8484
@property
85-
def columns(self) -> Index[Any]: ...
85+
def columns(self) -> Index: ...
8686
@property
87-
def index(self) -> Index[Any]: ...
87+
def index(self) -> Index: ...

0 commit comments

Comments
 (0)