Skip to content

Commit 16544c9

Browse files
TYP: Check for use of Union[Series, DataFrame] instead of FrameOrSeriesUnion alias (#36137)
1 parent 45cec12 commit 16544c9

File tree

7 files changed

+31
-25
lines changed

7 files changed

+31
-25
lines changed

ci/code_checks.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
230230
invgrep -R --include=*.{py,pyx} '!r}' pandas
231231
RET=$(($RET + $?)) ; echo $MSG "DONE"
232232

233+
# -------------------------------------------------------------------------
234+
# Type annotations
235+
233236
MSG='Check for use of comment-based annotation syntax' ; echo $MSG
234237
invgrep -R --include="*.py" -P '# type: (?!ignore)' pandas
235238
RET=$(($RET + $?)) ; echo $MSG "DONE"
@@ -238,6 +241,11 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
238241
invgrep -R --include="*.py" -P '# type:\s?ignore(?!\[)' pandas
239242
RET=$(($RET + $?)) ; echo $MSG "DONE"
240243

244+
MSG='Check for use of Union[Series, DataFrame] instead of FrameOrSeriesUnion alias' ; echo $MSG
245+
invgrep -R --include="*.py" --exclude=_typing.py -E 'Union\[.*(Series.*DataFrame|DataFrame.*Series).*\]' pandas
246+
RET=$(($RET + $?)) ; echo $MSG "DONE"
247+
248+
# -------------------------------------------------------------------------
241249
MSG='Check for use of foo.__class__ instead of type(foo)' ; echo $MSG
242250
invgrep -R --include=*.{py,pyx} '\.__class__' pandas
243251
RET=$(($RET + $?)) ; echo $MSG "DONE"

pandas/core/apply.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import abc
22
import inspect
3-
from typing import TYPE_CHECKING, Any, Dict, Iterator, Optional, Tuple, Type, Union
3+
from typing import TYPE_CHECKING, Any, Dict, Iterator, Optional, Tuple, Type
44

55
import numpy as np
66

77
from pandas._config import option_context
88

9-
from pandas._typing import Axis
9+
from pandas._typing import Axis, FrameOrSeriesUnion
1010
from pandas.util._decorators import cache_readonly
1111

1212
from pandas.core.dtypes.common import is_dict_like, is_list_like, is_sequence
@@ -73,7 +73,7 @@ def series_generator(self) -> Iterator["Series"]:
7373
@abc.abstractmethod
7474
def wrap_results_for_axis(
7575
self, results: ResType, res_index: "Index"
76-
) -> Union["Series", "DataFrame"]:
76+
) -> FrameOrSeriesUnion:
7777
pass
7878

7979
# ---------------------------------------------------------------
@@ -289,9 +289,7 @@ def apply_series_generator(self) -> Tuple[ResType, "Index"]:
289289

290290
return results, res_index
291291

292-
def wrap_results(
293-
self, results: ResType, res_index: "Index"
294-
) -> Union["Series", "DataFrame"]:
292+
def wrap_results(self, results: ResType, res_index: "Index") -> FrameOrSeriesUnion:
295293
from pandas import Series
296294

297295
# see if we can infer the results
@@ -335,7 +333,7 @@ def result_columns(self) -> "Index":
335333

336334
def wrap_results_for_axis(
337335
self, results: ResType, res_index: "Index"
338-
) -> Union["Series", "DataFrame"]:
336+
) -> FrameOrSeriesUnion:
339337
""" return the results for the rows """
340338

341339
if self.result_type == "reduce":
@@ -408,9 +406,9 @@ def result_columns(self) -> "Index":
408406

409407
def wrap_results_for_axis(
410408
self, results: ResType, res_index: "Index"
411-
) -> Union["Series", "DataFrame"]:
409+
) -> FrameOrSeriesUnion:
412410
""" return the results for the columns """
413-
result: Union["Series", "DataFrame"]
411+
result: FrameOrSeriesUnion
414412

415413
# we have requested to expand
416414
if self.result_type == "expand":

pandas/core/groupby/generic.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def _aggregate_multiple_funcs(self, arg):
308308

309309
arg = zip(columns, arg)
310310

311-
results: Dict[base.OutputKey, Union[Series, DataFrame]] = {}
311+
results: Dict[base.OutputKey, FrameOrSeriesUnion] = {}
312312
for idx, (name, func) in enumerate(arg):
313313
obj = self
314314

@@ -332,7 +332,7 @@ def _wrap_series_output(
332332
self,
333333
output: Mapping[base.OutputKey, Union[Series, np.ndarray]],
334334
index: Optional[Index],
335-
) -> Union[Series, DataFrame]:
335+
) -> FrameOrSeriesUnion:
336336
"""
337337
Wraps the output of a SeriesGroupBy operation into the expected result.
338338
@@ -355,7 +355,7 @@ def _wrap_series_output(
355355
indexed_output = {key.position: val for key, val in output.items()}
356356
columns = Index(key.label for key in output)
357357

358-
result: Union[Series, DataFrame]
358+
result: FrameOrSeriesUnion
359359
if len(output) > 1:
360360
result = self.obj._constructor_expanddim(indexed_output, index=index)
361361
result.columns = columns
@@ -373,7 +373,7 @@ def _wrap_aggregated_output(
373373
self,
374374
output: Mapping[base.OutputKey, Union[Series, np.ndarray]],
375375
index: Optional[Index],
376-
) -> Union[Series, DataFrame]:
376+
) -> FrameOrSeriesUnion:
377377
"""
378378
Wraps the output of a SeriesGroupBy aggregation into the expected result.
379379
@@ -1085,7 +1085,7 @@ def blk_func(bvalues: ArrayLike) -> ArrayLike:
10851085
raise
10861086

10871087
# We get here with a) EADtypes and b) object dtype
1088-
obj: Union[Series, DataFrame]
1088+
obj: FrameOrSeriesUnion
10891089
# call our grouper again with only this block
10901090
if isinstance(bvalues, ExtensionArray):
10911091
# TODO(EA2D): special case not needed with 2D EAs

pandas/core/groupby/grouper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ class Grouping:
393393
----------
394394
index : Index
395395
grouper :
396-
obj Union[DataFrame, Series]:
396+
obj : DataFrame or Series
397397
name : Label
398398
level :
399399
observed : bool, default False

pandas/core/reshape/merge.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import datetime
77
from functools import partial
88
import string
9-
from typing import TYPE_CHECKING, Optional, Tuple, Union
9+
from typing import TYPE_CHECKING, Optional, Tuple
1010
import warnings
1111

1212
import numpy as np
1313

1414
from pandas._libs import Timedelta, hashtable as libhashtable, lib
1515
import pandas._libs.join as libjoin
16-
from pandas._typing import ArrayLike, FrameOrSeries
16+
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion
1717
from pandas.errors import MergeError
1818
from pandas.util._decorators import Appender, Substitution
1919

@@ -51,7 +51,7 @@
5151
from pandas.core.sorting import is_int64_overflow_possible
5252

5353
if TYPE_CHECKING:
54-
from pandas import DataFrame, Series # noqa:F401
54+
from pandas import DataFrame # noqa:F401
5555

5656

5757
@Substitution("\nleft : DataFrame")
@@ -575,8 +575,8 @@ class _MergeOperation:
575575

576576
def __init__(
577577
self,
578-
left: Union["Series", "DataFrame"],
579-
right: Union["Series", "DataFrame"],
578+
left: FrameOrSeriesUnion,
579+
right: FrameOrSeriesUnion,
580580
how: str = "inner",
581581
on=None,
582582
left_on=None,

pandas/core/reshape/pivot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import numpy as np
1414

15-
from pandas._typing import Label
15+
from pandas._typing import FrameOrSeriesUnion, Label
1616
from pandas.util._decorators import Appender, Substitution
1717

1818
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
@@ -200,7 +200,7 @@ def pivot_table(
200200

201201

202202
def _add_margins(
203-
table: Union["Series", "DataFrame"],
203+
table: FrameOrSeriesUnion,
204204
data,
205205
values,
206206
rows,

pandas/io/pytables.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from pandas._libs import lib, writers as libwriters
1818
from pandas._libs.tslibs import timezones
19-
from pandas._typing import ArrayLike, FrameOrSeries, Label
19+
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label
2020
from pandas.compat._optional import import_optional_dependency
2121
from pandas.compat.pickle_compat import patch_pickle
2222
from pandas.errors import PerformanceWarning
@@ -2566,7 +2566,7 @@ class Fixed:
25662566

25672567
pandas_kind: str
25682568
format_type: str = "fixed" # GH#30962 needed by dask
2569-
obj_type: Type[Union[DataFrame, Series]]
2569+
obj_type: Type[FrameOrSeriesUnion]
25702570
ndim: int
25712571
encoding: str
25722572
parent: HDFStore
@@ -4442,7 +4442,7 @@ class AppendableFrameTable(AppendableTable):
44424442
pandas_kind = "frame_table"
44434443
table_type = "appendable_frame"
44444444
ndim = 2
4445-
obj_type: Type[Union[DataFrame, Series]] = DataFrame
4445+
obj_type: Type[FrameOrSeriesUnion] = DataFrame
44464446

44474447
@property
44484448
def is_transposed(self) -> bool:

0 commit comments

Comments
 (0)