Skip to content

Commit d385465

Browse files
authored
STYLE start enabling TCH (#51687)
* start enabling TCH * fixup * fixup --------- Co-authored-by: MarcoGorelli <>
1 parent b056c63 commit d385465

File tree

18 files changed

+119
-42
lines changed

18 files changed

+119
-42
lines changed

pandas/_config/config.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@
6666
)
6767
import warnings
6868

69-
from pandas._typing import (
70-
F,
71-
T,
72-
)
69+
from pandas._typing import F # noqa: TCH001
70+
from pandas._typing import T # noqa: TCH001
7371
from pandas.util._exceptions import find_stack_level
7472

7573

pandas/_testing/__init__.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
set_locale,
2626
)
2727

28-
from pandas._typing import (
29-
Dtype,
30-
Frequency,
31-
NpDtype,
32-
)
3328
from pandas.compat import pa_version_under7p0
3429

3530
from pandas.core.dtypes.common import (
@@ -118,6 +113,12 @@
118113
from pandas.core.construction import extract_array
119114

120115
if TYPE_CHECKING:
116+
from pandas._typing import (
117+
Dtype,
118+
Frequency,
119+
NpDtype,
120+
)
121+
121122
from pandas import (
122123
PeriodIndex,
123124
TimedeltaIndex,

pandas/_testing/_io.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
)
1414
import zipfile
1515

16-
from pandas._typing import (
17-
FilePath,
18-
ReadPickleBuffer,
19-
)
2016
from pandas.compat import get_lzma_file
2117
from pandas.compat._optional import import_optional_dependency
2218

@@ -27,6 +23,11 @@
2723
from pandas.io.common import urlopen
2824

2925
if TYPE_CHECKING:
26+
from pandas._typing import (
27+
FilePath,
28+
ReadPickleBuffer,
29+
)
30+
3031
from pandas import (
3132
DataFrame,
3233
Series,

pandas/_testing/_random.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
from __future__ import annotations
2+
13
import string
4+
from typing import TYPE_CHECKING
25

36
import numpy as np
47

5-
from pandas._typing import NpDtype
6-
8+
if TYPE_CHECKING:
9+
from pandas._typing import NpDtype
710
RANDS_CHARS = np.array(list(string.ascii_letters + string.digits), dtype=(np.str_, 1))
811

912

pandas/_testing/compat.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
"""
22
Helpers for sharing tests between DataFrame/Series
33
"""
4-
from pandas._typing import DtypeObj
4+
from __future__ import annotations
5+
6+
from typing import TYPE_CHECKING
57

68
from pandas import DataFrame
79

10+
if TYPE_CHECKING:
11+
from pandas._typing import DtypeObj
12+
813

914
def get_dtype(obj) -> DtypeObj:
1015
if isinstance(obj, DataFrame):

pandas/_testing/contexts.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@
66
import tempfile
77
from typing import (
88
IO,
9+
TYPE_CHECKING,
910
Any,
1011
Generator,
1112
)
1213
import uuid
1314

14-
from pandas._typing import (
15-
BaseBuffer,
16-
CompressionOptions,
17-
FilePath,
18-
)
1915
from pandas.compat import PYPY
2016
from pandas.errors import ChainedAssignmentError
2117

2218
from pandas import set_option
2319

2420
from pandas.io.common import get_handle
2521

22+
if TYPE_CHECKING:
23+
from pandas._typing import (
24+
BaseBuffer,
25+
CompressionOptions,
26+
FilePath,
27+
)
28+
2629

2730
@contextmanager
2831
def decompress_file(

pandas/compat/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import os
1313
import platform
1414
import sys
15+
from typing import TYPE_CHECKING
1516

16-
from pandas._typing import F
1717
from pandas.compat._constants import (
1818
IS64,
1919
PY39,
@@ -33,6 +33,9 @@
3333
pa_version_under11p0,
3434
)
3535

36+
if TYPE_CHECKING:
37+
from pandas._typing import F
38+
3639

3740
def set_function_name(f: F, name: str, cls) -> F:
3841
"""

pandas/compat/_optional.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import importlib
44
import sys
5-
import types
5+
from typing import TYPE_CHECKING
66
import warnings
77

88
from pandas.util._exceptions import find_stack_level
99

1010
from pandas.util.version import Version
1111

12+
if TYPE_CHECKING:
13+
import types
14+
1215
# Update install.rst & setup.cfg when updating versions!
1316

1417
VERSIONS = {

pandas/compat/numpy/function.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import annotations
1919

2020
from typing import (
21+
TYPE_CHECKING,
2122
Any,
2223
TypeVar,
2324
cast,
@@ -30,18 +31,20 @@
3031
is_bool,
3132
is_integer,
3233
)
33-
from pandas._typing import (
34-
Axis,
35-
AxisInt,
36-
)
3734
from pandas.errors import UnsupportedFunctionCall
3835
from pandas.util._validators import (
3936
validate_args,
4037
validate_args_and_kwargs,
4138
validate_kwargs,
4239
)
4340

44-
AxisNoneT = TypeVar("AxisNoneT", Axis, None)
41+
if TYPE_CHECKING:
42+
from pandas._typing import (
43+
Axis,
44+
AxisInt,
45+
)
46+
47+
AxisNoneT = TypeVar("AxisNoneT", Axis, None)
4548

4649

4750
class CompatValidator:

pandas/core/_numba/executor.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
Callable,
77
)
88

9+
if TYPE_CHECKING:
10+
from pandas._typing import Scalar
11+
912
import numpy as np
1013

11-
from pandas._typing import Scalar
1214
from pandas.compat._optional import import_optional_dependency
1315

1416

pandas/core/_numba/kernels/shared.py

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

3+
from typing import TYPE_CHECKING
4+
35
import numba
4-
import numpy as np
6+
7+
if TYPE_CHECKING:
8+
import numpy as np
59

610

711
@numba.jit(

pandas/core/computation/expressions.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
from __future__ import annotations
99

1010
import operator
11+
from typing import TYPE_CHECKING
1112
import warnings
1213

1314
import numpy as np
1415

1516
from pandas._config import get_option
1617

17-
from pandas._typing import FuncType
1818
from pandas.util._exceptions import find_stack_level
1919

2020
from pandas.core.computation.check import NUMEXPR_INSTALLED
@@ -23,6 +23,9 @@
2323
if NUMEXPR_INSTALLED:
2424
import numexpr as ne
2525

26+
if TYPE_CHECKING:
27+
from pandas._typing import FuncType
28+
2629
_TEST_MODE: bool | None = None
2730
_TEST_RESULT: list[bool] = []
2831
USE_NUMEXPR = NUMEXPR_INSTALLED

pandas/core/computation/pytables.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33

44
import ast
55
from functools import partial
6-
from typing import Any
6+
from typing import (
7+
TYPE_CHECKING,
8+
Any,
9+
)
710

811
import numpy as np
912

1013
from pandas._libs.tslibs import (
1114
Timedelta,
1215
Timestamp,
1316
)
14-
from pandas._typing import npt
1517
from pandas.errors import UndefinedVariableError
1618

1719
from pandas.core.dtypes.common import is_list_like
@@ -33,6 +35,9 @@
3335
pprint_thing_encoded,
3436
)
3537

38+
if TYPE_CHECKING:
39+
from pandas._typing import npt
40+
3641

3742
class PyTablesScope(_scope.Scope):
3843
__slots__ = ("queryables",)

pandas/core/indexing.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717

1818
from pandas._libs.indexing import NDFrameIndexerBase
1919
from pandas._libs.lib import item_from_zerodim
20-
from pandas._typing import (
21-
Axis,
22-
AxisInt,
23-
)
2420
from pandas.compat import PYPY
2521
from pandas.errors import (
2622
AbstractMethodError,
@@ -79,6 +75,11 @@
7975
)
8076

8177
if TYPE_CHECKING:
78+
from pandas._typing import (
79+
Axis,
80+
AxisInt,
81+
)
82+
8283
from pandas import (
8384
DataFrame,
8485
Series,

pandas/core/interchange/utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010
import numpy as np
1111

12-
from pandas._typing import DtypeObj
13-
1412
import pandas as pd
1513
from pandas.api.types import is_datetime64_dtype
1614

15+
if typing.TYPE_CHECKING:
16+
from pandas._typing import DtypeObj
17+
1718

1819
class ArrowCTypes:
1920
"""

pandas/core/sample.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import numpy as np
99

1010
from pandas._libs import lib
11-
from pandas._typing import AxisInt
1211

1312
from pandas.core.dtypes.generic import (
1413
ABCDataFrame,
1514
ABCSeries,
1615
)
1716

1817
if TYPE_CHECKING:
18+
from pandas._typing import AxisInt
19+
1920
from pandas.core.generic import NDFrame
2021

2122

pyproject.toml

+38
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ select = [
218218
"TID",
219219
# implicit string concatenation
220220
"ISC",
221+
# type-checking imports
222+
"TCH",
221223
]
222224

223225
ignore = [
@@ -288,6 +290,42 @@ exclude = [
288290
[tool.ruff.per-file-ignores]
289291
# relative imports allowed for asv_bench
290292
"asv_bench/*" = ["TID"]
293+
# TCH to be enabled gradually
294+
"pandas/core/arrays/*" = ["TCH"]
295+
"pandas/core/io/*" = ["TCH"]
296+
"pandas/core/indexers/*" = ["TCH"]
297+
"pandas/core/indexes/*" = ["TCH"]
298+
"pandas/core/internals/*" = ["TCH"]
299+
"pandas/core/groupby/*" = ["TCH"]
300+
"pandas/core/methods/*" = ["TCH"]
301+
"pandas/core/array_algos/*" = ["TCH"]
302+
"pandas/core/dtypes/*" = ["TCH"]
303+
"pandas/core/generic.py" = ["TCH"]
304+
"pandas/core/frame.py" = ["TCH"]
305+
"pandas/core/series.py" = ["TCH"]
306+
"pandas/core/resample.py" = ["TCH"]
307+
"pandas/core/nanops.py" = ["TCH"]
308+
"pandas/core/apply.py" = ["TCH"]
309+
"pandas/core/base.py" = ["TCH"]
310+
"pandas/core/algorithms.py" = ["TCH"]
311+
"pandas/core/ops/*" = ["TCH"]
312+
"pandas/core/sorting.py" = ["TCH"]
313+
"pandas/core/construction.py" = ["TCH"]
314+
"pandas/core/common.py" = ["TCH"]
315+
"pandas/core/missing.py" = ["TCH"]
316+
"pandas/core/util/*" = ["TCH"]
317+
"pandas/core/reshape/*" = ["TCH"]
318+
"pandas/core/strings/*" = ["TCH"]
319+
"pandas/core/tools/*" = ["TCH"]
320+
"pandas/core/window/*" = ["TCH"]
321+
"pandas/io/*" = ["TCH"]
322+
"pandas/tseries/*" = ["TCH"]
323+
"pandas/tests/*" = ["TCH"]
324+
"pandas/plotting/*" = ["TCH"]
325+
"pandas/util/*" = ["TCH"]
326+
"pandas/_libs/*" = ["TCH"]
327+
# Keep this one enabled
328+
"pandas/_typing.py" = ["TCH"]
291329

292330
[tool.pylint.messages_control]
293331
max-line-length = 88

0 commit comments

Comments
 (0)