Skip to content

Commit 4cb2e1a

Browse files
committed
Type common pipe with function params
1 parent 6155d05 commit 4cb2e1a

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

pandas/core/common.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@
5151
from pandas._typing import (
5252
AnyArrayLike,
5353
ArrayLike,
54+
Concatenate,
5455
NpDtype,
56+
NDFrameT,
57+
P,
5558
RandomState,
5659
T,
5760
)
@@ -463,8 +466,31 @@ def random_state(state: RandomState | None = None):
463466
)
464467

465468

469+
@overload
470+
def pipe(
471+
obj: NDFrameT,
472+
func: Callable[Concatenate[NDFrameT, P], T],
473+
*args: P.args,
474+
**kwargs: P.kwargs,
475+
) -> T:
476+
...
477+
478+
479+
@overload
480+
def pipe(
481+
obj: Any,
482+
func: tuple[Callable[..., T], str],
483+
*args: Any,
484+
**kwargs: Any,
485+
) -> T:
486+
...
487+
488+
466489
def pipe(
467-
obj, func: Callable[..., T] | tuple[Callable[..., T], str], *args, **kwargs
490+
obj: NDFrameT,
491+
func: Callable[Concatenate[NDFrameT, P], T] | tuple[Callable[..., T], str],
492+
*args: Any,
493+
**kwargs: Any,
468494
) -> T:
469495
"""
470496
Apply a function ``func`` to object ``obj`` either by passing obj as the
@@ -490,12 +516,13 @@ def pipe(
490516
object : the return type of ``func``.
491517
"""
492518
if isinstance(func, tuple):
493-
func, target = func
519+
# Assigning to func_ so pyright understands that it's a callable
520+
func_, target = func
494521
if target in kwargs:
495522
msg = f"{target} is both the pipe target and a keyword argument"
496523
raise ValueError(msg)
497524
kwargs[target] = obj
498-
return func(*args, **kwargs)
525+
return func_(*args, **kwargs)
499526
else:
500527
return func(obj, *args, **kwargs)
501528

0 commit comments

Comments
 (0)