Skip to content

Commit

Permalink
Type generic pipe with function params
Browse files Browse the repository at this point in the history
  • Loading branch information
paw-lu committed Jan 7, 2024
1 parent b2bca5e commit 6155d05
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
13 changes: 12 additions & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,29 @@
from typing import SupportsIndex

if sys.version_info >= (3, 10):
from typing import Concatenate # pyright: ignore[reportUnusedImport]
from typing import ParamSpec
from typing import TypeGuard # pyright: ignore[reportUnusedImport]
else:
from typing_extensions import TypeGuard # pyright: ignore[reportUnusedImport]
from typing_extensions import ( # pyright: ignore[reportUnusedImport]
Concatenate,
ParamSpec,
TypeGuard,
)

P = ParamSpec("P")

if sys.version_info >= (3, 11):
from typing import Self # pyright: ignore[reportUnusedImport]
else:
from typing_extensions import Self # pyright: ignore[reportUnusedImport]

else:
npt: Any = None
ParamSpec: Any = None
Self: Any = None
TypeGuard: Any = None
Concatenate: Any = None

HashableT = TypeVar("HashableT", bound=Hashable)
MutableMappingT = TypeVar("MutableMappingT", bound=MutableMapping)
Expand Down
26 changes: 23 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
Axis,
AxisInt,
CompressionOptions,
Concatenate,
DtypeArg,
DtypeBackend,
DtypeObj,
Expand Down Expand Up @@ -213,6 +214,7 @@
)

from pandas._libs.tslibs import BaseOffset
from pandas._typing import P

from pandas import (
DataFrame,
Expand Down Expand Up @@ -6118,13 +6120,31 @@ def sample(

return result

@overload
def pipe(
self,
func: Callable[Concatenate[Self, P], T],
*args: P.args,
**kwargs: P.kwargs,
) -> T:
...

@overload
def pipe(
self,
func: tuple[Callable[..., T], str],
*args: Any,
**kwargs: Any,
) -> T:
...

@final
@doc(klass=_shared_doc_kwargs["klass"])
def pipe(
self,
func: Callable[..., T] | tuple[Callable[..., T], str],
*args,
**kwargs,
func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str],
*args: Any,
**kwargs: Any,
) -> T:
r"""
Apply chainable functions that expect Series or DataFrames.
Expand Down

0 comments on commit 6155d05

Please sign in to comment.