From fcd6d36b9673627cc7df1c1ef449912b2a0ed523 Mon Sep 17 00:00:00 2001 From: tp Date: Sun, 10 Jan 2021 18:08:19 +0000 Subject: [PATCH 1/6] TYP: type up NDFrame.pipe GroupBy.pipe etc. --- pandas/core/common.py | 21 +++++++++++++++++++-- pandas/core/generic.py | 8 +++++++- pandas/core/groupby/groupby.py | 21 ++++++++++++++++----- pandas/core/resample.py | 12 ++++++++++-- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index a6514b5167460..effb49be08cef 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -8,7 +8,19 @@ import contextlib from functools import partial import inspect -from typing import Any, Collection, Iterable, Iterator, List, Optional, Union, cast +from typing import ( + TYPE_CHECKING, + Any, + Callable, + Collection, + Iterable, + Iterator, + List, + Optional, + Tuple, + Union, + cast, +) import warnings import numpy as np @@ -28,6 +40,9 @@ from pandas.core.dtypes.inference import iterable_not_string from pandas.core.dtypes.missing import isna, isnull, notnull # noqa +if TYPE_CHECKING: + from pandas._typing import T + class SettingWithCopyError(ValueError): pass @@ -405,7 +420,9 @@ def random_state(state=None): ) -def pipe(obj, func, *args, **kwargs): +def pipe( + obj, func: Union[Callable[..., T], Tuple[Callable[..., T], str]], *args, **kwargs +) -> T: """ Apply a function ``func`` to object ``obj`` either by passing obj as the first argument to the function or, in the case that the func is a tuple, diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2f4340c17c5a7..676f17a8cbf99 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -123,6 +123,7 @@ if TYPE_CHECKING: from pandas._libs.tslibs import BaseOffset + from pandas._typing import T from pandas.core.frame import DataFrame from pandas.core.resample import Resampler @@ -5356,7 +5357,12 @@ def sample( @final @doc(klass=_shared_doc_kwargs["klass"]) - def pipe(self, func, *args, **kwargs): + def pipe( + self, + func: Union[Callable[..., T], Tuple[Callable[..., T], str]], + *args, + **kwargs, + ) -> T: r""" Apply func(self, \*args, \*\*kwargs). diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index aef4c036abc65..90bd01110d886 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -6,6 +6,7 @@ class providing the base-class of operations. (defined in pandas.core.groupby.generic) expose these user-facing objects to provide specific functionality. """ +from __future__ import annotations from contextlib import contextmanager import datetime @@ -14,6 +15,7 @@ class providing the base-class of operations. from textwrap import dedent import types from typing import ( + TYPE_CHECKING, Callable, Dict, FrozenSet, @@ -78,6 +80,10 @@ class providing the base-class of operations. from pandas.core.sorting import get_group_index_sorter from pandas.core.util.numba_ import NUMBA_FUNC_CACHE +if TYPE_CHECKING: + from pandas._typing import T + + _common_see_also = """ See Also -------- @@ -476,7 +482,7 @@ def f(self): @contextmanager -def group_selection_context(groupby: "BaseGroupBy") -> Iterator["BaseGroupBy"]: +def group_selection_context(groupby: BaseGroupBy) -> Iterator[BaseGroupBy]: """ Set / reset the group_selection_context. """ @@ -724,8 +730,8 @@ def _set_group_selection(self) -> None: @final def _set_result_index_ordered( - self, result: "OutputFrameOrSeries" - ) -> "OutputFrameOrSeries": + self, result: OutputFrameOrSeries + ) -> OutputFrameOrSeries: # set the result index on the passed values object and # return the new object, xref 8046 @@ -790,7 +796,12 @@ def __getattr__(self, attr: str): ), ) @Appender(_pipe_template) - def pipe(self, func, *args, **kwargs): + def pipe( + self, + func: Union[Callable[..., T], Tuple[Callable[..., T], str]], + *args, + **kwargs, + ) -> T: return com.pipe(self, func, *args, **kwargs) plot = property(GroupByPlot) @@ -3058,7 +3069,7 @@ def get_groupby( by: Optional[_KeysArgType] = None, axis: int = 0, level=None, - grouper: "Optional[ops.BaseGrouper]" = None, + grouper: Optional[ops.BaseGrouper] = None, exclusions=None, selection=None, as_index: bool = True, diff --git a/pandas/core/resample.py b/pandas/core/resample.py index e432ec6cb54a2..79bbf0cd734a1 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1,7 +1,7 @@ import copy from datetime import timedelta from textwrap import dedent -from typing import Dict, Optional, Union, no_type_check +from typing import TYPE_CHECKING, Callable, Dict, Optional, Tuple, Union, no_type_check import numpy as np @@ -43,6 +43,9 @@ from pandas.tseries.frequencies import is_subperiod, is_superperiod from pandas.tseries.offsets import DateOffset, Day, Nano, Tick +if TYPE_CHECKING: + from pandas._typing import T + _shared_docs_kwargs: Dict[str, str] = {} @@ -231,7 +234,12 @@ def _assure_grouper(self): 2012-08-04 1""", ) @Appender(_pipe_template) - def pipe(self, func, *args, **kwargs): + def pipe( + self, + func: Union[Callable[..., T], Tuple[Callable[..., T], str]], + *args, + **kwargs, + ) -> T: return super().pipe(func, *args, **kwargs) _agg_see_also_doc = dedent( From cde9241fd278af120bef3f13d1842fa4e0f34466 Mon Sep 17 00:00:00 2001 From: tp Date: Sun, 10 Jan 2021 18:27:47 +0000 Subject: [PATCH 2/6] fixup flake8 stuff --- pandas/core/common.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index effb49be08cef..81e9ac8a1739d 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -40,9 +40,6 @@ from pandas.core.dtypes.inference import iterable_not_string from pandas.core.dtypes.missing import isna, isnull, notnull # noqa -if TYPE_CHECKING: - from pandas._typing import T - class SettingWithCopyError(ValueError): pass From b5dace183def4c954fc1a715317c486445537ecf Mon Sep 17 00:00:00 2001 From: tp Date: Sun, 10 Jan 2021 18:31:15 +0000 Subject: [PATCH 3/6] fixup more stuff --- pandas/core/resample.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 79bbf0cd734a1..0a23a1f2c2251 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import copy from datetime import timedelta from textwrap import dedent From 1b7fce09c6f17584cf0f473230f8365f613870cd Mon Sep 17 00:00:00 2001 From: tp Date: Sun, 10 Jan 2021 18:45:31 +0000 Subject: [PATCH 4/6] fixup more stuff --- pandas/core/common.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 81e9ac8a1739d..e23091b2efa45 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -9,7 +9,6 @@ from functools import partial import inspect from typing import ( - TYPE_CHECKING, Any, Callable, Collection, From 3dfcef0ee6464116e7b1ff713a612720d8d571fb Mon Sep 17 00:00:00 2001 From: tp Date: Tue, 12 Jan 2021 13:42:13 +0000 Subject: [PATCH 5/6] don't use TYPE_CHECKING --- pandas/core/generic.py | 2 +- pandas/core/groupby/groupby.py | 5 +---- pandas/core/resample.py | 5 +---- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 676f17a8cbf99..9508d98d3817b 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -49,6 +49,7 @@ NpDtype, Renamer, StorageOptions, + T, TimedeltaConvertibleTypes, TimestampConvertibleTypes, ValueKeyFunc, @@ -123,7 +124,6 @@ if TYPE_CHECKING: from pandas._libs.tslibs import BaseOffset - from pandas._typing import T from pandas.core.frame import DataFrame from pandas.core.resample import Resampler diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 90bd01110d886..ba80ab5096d53 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -47,6 +47,7 @@ class providing the base-class of operations. IndexLabel, Label, Scalar, + T, final, ) from pandas.compat.numpy import function as nv @@ -80,10 +81,6 @@ class providing the base-class of operations. from pandas.core.sorting import get_group_index_sorter from pandas.core.util.numba_ import NUMBA_FUNC_CACHE -if TYPE_CHECKING: - from pandas._typing import T - - _common_see_also = """ See Also -------- diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 0a23a1f2c2251..ff268ef0f13b3 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -16,7 +16,7 @@ Timestamp, to_offset, ) -from pandas._typing import TimedeltaConvertibleTypes, TimestampConvertibleTypes +from pandas._typing import T, TimedeltaConvertibleTypes, TimestampConvertibleTypes from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError from pandas.util._decorators import Appender, Substitution, doc @@ -45,9 +45,6 @@ from pandas.tseries.frequencies import is_subperiod, is_superperiod from pandas.tseries.offsets import DateOffset, Day, Nano, Tick -if TYPE_CHECKING: - from pandas._typing import T - _shared_docs_kwargs: Dict[str, str] = {} From 34a83db0c57ac0a5bc63de9bf62ae15342974212 Mon Sep 17 00:00:00 2001 From: tp Date: Tue, 12 Jan 2021 13:47:11 +0000 Subject: [PATCH 6/6] flake8 stuff --- pandas/core/groupby/groupby.py | 1 - pandas/core/resample.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index ba80ab5096d53..741de5e23f9bc 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -15,7 +15,6 @@ class providing the base-class of operations. from textwrap import dedent import types from typing import ( - TYPE_CHECKING, Callable, Dict, FrozenSet, diff --git a/pandas/core/resample.py b/pandas/core/resample.py index ff268ef0f13b3..f6c1da723a1d9 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -3,7 +3,7 @@ import copy from datetime import timedelta from textwrap import dedent -from typing import TYPE_CHECKING, Callable, Dict, Optional, Tuple, Union, no_type_check +from typing import Callable, Dict, Optional, Tuple, Union, no_type_check import numpy as np