Skip to content

Commit f63956a

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

File tree

14 files changed

+87
-90
lines changed

14 files changed

+87
-90
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: 5 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(
@@ -949,7 +950,7 @@ ReplaceValue: TypeAlias = (
949950
| NAType
950951
| Sequence[Scalar | Pattern]
951952
| Mapping[HashableT, ScalarT]
952-
| Series[Any]
953+
| Series
953954
| None
954955
)
955956

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: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ from pandas._libs.tslibs import BaseOffset
7272
from pandas._libs.tslibs.nattype import NaTType
7373
from pandas._libs.tslibs.offsets import DateOffset
7474
from pandas._typing import (
75-
S1,
75+
S2,
7676
AggFuncTypeBase,
7777
AggFuncTypeDictFrame,
7878
AggFuncTypeDictSeries,
@@ -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 = ...,
@@ -1389,13 +1389,13 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
13891389
@overload
13901390
def apply(
13911391
self,
1392-
f: Callable[..., S1 | NAType],
1392+
f: Callable[..., S2 | NAType],
13931393
axis: AxisIndex = ...,
13941394
raw: _bool = ...,
13951395
result_type: None = ...,
13961396
args: Any = ...,
13971397
**kwargs: Any,
1398-
) -> Series[S1]: ...
1398+
) -> Series[S2]: ...
13991399
# Since non-scalar type T is not supported in Series[T],
14001400
# we separate this overload from the above one
14011401
@overload
@@ -1407,24 +1407,24 @@ 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
14141414
def apply(
14151415
self,
1416-
f: Callable[..., S1 | NAType],
1416+
f: Callable[..., S2 | NAType],
14171417
axis: Axis = ...,
14181418
raw: _bool = ...,
14191419
args: Any = ...,
14201420
*,
14211421
result_type: Literal["expand", "reduce"],
14221422
**kwargs: Any,
1423-
) -> Series[S1]: ...
1423+
) -> Series[S2]: ...
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,27 +1461,27 @@ 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
14751475
def apply(
14761476
self,
1477-
f: Callable[..., S1 | NAType],
1477+
f: Callable[..., S2 | NAType],
14781478
raw: _bool = ...,
14791479
result_type: None = ...,
14801480
args: Any = ...,
14811481
*,
14821482
axis: AxisColumn,
14831483
**kwargs: Any,
1484-
) -> Series[S1]: ...
1484+
) -> Series[S2]: ...
14851485
@overload
14861486
def apply(
14871487
self,
@@ -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 = ...,
@@ -2008,7 +2008,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
20082008
| Callable[[DataFrame], DataFrame]
20092009
| Callable[[Any], _bool]
20102010
),
2011-
other: Scalar | Series[S1] | DataFrame | Callable | NAType | None = ...,
2011+
other: Scalar | Series[S2] | DataFrame | Callable | NAType | None = ...,
20122012
*,
20132013
inplace: Literal[True],
20142014
axis: Axis | None = ...,
@@ -2024,7 +2024,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
20242024
| Callable[[DataFrame], DataFrame]
20252025
| Callable[[Any], _bool]
20262026
),
2027-
other: Scalar | Series[S1] | DataFrame | Callable | NAType | None = ...,
2027+
other: Scalar | Series[S2] | DataFrame | Callable | NAType | None = ...,
20282028
*,
20292029
inplace: Literal[False] = ...,
20302030
axis: Axis | None = ...,

pandas-stubs/core/generic.pyi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ from typing_extensions import (
2828

2929
from pandas._libs.lib import NoDefault
3030
from pandas._typing import (
31-
S1,
31+
S2,
3232
Axis,
3333
CompressionOptions,
3434
CSVQuoting,
@@ -81,7 +81,7 @@ class NDFrame(indexing.IndexingMixin):
8181
def ndim(self) -> int: ...
8282
@property
8383
def size(self) -> int: ...
84-
def equals(self, other: Series[S1]) -> _bool: ...
84+
def equals(self, other: Series[S2]) -> _bool: ...
8585
def __neg__(self) -> Self: ...
8686
def __pos__(self) -> Self: ...
8787
def __nonzero__(self) -> None: ...
@@ -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 = ...,

0 commit comments

Comments
 (0)