Skip to content

Commit fc6d249

Browse files
committed
Type generic pipe with function params
1 parent b2bca5e commit fc6d249

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

pandas/_typing.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,23 @@
9090
from typing import SupportsIndex
9191

9292
if sys.version_info >= (3, 10):
93-
from typing import TypeGuard # pyright: ignore[reportUnusedImport]
93+
from typing import ( # pyright: ignore[reportUnusedImport]
94+
ParamSpec,
95+
TypeGuard,
96+
)
9497
else:
95-
from typing_extensions import TypeGuard # pyright: ignore[reportUnusedImport]
98+
from typing_extensions import ( # pyright: ignore[reportUnusedImport]
99+
ParamSpec,
100+
TypeGuard,
101+
)
96102

97103
if sys.version_info >= (3, 11):
98104
from typing import Self # pyright: ignore[reportUnusedImport]
99105
else:
100106
from typing_extensions import Self # pyright: ignore[reportUnusedImport]
101107
else:
102108
npt: Any = None
109+
ParamSpec: Any = None
103110
Self: Any = None
104111
TypeGuard: Any = None
105112

@@ -222,6 +229,7 @@ def __reversed__(self) -> Iterator[_T_co]:
222229

223230
# to maintain type information across generic functions and parametrization
224231
T = TypeVar("T")
232+
P = ParamSpec("P")
225233

226234
# used in decorators to preserve the signature of the function it decorates
227235
# see https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators

pandas/core/generic.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
)
214214

215215
from pandas._libs.tslibs import BaseOffset
216+
from pandas._typing import P
216217

217218
from pandas import (
218219
DataFrame,
@@ -223,6 +224,12 @@
223224
from pandas.core.indexers.objects import BaseIndexer
224225
from pandas.core.resample import Resampler
225226

227+
if sys.version_info >= (3, 10):
228+
from typing import Concatenate
229+
230+
else:
231+
from typing_extensions import Concatenate
232+
226233
# goal is to be able to define the docs close to function, while still being
227234
# able to share
228235
_shared_docs = {**_shared_docs}
@@ -6118,13 +6125,31 @@ def sample(
61186125

61196126
return result
61206127

6128+
@overload
6129+
def pipe(
6130+
self,
6131+
func: Callable[Concatenate[Self, P], T],
6132+
*args: P.args,
6133+
**kwargs: P.kwargs,
6134+
) -> T:
6135+
...
6136+
6137+
@overload
6138+
def pipe(
6139+
self,
6140+
func: tuple[Callable[..., T], str],
6141+
*args: Any,
6142+
**kwargs: Any,
6143+
) -> T:
6144+
...
6145+
61216146
@final
61226147
@doc(klass=_shared_doc_kwargs["klass"])
61236148
def pipe(
61246149
self,
6125-
func: Callable[..., T] | tuple[Callable[..., T], str],
6126-
*args,
6127-
**kwargs,
6150+
func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str],
6151+
*args: Any,
6152+
**kwargs: Any,
61286153
) -> T:
61296154
r"""
61306155
Apply chainable functions that expect Series or DataFrames.

0 commit comments

Comments
 (0)