Skip to content

Commit 7ffb960

Browse files
authored
STYLE: enable ruff TCH on some file (#51835)
* STYLE: enable ruff TCH on some file * Fix bug * Fix bug * Fix typo import * Add import annotations
1 parent f7f0df1 commit 7ffb960

File tree

16 files changed

+72
-50
lines changed

16 files changed

+72
-50
lines changed

pandas/plotting/_core.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
from __future__ import annotations
22

33
import importlib
4-
import types
54
from typing import (
65
TYPE_CHECKING,
76
Sequence,
87
)
98

109
from pandas._config import get_option
1110

12-
from pandas._typing import IndexLabel
1311
from pandas.util._decorators import (
1412
Appender,
1513
Substitution,
@@ -27,8 +25,12 @@
2725
from pandas.core.base import PandasObject
2826

2927
if TYPE_CHECKING:
28+
import types
29+
3030
from matplotlib.axes import Axes
3131

32+
from pandas._typing import IndexLabel
33+
3234
from pandas import DataFrame
3335

3436

pandas/plotting/_matplotlib/boxplot.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from matplotlib.artist import setp
1212
import numpy as np
1313

14-
from pandas._typing import MatplotlibColor
1514
from pandas.util._exceptions import find_stack_level
1615

1716
from pandas.core.dtypes.common import is_dict_like
@@ -37,6 +36,8 @@
3736
from matplotlib.axes import Axes
3837
from matplotlib.lines import Line2D
3938

39+
from pandas._typing import MatplotlibColor
40+
4041

4142
class BoxPlot(LinePlot):
4243
@property

pandas/plotting/_matplotlib/core.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@
1414
import warnings
1515

1616
import matplotlib as mpl
17-
from matplotlib.artist import Artist
1817
import numpy as np
1918

20-
from pandas._typing import (
21-
IndexLabel,
22-
PlottingOrientation,
23-
npt,
24-
)
2519
from pandas.errors import AbstractMethodError
2620
from pandas.util._decorators import cache_readonly
2721
from pandas.util._exceptions import find_stack_level
@@ -79,9 +73,16 @@
7973
)
8074

8175
if TYPE_CHECKING:
76+
from matplotlib.artist import Artist
8277
from matplotlib.axes import Axes
8378
from matplotlib.axis import Axis
8479

80+
from pandas._typing import (
81+
IndexLabel,
82+
PlottingOrientation,
83+
npt,
84+
)
85+
8586

8687
def _color_in_style(style: str) -> bool:
8788
"""

pandas/plotting/_matplotlib/groupby.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
from __future__ import annotations
22

3-
import numpy as np
3+
from typing import TYPE_CHECKING
44

5-
from pandas._typing import (
6-
Dict,
7-
IndexLabel,
8-
)
5+
import numpy as np
96

107
from pandas.core.dtypes.missing import remove_na_arraylike
118

@@ -18,6 +15,12 @@
1815

1916
from pandas.plotting._matplotlib.misc import unpack_single_str_list
2017

18+
if TYPE_CHECKING:
19+
from pandas._typing import (
20+
Dict,
21+
IndexLabel,
22+
)
23+
2124

2225
def create_iter_data_given_by(
2326
data: DataFrame, kind: str = "hist"

pandas/plotting/_matplotlib/hist.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import numpy as np
99

10-
from pandas._typing import PlottingOrientation
11-
1210
from pandas.core.dtypes.common import (
1311
is_integer,
1412
is_list_like,
@@ -42,6 +40,8 @@
4240
if TYPE_CHECKING:
4341
from matplotlib.axes import Axes
4442

43+
from pandas._typing import PlottingOrientation
44+
4545
from pandas import DataFrame
4646

4747

pandas/plotting/_matplotlib/timeseries.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
from datetime import timedelta
65
import functools
76
from typing import (
87
TYPE_CHECKING,
@@ -37,6 +36,8 @@
3736
)
3837

3938
if TYPE_CHECKING:
39+
from datetime import timedelta
40+
4041
from matplotlib.axes import Axes
4142

4243
from pandas import (

pandas/tests/extension/array_with_attr/array.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
from __future__ import annotations
66

77
import numbers
8+
from typing import TYPE_CHECKING
89

910
import numpy as np
1011

11-
from pandas._typing import type_t
12-
1312
from pandas.core.dtypes.base import ExtensionDtype
1413

1514
import pandas as pd
1615
from pandas.core.arrays import ExtensionArray
1716

17+
if TYPE_CHECKING:
18+
from pandas._typing import type_t
19+
1820

1921
class FloatAttrDtype(ExtensionDtype):
2022
type = float

pandas/tests/extension/date/array.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1+
from __future__ import annotations
2+
13
import datetime as dt
24
from typing import (
5+
TYPE_CHECKING,
36
Any,
4-
Optional,
57
Sequence,
6-
Tuple,
7-
Union,
88
cast,
99
)
1010

1111
import numpy as np
1212

13-
from pandas._typing import (
14-
Dtype,
15-
PositionalIndexer,
16-
)
17-
1813
from pandas.core.dtypes.dtypes import register_extension_dtype
1914

2015
from pandas.api.extensions import (
@@ -23,6 +18,12 @@
2318
)
2419
from pandas.api.types import pandas_dtype
2520

21+
if TYPE_CHECKING:
22+
from pandas._typing import (
23+
Dtype,
24+
PositionalIndexer,
25+
)
26+
2627

2728
@register_extension_dtype
2829
class DateDtype(ExtensionDtype):
@@ -61,12 +62,12 @@ def __repr__(self) -> str:
6162
class DateArray(ExtensionArray):
6263
def __init__(
6364
self,
64-
dates: Union[
65-
dt.date,
66-
Sequence[dt.date],
67-
Tuple[np.ndarray, np.ndarray, np.ndarray],
68-
np.ndarray,
69-
],
65+
dates: (
66+
dt.date
67+
| Sequence[dt.date]
68+
| tuple[np.ndarray, np.ndarray, np.ndarray]
69+
| np.ndarray
70+
),
7071
) -> None:
7172
if isinstance(dates, dt.date):
7273
self._year = np.array([dates.year])
@@ -146,7 +147,7 @@ def __getitem__(self, item: PositionalIndexer):
146147
else:
147148
raise NotImplementedError("only ints are supported as indexes")
148149

149-
def __setitem__(self, key: Union[int, slice, np.ndarray], value: Any):
150+
def __setitem__(self, key: int | slice | np.ndarray, value: Any):
150151
if not isinstance(key, int):
151152
raise NotImplementedError("only ints are supported as indexes")
152153

@@ -160,7 +161,7 @@ def __setitem__(self, key: Union[int, slice, np.ndarray], value: Any):
160161
def __repr__(self) -> str:
161162
return f"DateArray{list(zip(self._year, self._month, self._day))}"
162163

163-
def copy(self) -> "DateArray":
164+
def copy(self) -> DateArray:
164165
return DateArray((self._year.copy(), self._month.copy(), self._day.copy()))
165166

166167
def isna(self) -> np.ndarray:
@@ -172,7 +173,7 @@ def isna(self) -> np.ndarray:
172173
)
173174

174175
@classmethod
175-
def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False):
176+
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
176177
if isinstance(scalars, dt.date):
177178
pass
178179
elif isinstance(scalars, DateArray):

pandas/tests/extension/decimal/array.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
import numbers
55
import random
66
import sys
7+
from typing import TYPE_CHECKING
78

89
import numpy as np
910

10-
from pandas._typing import type_t
11-
1211
from pandas.core.dtypes.base import ExtensionDtype
1312
from pandas.core.dtypes.common import (
1413
is_dtype_equal,
@@ -33,6 +32,9 @@
3332
)
3433
from pandas.core.indexers import check_array_indexer
3534

35+
if TYPE_CHECKING:
36+
from pandas._typing import type_t
37+
3638

3739
@register_extension_dtype
3840
class DecimalDtype(ExtensionDtype):

pandas/tests/extension/json/array.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
import string
2424
import sys
2525
from typing import (
26+
TYPE_CHECKING,
2627
Any,
2728
Mapping,
2829
)
2930

3031
import numpy as np
3132

32-
from pandas._typing import type_t
33-
3433
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
3534
from pandas.core.dtypes.common import (
3635
is_bool_dtype,
@@ -45,6 +44,9 @@
4544
)
4645
from pandas.core.indexers import unpack_tuple_and_ellipses
4746

47+
if TYPE_CHECKING:
48+
from pandas._typing import type_t
49+
4850

4951
class JSONDtype(ExtensionDtype):
5052
type = abc.Mapping

pandas/tests/extension/list/array.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import numbers
99
import random
1010
import string
11+
from typing import TYPE_CHECKING
1112

1213
import numpy as np
1314

14-
from pandas._typing import type_t
15-
1615
from pandas.core.dtypes.base import ExtensionDtype
1716

1817
import pandas as pd
@@ -22,6 +21,9 @@
2221
)
2322
from pandas.core.arrays import ExtensionArray
2423

24+
if TYPE_CHECKING:
25+
from pandas._typing import type_t
26+
2527

2628
class ListDtype(ExtensionDtype):
2729
type = list

pandas/tests/frame/common.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from __future__ import annotations
22

3-
from pandas._typing import AxisInt
3+
from typing import TYPE_CHECKING
44

55
from pandas import (
66
DataFrame,
77
concat,
88
)
99

10+
if TYPE_CHECKING:
11+
from pandas._typing import AxisInt
12+
1013

1114
def _check_mixed_float(df, dtype=None):
1215
# float16 are most likely to be upcasted to float32

pandas/tests/tseries/offsets/test_custom_business_month.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
datetime,
1212
timedelta,
1313
)
14+
from typing import TYPE_CHECKING
1415

1516
import numpy as np
1617
import pytest
@@ -29,11 +30,13 @@
2930
assert_is_on_offset,
3031
assert_offset_equal,
3132
)
32-
from pandas.tests.tseries.offsets.test_offsets import _ApplyCases
3333

3434
from pandas.tseries import offsets
3535
from pandas.tseries.holiday import USFederalHolidayCalendar
3636

37+
if TYPE_CHECKING:
38+
from pandas.tests.tseries.offsets.test_offsets import _ApplyCases
39+
3740

3841
@pytest.fixture
3942
def dt():

pandas/tseries/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# ruff: noqa: TCH004
12
from typing import TYPE_CHECKING
23

34
if TYPE_CHECKING:

pandas/tseries/frequencies.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import TYPE_CHECKING
4+
35
import numpy as np
46

57
from pandas._libs.algos import unique_deltas
@@ -26,7 +28,6 @@
2628
to_offset,
2729
)
2830
from pandas._libs.tslibs.parsing import get_rule_month
29-
from pandas._typing import npt
3031
from pandas.util._decorators import cache_readonly
3132

3233
from pandas.core.dtypes.common import (
@@ -42,6 +43,8 @@
4243

4344
from pandas.core.algorithms import unique
4445

46+
if TYPE_CHECKING:
47+
from pandas._typing import npt
4548
# ---------------------------------------------------------------------
4649
# Offset names ("time rules") and related functions
4750

pyproject.toml

-5
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,6 @@ exclude = [
300300
"pandas/core/algorithms.py" = ["TCH"]
301301
"pandas/core/ops/*" = ["TCH"]
302302
"pandas/core/sorting.py" = ["TCH"]
303-
"pandas/core/construction.py" = ["TCH"]
304-
"pandas/core/missing.py" = ["TCH"]
305-
"pandas/tseries/*" = ["TCH"]
306-
"pandas/tests/*" = ["TCH"]
307-
"pandas/plotting/*" = ["TCH"]
308303
"pandas/util/*" = ["TCH"]
309304
"pandas/_libs/*" = ["TCH"]
310305
# Keep this one enabled

0 commit comments

Comments
 (0)