Skip to content

Commit d483370

Browse files
committed
Type common pipe with function params
1 parent f89d534 commit d483370

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

pandas/core/common.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
TYPE_CHECKING,
2525
Any,
2626
Callable,
27+
TypeVar,
2728
cast,
2829
overload,
2930
)
@@ -51,7 +52,9 @@
5152
from pandas._typing import (
5253
AnyArrayLike,
5354
ArrayLike,
55+
Concatenate,
5456
NpDtype,
57+
P,
5558
RandomState,
5659
T,
5760
)
@@ -463,8 +466,34 @@ def random_state(state: RandomState | None = None):
463466
)
464467

465468

469+
_T = TypeVar("_T") # Secondary TypeVar for use in pipe's type hints
470+
471+
472+
@overload
473+
def pipe(
474+
obj: _T,
475+
func: Callable[Concatenate[_T, P], T],
476+
*args: P.args,
477+
**kwargs: P.kwargs,
478+
) -> T:
479+
...
480+
481+
482+
@overload
483+
def pipe(
484+
obj: Any,
485+
func: tuple[Callable[..., T], str],
486+
*args: Any,
487+
**kwargs: Any,
488+
) -> T:
489+
...
490+
491+
466492
def pipe(
467-
obj, func: Callable[..., T] | tuple[Callable[..., T], str], *args, **kwargs
493+
obj: _T,
494+
func: Callable[Concatenate[_T, P], T] | tuple[Callable[..., T], str],
495+
*args: Any,
496+
**kwargs: Any,
468497
) -> T:
469498
"""
470499
Apply a function ``func`` to object ``obj`` either by passing obj as the
@@ -490,12 +519,13 @@ def pipe(
490519
object : the return type of ``func``.
491520
"""
492521
if isinstance(func, tuple):
493-
func, target = func
522+
# Assigning to func_ so pyright understands that it's a callable
523+
func_, target = func
494524
if target in kwargs:
495525
msg = f"{target} is both the pipe target and a keyword argument"
496526
raise ValueError(msg)
497527
kwargs[target] = obj
498-
return func(*args, **kwargs)
528+
return func_(*args, **kwargs)
499529
else:
500530
return func(obj, *args, **kwargs)
501531

0 commit comments

Comments
 (0)