Skip to content

Commit 7292c3a

Browse files
committed
Type args and kwargs in frame pipe
1 parent 2aadf0e commit 7292c3a

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

pandas-stubs/_typing.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ from pandas.core.generic import NDFrame
2828
from pandas.core.groupby.grouper import Grouper
2929
from pandas.core.indexes.base import Index
3030
from pandas.core.series import Series
31-
from typing_extensions import TypeAlias
31+
from typing_extensions import (
32+
ParamSpec,
33+
TypeAlias,
34+
)
3235

3336
from pandas._libs.interval import Interval
3437
from pandas._libs.tslibs import (
@@ -446,6 +449,7 @@ JSONSerializable: TypeAlias = PythonScalar | list | dict
446449
Axes: TypeAlias = AnyArrayLike | list | dict | range | tuple
447450
Renamer: TypeAlias = Mapping[Any, Label] | Callable[[Any], Label]
448451
T = TypeVar("T")
452+
P = ParamSpec("P")
449453
FuncType: TypeAlias = Callable[..., Any]
450454
F = TypeVar("F", bound=FuncType)
451455
HashableT = TypeVar("HashableT", bound=Hashable)

pandas-stubs/core/frame.pyi

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ from pandas.core.window.rolling import (
5252
Rolling,
5353
Window,
5454
)
55-
from typing_extensions import Self
55+
from typing_extensions import (
56+
Concatenate,
57+
Self,
58+
)
5659
import xarray as xr
5760

5861
from pandas._libs.missing import NAType
@@ -100,6 +103,7 @@ from pandas._typing import (
100103
MergeHow,
101104
NaPosition,
102105
NDFrameT,
106+
P,
103107
ParquetEngine,
104108
QuantileInterpolation,
105109
RandomState,
@@ -1832,9 +1836,9 @@ class DataFrame(NDFrame, OpsMixin):
18321836
) -> DataFrame: ...
18331837
def pipe(
18341838
self,
1835-
func: Callable[..., TType] | tuple[Callable[..., TType], _str],
1836-
*args,
1837-
**kwargs,
1839+
func: Callable[Concatenate[Self, P], TType] | tuple[Callable[..., TType], _str],
1840+
*args: P.args,
1841+
**kwargs: P.kwargs,
18381842
) -> TType: ...
18391843
def pop(self, item: _str) -> Series: ...
18401844
def pow(

tests/test_frame.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,16 @@ def baz(val: Styler) -> str:
13861386

13871387
check(assert_type(pd.DataFrame({"a": [1], "b": [1]}).style.pipe(baz), str), str)
13881388

1389+
def qux(df: pd.DataFrame, a: int, b: str) -> pd.DataFrame:
1390+
return pd.DataFrame(df)
1391+
1392+
check(
1393+
assert_type(
1394+
pd.DataFrame({"a": [1], "b": [1]}).pipe(qux, 1, b="b"), pd.DataFrame
1395+
),
1396+
pd.DataFrame,
1397+
)
1398+
13891399

13901400
# set_flags() method added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html
13911401
def test_types_set_flags() -> None:

0 commit comments

Comments
 (0)